What is the difference between these compiler directives?

What is the difference between these directives?

#ifdef FOO

#if defined FOO

#if defined(FOO)

I use the CCS compiler, but I'm interested in other C compilers .

+3
source share
4 answers

From what I saw, the main use #if definedis checking multiple macros on the same line. Otherwise, for single definitions of macro definitions, they are identical to my knowledge.

#include <stdio.h>
int main()
{
#if defined(FOO) && defined(BAR)
    printf("foobar!\n");
#else
    printf("nothing.\n");
#endif
    return 0;
}
$ tcc -DFOO -run ac 
nothing.
$ tcc -DBAR -run ac 
nothing.
$ tcc -DFOO -DBAR -run ac 
foobar!

, gcc -Wall -ansi a.c, , #if defined ANSI C. , ANSI C 1987 #if defined ANSI - ANSI- , .

#if defined,

#ifdef FOO
#ifdef BAR
    printf("foobar!\n");
#endif /* BAR */
#endif /* FOO */

, Redhat C

#if defined MACRO #ifdef MACRO.

+9

3 C99. #ifdef , , , :

  • :

    #if defined(ONE_THING) && define(ANOTHER_THING) && (3 < 4)
    ...
    #endif
    
  • , #elif defined(...), #else, #ifdef:

    #if defined(ONE_THING)
    ...
    #elif defined(ANOTHER_THING)
    ...
    #elif defined(THIRD_THING)
    ...
    #else
    ...
    #endif
    

defined ; , .

+3

#ifdef #if, , , , .

Turbo C, #ifdef, #if

+1

Originally it existed only #ifdef, but when it #ifwas necessary, it was necessary defined()to #ifmove it #ifdef.

+1
source

Source: https://habr.com/ru/post/1719244/


All Articles