hartmannathan commented on PR #15861:
URL: https://github.com/apache/nuttx/pull/15861#issuecomment-2698429383

   > > Sure, won't argue, but #define BOARD_NGPIOINT 0 and no define at all is 
the same thing. Preprocessor assigns 0 to the defines not given.
   > 
   > @LuchianMihai I don't think that's true.
   > 
   > ```
   > $ cat main.c
   > #include <stdio.h>
   > 
   > #define MY_ZERO 0
   > 
   > #ifdef MY_ZERO
   > #warning "MY_ZERO is defined!"
   > #else
   > #warning "MY_ZERO is NOT defined!"
   > #endif
   > 
   > int main(int argc, const char * argv[])
   > {
   >    printf("Hello, World!\n");
   >    return 0;
   > }
   > 
   > $ gcc main.c -o main
   > main.c:6:2: warning: "MY_ZERO is defined!" [-W#warnings]
   > #warning "MY_ZERO is defined!"
   >  ^
   > 1 warning generated.
   > ```
   
   On further checking, I see what you mean:
   
   ```
   $ cat main.c
   #include <stdio.h>
   
   #if (MY_ZERO == 0)
   #warning "MY_ZERO is defined!"
   #else
   #warning "MY_ZERO is NOT defined!"
   #endif
   
   int main(int argc, const char * argv[])
   {
        printf("Hello, World!\n");
        return 0;
   }
   
   $ gcc main.c -o main
   main.cpp:4:2: warning: "MY_ZERO is defined!" [-W#warnings]
   #warning "MY_ZERO is defined!"
    ^
   1 warning generated.
   ```
   
   So, for best safety, we need the condition to be written like this:
   
   ```
   #include <stdio.h>
   
   #if (defined MY_ZERO) && (MY_ZERO == 0)
   #warning "MY_ZERO is defined!"
   #else
   #warning "MY_ZERO is NOT defined!"
   #endif
   
   int main(int argc, const char * argv[])
   {
        printf("Hello, World!\n");
        return 0;
   }
   ```
   
   Then result is what we want:
   
   ```
   $ gcc main.c -o main
   main.cpp:6:2: warning: "MY_ZERO is NOT defined!" [-W#warnings]
   #warning "MY_ZERO is NOT defined!"
    ^
   1 warning generated.
   ```
   
   So I learned something today! Thank you!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to