Thread: #pragma directive

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    90

    #pragma directive

    I am trying to understand #pragma directive in language.

    https://www.geeksforgeeks.org/pragma-directive-in-c-c/

    I don't understand what can be it's benefits

    Code:
    #include<stdio.h>          
    int show(int x)
    {     
        x = 5;
        printf("x = %d", x);
     
    }
                
    int main()
    {
        show(10);
          
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Pragmas (short for pragmatic) are compiler specific ways of solving certain kinds of problems.

    Their effect is completely non-standard, so anything after the #pragma at the start of the line, you have to go read the compiler manual to find out what it means.

    Pragmas (Using the GNU Compiler Collection (GCC))
    Pragma directives and the __pragma and _Pragma keywords | Microsoft Learn

    If you're still learning C, it's not something you need to worry about.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Even after using C for 26 years (and professionally for about half of that time), I have not once had a need for the #pragma directive. That's even in a codebase of nearly 100k lines that I develop/maintain.

  4. #4
    Registered User
    Join Date
    Sep 2022
    Posts
    55
    That's a little surprising I always enable compiler warnings at a high level (if not the highest), check all warnings I get, and disable those that are only informal or those that I won't fix using pragma directives, after double checking. Of course this takes some additional time. And it may require branching for the preprocessors of different compilers. I'm only a programming hobbyist though (for ~15 years now).

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    aGerman: If there's any type of warning that I don't care about, I'll generally want to ignore all warnings of that type, so I can just add a -Wno-foo option when building. But usually I'll try to fix what's causing the warning instead. (E.g., for unused variable warnings, I'll either add -Wno-unused-variable to the build options or just remove the unused variables.)

  6. #6
    Registered User
    Join Date
    Sep 2022
    Posts
    55
    I agree as long as you have full control to the compilation. As soon as you publish code you do neither know the compiler nor the settings that people prefer. At some point I began using several commonly used compilers like GCC, Clang, and MSVC. I also often push/pop the suppression of certain warnings for the whole file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pragma pack in a preprocessing directive
    By adetheheat in forum C Programming
    Replies: 5
    Last Post: 12-24-2019, 12:55 PM
  2. #pragma directive
    By Usha in forum C Programming
    Replies: 1
    Last Post: 07-13-2014, 12:57 AM
  3. using directive.
    By leonm54 in forum C++ Programming
    Replies: 3
    Last Post: 07-16-2010, 10:08 AM
  4. #pragma directive
    By roaan in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 04:17 AM

Tags for this Thread