int c = a // * ... * / b; int d =''??''''; //这是//评论,翻译好了吗? - 彼得 jacob navia写道: 最近,由于贫穷的希思菲尔德先生开始激烈辩论 无法编制程序//评论。 这是他的实用程序,所以他可以(最后)编译我的 程序:-) 嘿,谢谢:)我的TODO列表中的一件事是 编写这样的实用程序,所以我可以用ANSI编译一个大项目br /> 一致性模式并查看编译器是否会抛出任何 错误。项目来源符合(afaik!),除了 使用//评论。 Recently, a heated debate started because of poor mr heathfieldwas unable to compile a program with // comments. Here is a utility for him, so that he can (at last) compile myprograms :-) More seriously, this code takes 560 bytes. Amazing isn''t it? C is veryompact, you can do great things in a few bytes. Obviously I have avoided here, in consideration for his pedanticcompiler flags, any C99 issues, so it will compile in obsoletecompilers, and with only ~600 bytes you can run it in the toaster! --------------------------------------------------------------cut here /* This program reads a C source file and writes it modified to stdoutAll // comments will be replaced by /* ... */ comments, to easy theporting to old environments or to post it in usenet, where// comments can be broken in several lines, and messed up.*/ #include <stdio.h> /* This function reads a character and writes it to stdout */static int Fgetc(FILE *f){int c = fgetc(f);if (c != EOF)putchar(c);return c;} /* This function skips strings */static int ParseString(FILE *f){int c = Fgetc(f);while (c != EOF && c != ''"'') {if (c == ''\\'')c = Fgetc(f);if (c != EOF)c = Fgetc(f);}if (c == ''"'')c = Fgetc(f);return c;}/* Skips multi-line comments */static int ParseComment(FILE *f){int c = Fgetc(f); while (1) {while (c != ''*'') {c = Fgetc(f);if (c == EOF)return EOF;}c = Fgetc(f);if (c == ''/'')break;}return Fgetc(f);} /* Skips // comments. Note that we use fgetc here and NOT Fgetc *//* since we want to modify the output before gets echoed */static int ParseCppComment(FILE *f){int c = fgetc(f); while (c != EOF && c != ''\n'') {putchar(c);c = fgetc(f);}if (c == ''\n'') {puts(" */");c = Fgetc(f);}return c;} /* Checks if a comment is followed after a ''/'' char */static int CheckComment(int c,FILE *f){if (c == ''/'') {c = fgetc(f);if (c == ''*'') {putchar(''*'');c = ParseComment(f);}else if (c == ''/'') {putchar(''*'');c = ParseCppComment(f);}else {putchar(c);c = Fgetc(f);}}return c;} /* Skips chars between simple quotes */static int ParseQuotedChar(FILE *f){int c = Fgetc(f);while (c != EOF && c != ''\'''') {if (c == ''\\'')c = Fgetc(f);if (c != EOF)c = Fgetc(f);}if (c == ''\'''')c = Fgetc(f);return c;}int main(int argc,char *argv[]){FILE *f;int c;if (argc == 1) {fprintf(stderr,"Usage: %s <file.c>\n",argv[0]);return EXIT_FAILURE;}f = fopen(argv[1],"r");if (f == NULL) {fprintf(stderr,"Can''t find %s\n",argv[1]);return EXIT_FAILURE;}c = Fgetc(f);while (c != EOF) {/* Note that each of the switches must advance the character *//* read so that we avoid an infinite loop. */switch (c) {case ''"'':c = ParseString(f);break;case ''/'':c = CheckComment(c,f);break;case ''\'''':c = ParseQuotedChar(f);break;default:c = Fgetc(f);}}fclose(f);return 0;} 解决方案 jacob navia said: Recently, a heated debate started because of poor mr heathfieldwas unable to compile a program with // comments.Not so. It''s not difficult to compile a program with // "comments" undergcc. All I have to do is invoke gcc in non-conforming mode, thus foregoingopportunities for useful diagnostic messages - something I''m not preparedto do lightly. Here is a utility for him, so that he can (at last) compile myprograms :-)Alas, not yet. You see, the utility itself won''t compile: foo.c: In function `main'':foo.c:104: `EXIT_FAILURE'' undeclared (first use in this function)foo.c:104: (Each undeclared identifier is reported only oncefoo.c:104: for each function it appears in.)make: *** [foo.o] Error 1 Sometimes, words fail me. --Richard Heathfield"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.ukemail: rjh at above domain (but drop the www, obviously)jacob navia wrote:... poor mr heathfield ... Here is a utility for him ...Tediously childish. --------------------------------------------------------------cut here/* This program reads a C source file and writes it modified to stdout All // comments will be replaced by /* ... */ comments, to easy thePerhaps you should write a utility that also fixes nested comments thatare not allowed by C90 or C99. porting to old environments or to post it in usenet, where // comments can be broken in several lines, and messed up.*/I''m sure there are alternative one line perl scripts floating around. #include <stdio.h> Does this header define the identifier EXIT_FAILURE which you usefurther on? If so, your implementation is not conforming. <snip> Some test cases for you to consider... int c = a //* ... */b;int d = ''??''''; // this is a // comment, is it translated? --Peter jacob navia wrote: Recently, a heated debate started because of poor mr heathfieldwas unable to compile a program with // comments.Here is a utility for him, so that he can (at last) compile myprograms :-)Hey, thanks :) One of the things on my TODO list was towrite such a utility, so I can compile a large project in ANSIconformance mode and see if the compiler throws up anyerrors. The project source is conforming (afaik!) except for theuse of // comments. 这篇关于如何删除//评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-16 20:55