prev up next   top/contents search

comp.lang.c FAQ list · Question 11.18

Q: What does the message ``warning: macro replacement within a string literal'' mean?


A: Some pre-ANSI compilers/preprocessors interpreted macro definitions like

	#define TRACE(var, fmt) printf("TRACE: var = fmt\n", var)
such that invocations like
	TRACE(i, %d);
were expanded as
	printf("TRACE: i = %d\n", i);
In other words, macro parameters were expanded even inside string literals and character constants. (This interpretation may even have been an accident of early implementations, but it can prove useful for macros like this.)

Macro expansion is not defined in this way by K&R or by Standard C. (It can be dangerous and confusing: see question 10.22.) When you do want to turn macro arguments into strings, you can use the new # preprocessing operator, along with string literal concatenation (another new ANSI feature):

	#define TRACE(var, fmt) \
		printf("TRACE: " #var " = " #fmt "\n", var)
See also question 11.17.

References: H&S Sec. 3.3.8 p. 51


prev up next   contents search
about this FAQ list   about eskimo   search   feedback   copyright

Hosted by Eskimo North