Thread: Preprocessing #if

  1. #1
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115

    Preprocessing #if

    Code:
    #include <stdio.h>
    
    struct g {} f;
    
    int main ()
    {
    #if '\0'
      printf ("\nTomato\n");
    #elif 10
      printf ("\nPotato %u\n", f);
    #elif f
      printf ("\nLadyfinger\n");
    #endif
    }
    The output of the above code is:
    Code:
    anisha@linux-uitj:~/junk/preprocessor> gcc hello.c
    anisha@linux-uitj:~/junk/preprocessor> ./a.out
    
    Potato 562924280
    This proves that f has a non zero value, so I modified the above code as follows:
    Code:
    #include <stdio.h>
    
    struct g {} f;
    int main ()
    {
    #if 0
      printf ("\nTomato\n");
    #elif 0
      printf ("\nPotato %u\n", f);
    #elif f
     printf ("\nLadyfinger\n");
    #endif
    }
    Now the first two if / else conditions fail since they are false but shouldn't the third get executed since f has the non-zero value ???

    But the output is nil:
    Code:
    anisha@linux-uitj:~/junk/preprocessor> gcc hello.c
    anisha@linux-uitj:~/junk/preprocessor> ./a.out
    anisha@linux-uitj:~/junk/preprocessor>
    Why it is so ?

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Because f is neither a macro, an integer constant or a character constant.

    GCC #if and #elif documentation
    Last edited by msh; 09-09-2010 at 06:20 AM. Reason: Formatting.

  3. #3
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Quote Originally Posted by msh View Post
    Because f is neither a macro, an integer constant or a character constant.

    GCC #if and #elif documentation
    Thank you for the wonderful link. It has cleared my doubt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. preprocessing
    By ganesh bala in forum C Programming
    Replies: 3
    Last Post: 02-18-2009, 03:24 AM
  2. C preprocessing
    By deepakwar in forum C Programming
    Replies: 7
    Last Post: 11-13-2007, 02:57 AM
  3. Shell command as a preprocessing token
    By hzmonte in forum C Programming
    Replies: 6
    Last Post: 11-01-2005, 07:45 PM
  4. Windows GUI Preprocessing
    By Jaken Veina in forum Windows Programming
    Replies: 5
    Last Post: 07-22-2005, 01:22 PM
  5. Preprocessing C
    By trem in forum C Programming
    Replies: 3
    Last Post: 04-08-2002, 01:42 PM