We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
#define A 100
void main() { int b=0; int c=30;
while(1) { b++; if(b>1000) b=0; if(abs(b-c)>10) { #ifdef A #undef A #define A 200 #endif
print("A1= %d",A); } else { #ifdef A #undef A #define A 300 #endif
print("A2= %d",A); }
} }
taget result A1 =200 A2 = 300
I want to ask, Dos the #undef rules meet The C standard specifies? thank you
But maybe you should spend some time and tell what you think - and more importantly why you think - this should, or shouldn't function.
#define A 100 void main() { int b=0; int c=30; while (1) { b++; if (b > 1000) b=0; if (abs(b-c)>10) { #ifdef A #undef A #define A 200 #endif print("A1= %d",A); } else { #ifdef A #undef A #define A 300 #endif print("A2= %d",A); } } }
Note that the preprocessor does not care about any logical conditions or identations of the C/C++ source code. So the "if (abs(b-c)>10)" condition is irrelevant.
The preprocessor just walks down the code text line for text line. taget result A1 =200 A2 = 300
This is what the C compiler sees:
<=== empty line where the preprocessor define was void main() { int b=0; int c=30; while (1) { b++; if (b > 1000) b=0; if (abs(b-c)>10) { <==== empty lines where your #ifdef/#endif section was print("A1= %d",200); } else { <==== empty lines where your #ifdef/#endif section was print("A2= %d",300); } } }
Never think you can mix C flow control with preprocessor statements, since the preprocessor is just a glorious "search-replace" engine that performs the task before the C compiler starts to split the code into tokens for processing.
Note, by the way, that it helps to use the proper tags when posting source code.