This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

BOLD

#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

  • To view the results of the defines, you can look at the output of the preprocessor in a file:

    http://www.keil.com/support/man/docs/c51/c51_cm_ifile.htm

    That way you can see what the results of the #ifdefs.

  • Nothing strange with #undef.

    The defined symbols are just a text-based cut and paste in your source code, while the #undef keeps the preprocessor happy that you aren't accidentally trying to clash preprocessor symbol names.

    But how is "BOLD" a summary of your post, that may help other readers to understand what this thread is about?

  • Sorry for the summary type error, the summary should be "Question of Keil C complier Preprocessor"

    but the Preprocessor I put it in if…else statement

    I think the complier Preprocessor won't know what the variable it is . so I think the code will not print correct target data , but In a fact it's work fine , so I don't understand why?

    Many Thanks for your answer ...

  • 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.