Search code examples
cgccmacrospragma

Is there any relation between pragma and predefined macros in C programming language, using GCC or any other compiler?


Recently I was asked if I know what pragma and macro is, and since I had used #pragma directive, I answered yes. But when I failed to implement a predefined macro (-Dsome_macro in gcc command arguments to define the macro some_macro) I was told that it is because I do not know what pragma is!

I know that in C and C++ it is possible to define a macro which contains pragma keyword in it, and I think it is not relevant to this situation. I looked up features of #pragma and I was not able to find any thing related to predefined macros. So I was wondering if they were referring to another type of pragma or another concept in programming for it.

Sorry for the vague question, but is there any relation between pragma and macro in c programming language according to what I described?


Edit:

OK, I finally asked the person directly and it turned out that it was just a misunderstanding. And by pragma he simply meant the #pragma directive and there was no relation between it and the macro.

Thanks to every one who responded and sorry for the confusing question.


Solution

  • The formal definition of predefined macros (C17 6.10.8) are things like __STDC__ which are specified by the compiler and/or standard libraries rather than the application programmer.

    -D is a special gcc feature to define macros at compile-time - I guess you could call those "pre-defined" if you like but I don't think that's a (de facto) standard definition of the term.

    #pragma is generally used whenever a compiler wants to use an implementation-defined feature but without having that feature collide with other compilers. A compiler which doesn't recognize a pragma should ignore it.

    There are a very special case #pragma specified by the C standard such as #pragma STDC FENV_ACCESS coming with the floating point environment fenv.h header. I guess these are somewhat similar to predefined macros, but this is such a specialized use that I wouldn't expect the average programmer to know anything about it. Personally, I barely ever used fenv.h. Other than these rare STDC pragmas, there's no obvious relation between pragmas and predefined macros.

    As of C99, C has the _Pragma operator which is also a special snowflake barely ever used, the purpose is indeed to enable pragmas inside function-like macros.

    None of the most common uses of pragmas have anything to do with function-like macros or pre-defined macros though. I'd say the most common use of pragmas encountered by the average programmer is certain semi-portable ones like #pragma pack() used to set struct padding/alignment. Or to enable/disable compiler warnings locally, such as this:

    #pragma GCC diagnostic push
    #pragma GCC diagnostic ignored "-Wpedantic"
    int arr[0];
    #pragma GCC diagnostic pop
    

    Similarly, this could have been written as a function-like macro with the _Pragmaoperator:

    #define declare_skunky_array(name)                 \
      _Pragma("GCC diagnostic push")                   \
      _Pragma("GCC diagnostic ignored \"-Wpedantic\"") \
      int name[0];                                     \
      _Pragma("GCC diagnostic pop")