Use of #pragma in C

CPragma

C Problem Overview


What are some uses of #pragma in C, with examples?

C Solutions


Solution 1 - C

#pragma is for compiler directives that are machine-specific or operating-system-specific, i.e. it tells the compiler to do something, set some option, take some action, override some default, etc. that may or may not apply to all machines and operating systems.

See msdn for more info.

Solution 2 - C

#pragma is used to do something implementation-specific in C, i.e. be pragmatic for the current context rather than ideologically dogmatic.

The one I regularly use is #pragma pack(1) where I'm trying to squeeze more out of my memory space on embedded solutions, with arrays of structures that would otherwise end up with 8 byte alignment.

Pity we don't have a #dogma yet. That would be fun ;)

Solution 3 - C

I would generally try to avoid the use of #pragmas if possible, since they're extremely compiler-dependent and non-portable. If you want to use them in a portable fashion, you'll have to surround every pragma with a #if/#endif pair. GCC discourages the use of pragmas, and really only supports some of them for compatibility with other compilers; GCC has other ways of doing the same things that other compilers use pragmas for.

For example, here's how you'd ensure that a structure is packed tightly (i.e. no padding between members) in MSVC:

#pragma pack(push, 1)
struct PackedStructure
{
  char a;
  int b;
  short c;
};
#pragma pack(pop)
// sizeof(PackedStructure) == 7

Here's how you'd do the same thing in GCC:

struct PackedStructure __attribute__((__packed__))
{
  char a;
  int b;
  short c;
};
// sizeof(PackedStructure == 7)

The GCC code is more portable, because if you want to compile that with a non-GCC compiler, all you have to do is

#define __attribute__(x)

Whereas if you want to port the MSVC code, you have to surround each pragma with a #if/#endif pair. Not pretty.

Solution 4 - C

Putting #pragma once at the top of your header file will ensure that it is only included once. Note that #pragma once is not standard C99, but supported by most modern compilers.

An alternative is to use include guards (e.g. #ifndef MY_FILE #define MY_FILE ... #endif /* MY_FILE */)

Solution 5 - C

what i feel is #pragma is a directive where if you want the code to be location specific .say a situation where you want the program counter to read from the specific address where the ISR is written then you can specify ISR at that location using #pragma vector=ADC12_VECTOR and followd by interrupt rotines name and its description

Solution 6 - C

My best advice is to look at your compiler's documentation, because pragmas are by definition implementation-specific. For instance, in embedded projects I've used them to locate code and data in different sections, or declare interrupt handlers. i.e.:

#pragma code BANK1
#pragma data BANK2

#pragma INT3 TimerHandler

Solution 7 - C

All answers above make nice explanations for #pragma but I wanted to a add small example

I just want to explain a simple OpenMP example that demonstrate some uses of #pragma to doing its work

> OpenMp briefly is an implementation for multi-platform shared-memory > parallel programming (then we can say it's machine-specific or operating-system-specific)

let's go to the example

#include <stdio.h>
#include <omp.h>// compile with: /openmp

int main() {
   #pragma omp parallel num_threads(4)
   {
      int i = omp_get_thread_num();
      printf_s("Hello from thread %d\n", i);
   }
}

the output is

Hello from thread 0
Hello from thread 1
Hello from thread 2
Hello from thread 3

Note that the order of output can vary on different machines.

now let me tell you what #pragma did...

it tells the OS to run the some block of code on 4 threads

this is just one of many many applications you can do with the little #pragma

sorry for the outside sample OpenMP

Solution 8 - C

This is a preprocessor directive that can be used to turn on or off certain features.

It is of two types #pragma startup, #pragma exit and #pragma warn.

#pragma startup allows us to specify functions called upon program startup.

#pragma exit allows us to specify functions called upon program exit.

#pragma warn tells the computer to suppress any warning or not.

Many other #pragma styles can be used to control the compiler.

Solution 9 - C

#pragma startup is a directive which is used to call a function before the main function and to call another function after the main function, e.g.

#pragma startup func1
#pragma exit func2

Here, func1 runs before main and func2 runs afterwards.

NOTE: This code works only in Turbo-C compiler. To achieve this functionality in GCC, you can declare func1 and func2 like this:

void __attribute__((constructor)) func1();
void __attribute__((destructor)) func2();

Solution 10 - C

To sum it up, #pragma tells the compiler to do stuff. Here are a couple of ways I use it:

  • #pragma can be used to ignore compiler warnings. For example, to make GCC shut up about implicit function declarations, you can write:

     #pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
    

    An older version of libportable does this portably.

  • #pragma once, when written at the top of a header file, will cause said header file to be included once. libportable checks for pragma once support.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsrujanView Question on Stackoverflow
Solution 1 - CSteven A. LoweView Answer on Stackoverflow
Solution 2 - CSmacLView Answer on Stackoverflow
Solution 3 - CAdam RosenfieldView Answer on Stackoverflow
Solution 4 - CSchildmeijerView Answer on Stackoverflow
Solution 5 - CsandeepView Answer on Stackoverflow
Solution 6 - CJustin LoveView Answer on Stackoverflow
Solution 7 - CBasheer AL-MOMANIView Answer on Stackoverflow
Solution 8 - Csuresh pareekView Answer on Stackoverflow
Solution 9 - CSparkzzView Answer on Stackoverflow
Solution 10 - CMD XFView Answer on Stackoverflow