#include <stdio.h>
#include<stdlib.h>
#define LIST.H onus;
int main ()
{
char *p,*s;
printf(" LIST.H ");
}
I expect LIST.H to print onus
as out put.
But this does not happen.
upon compiling I get a warning
temp.c:3:13: warning: missing whitespace after the macro name
and the开发者_StackOverflow output is LIST.H not onus. How can I get desired thing printed by the above macro?
UPDATE
I want to have the output asonus
with one space before and after the string.Macros names cannot have .
inside them. That's why you get the warning:
warning: missing whitespace after the macro name
, after LIST
it expects a space, but it gets a .
instead.
Also, when a macro name is inside a string(between "string"
) it is not replaced by the macro definition.
You could do this instead:
#define LISTH "onus"
// and then
printf(LISTH);
which the preprocessor will transform to:
printf("onus");
If you do:
#define LISTH "onus";
the preprocessor will transform it to:
printf("onus";);
which won't compile.
Firstly, you can't use .
in macro names.
Secondly, you should "expect" it to print ouns;
, since you included a ;
into your macro definition.
Thirdly, in order to achieve that you can use "stringization" macro-operator #
with some helper macros
#define TO_STRING_(x) #x
#define TO_STRING(x) TO_STRING_(x)
#define LIST_H onus
...
printf(" " TO_STRING(LIST_H) " ");
or, better
printf(" %s ", TO_STRING(LIST_H));
#include <stdio.h>
#define LIST_H "onus"
int main()
{
printf(LIST_H);
}
macros in strings aren't resolved, you need to layers of macro resolution to do that:
#define __STR(x) #x
#define _STR(x) __STR(x)
printf(_STR(LIST));
you also cannot have dots in macro defines last I checked, which would be what your error is about, so rather use LIST_H
...
精彩评论