Search notes:

Preprocessor: #if

#if

ifdef … elif defined… endif

The following simple example program probes the possibly predefined macros __GNUC__ and _MSC_VER to determine if the program was compiled with gcc or the Visual Studio compiler.
#include <stdio.h>

int main() {

#ifdef __GNUC__

  printf("This program was compiled with the GNU compiler.\n");

#elif defined _MSC_VER

  printf("This program was compiled with the Microsoft compiler.\n");

#else

  printf("This program was neither compiled with the GNU nor the Microsoft compiler.\n");

#endif

}
Github repository about-preprocessor, path: /if/ifdef-elif-defined-else.c

Comparing values

The #if directive allows to compare integer and character types, but not string types.
An undefined macro is considered to be 0, thus it is smaller than 1 or 2000, but neither smaller or greater than 0.
A «string» is neither smaller nor larger than another string. Conversely, any two strings are considered equal.
The following program prints
! 9 > 1111
  9 < 1111
! 'f' > 'y'
  'f' < 'y'
! 'f' == 'y'
  'f' == 'f'
! foo > bar
! foo < bar
  foo == bar
! AN_UNDEFINED_MACRO > 2000
  AN_UNDEFINED_MACRO < 2000
  AN_UNDEFINED_MACRO == 0
! AN_UNDEFINED_MACRO > 0
! AN_UNDEFINED_MACRO < 0
! AN_UNDEFINED_MACRO > 1
  AN_UNDEFINED_MACRO < 1
#include <stdio.h>

//
// https://renenyffenegger.ch/notes/development/languages/C-C-plus-plus/preprocessor/if
//


int main() {

#define NUM_1   9
#define NUM_2   1111

#if NUM_1 > NUM_2
    printf("  9 > 1111\n");
#else
    printf("! 9 > 1111\n");
#endif

#if NUM_1 < NUM_2
    printf("  9 < 1111\n");
#else
    printf("! 9 < 1111\n");
#endif


#define CHAR_1   'f'
#define CHAR_2   'y'

#if CHAR_1 > CHAR_2
   printf("  'f' > 'y'\n");
#else
   printf("! 'f' > 'y'\n");
#endif

#if CHAR_1 < CHAR_2
   printf("  'f' < 'y'\n");
#else
   printf("! 'f' < 'y'\n");
#endif

#define STRING_1   foo
#define STRING_2   bar

#if STRING_1 > STRING_2
   printf("  foo > bar\n");
#else
   printf("! foo > bar\n");
#endif

#if STRING_1 < STRING_2
   printf("  foo < bar\n");
#else
   printf("! foo < bar\n");
#endif

#if STRING_1 == STRING_2
   printf("  foo == bar\n");
#else
   printf("! foo == bar\n");
#endif

#if AN_UNDEFINED_MACRO > 2000
   printf("  AN_UNDEFINED_MACRO > 2000\n");
#else
   printf("! AN_UNDEFINED_MACRO > 2000\n");
#endif

#if AN_UNDEFINED_MACRO < 2000
   printf("  AN_UNDEFINED_MACRO < 2000\n");
#else
   printf("! AN_UNDEFINED_MACRO < 2000\n");
#endif

#if AN_UNDEFINED_MACRO == 0
   printf("  AN_UNDEFINED_MACRO == 0\n");
#else
   printf("! AN_UNDEFINED_MACRO == 0\n");
#endif

#if AN_UNDEFINED_MACRO > 0
   printf("  AN_UNDEFINED_MACRO > 0\n");
#else
   printf("! AN_UNDEFINED_MACRO > 0\n");
#endif

#if AN_UNDEFINED_MACRO < 0
   printf("  AN_UNDEFINED_MACRO < 0\n");
#else
   printf("! AN_UNDEFINED_MACRO < 0\n");
#endif

#if AN_UNDEFINED_MACRO > 1
   printf("  AN_UNDEFINED_MACRO > 1\n");
#else
   printf("! AN_UNDEFINED_MACRO > 1\n");
#endif

#if AN_UNDEFINED_MACRO < 1
   printf("  AN_UNDEFINED_MACRO < 1\n");
#else
   printf("! AN_UNDEFINED_MACRO < 1\n");
#endif

}
Github repository about-preprocessor, path: /if/cmp.c

See also

Preprocessor

Index