문제

Sorry I missed semicolon, I have added now.

#include <stdio.h>
#define m(i,j) (i##j)
int main(){
m(hello,world);
return 0;
}

While compiling I got below error main.c: In function ‘main’: main.c:8:1: error: ‘helloworld’ undeclared (first use in this function) main.c:8:1: note: each undeclared identifier is reported only once for each function it appears in

But

#include <stdio.h>
#define m(i,j) (i##j)
int main(){
m(1,2);
return 0;
}

works perfectly and give answer as 12

도움이 되었습니까?

해결책 2

After your macro is applied you just get this:

#include <stdio.h>
int main(){
(helloworld)
return 0;
}

And the compiler is saying, what is helloworld?

다른 팁

You have to remember that the preprocessor replaces macro invocation quite literally. So in your case the macro invocation

m(hello,world)

becomes

(helloworld)

And that's not a valid statement or expression in C. The error you ask about is because the compiler don't know what helloworld is, but there should also be other errors because of the missing semicolon.

The other example, where m(1,2) is replaced by (12) at least is more valid in that there are no undeclared identifiers. But it's still missing a semicolon.

Check this out

#include <stdio.h>
#define m(i,j) (i##j)
int main(){
m('hello','world')
return 0;
}

When you concatenated both hello, and world, using a macro, you did not really create a char* or char[], but a symbol.

Because the symbol helloworld is not defined before in the code, you would of course get an undefined error. To solve such an issue, you could simply define the symbol to be an int or any other primitive, and put the declaration of it before the macro:

int helloworld;

and then

m(hello,world) = 10;
printf ("%d", helloworld);

You can concatenate strings just by placing them next to each other.

#include <stdio.h>
#define m(i,j) (i j)
int main(){
m("hello","world");
return 0;
}

or just

int main(){
"hello" "world";
return 0;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top