#define a #include"stdio.h"
#define b main()
#define c {
#define d printf("hello world\n");
#define e }
a
b
c
d
e
Will it work?
No. It didnt worked!!!
I got the following error.
syntax error at '#' token
syntax error before string constant
What exactly could be the reason?
Lets analyse it step by step, from the DOS prompt. The following steps will work only when your path variables are correct. In order to correct them you must tweak in the Environment variables.
First lets just preprocess it.
Type the following command in the DOS Prompt
gcc -E main.c>main.i
The output of preprocessing has been redirected to the main.i.
When I opened main.i, I found the following.
----------main.i file starts here---------------------
# 1 "src\\main.c"
# 1 ""
# 1 ""
# 1 "src\\main.c"
# 58 "src\\main.c"
#include"stdio.h"
main()
{
printf("hello world\n");
}
----------main.i file ends here-----------------------
Hurray!!! I found out the problem.
After preprocessing, all the #defines has been replaced by their respective counter parts. But the only #include remains as it is. It didnot get replaced by the #include "stdio.h" file. This is causing the error.
Now let me make some modifications.
#define b main()
#define c {
#define d printf("hello world\n");
#define e }
#include "stdio.h"
b
c
d
e
Now it is compiling.
Is it correct? Lets check it out.
Now run the same command
gcc -E main.c>main.i
Observe the main.i file
My god, I am getting a very big main.i file.
If you see that file, the stdio.h file has been pasted there.
The conclusion is that the input to a compiler should not have #'s in the code. All #'s should be removed at the preprocessing stage itself.
A compiler doesnot know #.
Isnt this program weird? ;)
No comments:
Post a Comment