I was under the impression that #define and #include can be written anywhere in our code, as long as they don't produce any syntax errors when the pre-processor processes the macros before it is fetched to the compiler.
I ran the following code:
#include <stdio.h> int main(void) { int B = A; #define A 4 printf("%d", B); return 0; } and it produced the following error:
prog.c: In function 'main': prog.c:4:13: error: 'A' undeclared (first use in this function) int B = A; ^ prog.c:4:13: note: each undeclared identifier is reported only once for each function it appears in
But when I do this, it works!
#include <stdio.h> int main(void) { #define A 4 int B = A; printf("%d", B); return 0; } Not sure what am I missing here, but why does compiler give such an error "undeclared A"?
Is it so that when pre-processor reads the line #define A 4 it will start replacing any A with 4 from the subsequent lines of code?