Re^2: Why nested comments not allowed?

What`s in a name? misu_ss at uhura.cc.rochester.edu
Thu Feb 22 03:29:08 AEST 1990


In article <4292 at jhunix.HCF.JHU.EDU> barrett at jhunix.UUCP (Dan Barrett) writes:
>In article <4509 at jarthur.Claremont.EDU> dfoster at jarthur.Claremont.EDU (Derek R. Foster) writes:
>>It seems to me that putting a start or end
>>comment in a string literal (like above) is a HORRIBLE programming practice
>>whether you can have nested comments or not.
>
>	Oh yes?  Well, what if you are writing a C program that generates
>C code as its output?  This is a common practice.
>

This leads me to a question on how compilers work...

Do #defines get worked out before comments are discarded...

I.E.  if I write code thus:

#define OPEN_COMMENT "/*"
#define CLOSE_COMMENT "*/"

main()
{
char *string[30]

	string = OPEN_COMMENT;
	printf("s% ", string);
	string = "This is a C comment";
	printf("s% ", string);
	string = CLOSE_COMMENT;
	printf("s%\n", string);

}

will this produce the output:

/* This is a C comment */

or will it simply do a newline and make string point to the return value of
printf? (likely causing a seg fault if string is ever assigned again...)

On gcc version 1.36 and cc (don't know version) running under SunOS4.0.3
this code produces the first output.  Any compilers out there that do comment
checking after filling out macros? 

If this always works then this is a possible solution to the problem...
Obviously, you'll have to use string functions but is that so bad?  If you
are doing something that will output C code it's going to be pretty general
in nature... There won't be a lot of cases where constant output is easiner
than variable output. 

						--mike



-- 
Mic3hael Sullivan,                    Society for the Incurably Pompous
		     		-*-*-*-*- 
...I know there are people in the world who do not love their fellow human
beings and I *HATE* people like that!  		--Tom Lehrer



More information about the Comp.lang.c mailing list