133

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

MD XF
  • 7,860
  • 7
  • 40
  • 71
  • 3
    `#pragma` directive survives the pre-processing stage. Unlike `#include` and `#define`. – smwikipedia Feb 28 '18 at 09:31
  • Possible duplicate of [What does #pragma once mean in C?](https://stackoverflow.com/questions/5776910/what-does-pragma-once-mean-in-c) –  Apr 17 '18 at 02:49
  • @smwikipedia you mean some pragmas survive? #pragma once is a preprocessor directive but #pragma pack is a compiler directive – Lewis Kelsey Mar 26 '20 at 14:00

10 Answers10

77

#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.

MD XF
  • 7,860
  • 7
  • 40
  • 71
Steven A. Lowe
  • 60,273
  • 18
  • 132
  • 202
  • 12
    "that may or may not apply to all machines and operating systems." - and different compilers on the same machine. And which might mean different things on different compilers. – Steve Jessop Oct 24 '08 at 08:22
62

#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 ;)

MD XF
  • 7,860
  • 7
  • 40
  • 71
SmacL
  • 22,555
  • 12
  • 95
  • 149
  • @ShaneMacLaughlin, Doesn't `pragma(1)` actully improves speed too? See http://stackoverflow.com/questions/3318410/pragma-pack-effect#comment31382945_3318475 – Pacerier May 14 '15 at 16:18
  • 4
    @Pacerier, typically not. As per jalfs comments, data that is aligned on a 4 byte boundary for 32 bit processors or 8 byte boundary for 64 bit processors will typically be loaded and stored in a single operation. Data that is aligned on smaller boundaries will take multiple operations to load or store. This is slower. – SmacL May 15 '15 at 07:34
42

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.

nick_g
  • 489
  • 1
  • 7
  • 15
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • 4
    So if I want to compile the GCC code on MSVC and need to pack the structure how exactly do I do it? – SmacL Jan 06 '09 at 12:15
  • 2
    For gcc, it's `struct __attribute__((__packed__)) PackedStructure` – Laurent Debricon Jul 26 '10 at 12:14
  • #pragma once is not realistically "compiler dependent and non-portable". It's supported on every major platform and many not-major platforms.. https://en.wikipedia.org/wiki/Pragma_once#Portability – xaxxon Jun 21 '16 at 22:52
  • 2
    Note that C99 and C11 both contain (C11) [§6.10.6 Pragma directives](http://port70.net/~nsz/c/c11/n1570.html#6.10.6) and ¶1 _Any such pragma that is not recognized by the implementation is ignored._ Even C90 says that, though it was in section §6.8.6. (This makes GCC non-compliant if it runs `hack` when it encounters a pragma it doesn't recognize, as it used to do once upon a very, very long time ago – see [`#pragma` and GCC](http://toni.technetium.be/hacker/pragma.htm), etc.) – Jonathan Leffler Nov 22 '18 at 04:14
  • GCC's syntax is always totally f-ed compared to all other alternatives - just look at what they did to inline assembly - ugh. Anyway, might not need that eye-cancer for structure packing - see: https://gcc.gnu.org/onlinedocs/gcc/Structure-Layout-Pragmas.html#Structure-Layout-Pragmas – Christoffer Bubach Aug 18 '21 at 17:05
17

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 */)

user01CU812
  • 293
  • 3
  • 10
Schildmeijer
  • 20,702
  • 12
  • 62
  • 79
7

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

MD XF
  • 7,860
  • 7
  • 40
  • 71
sandeep
  • 71
  • 1
  • 1
6

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
Justin Love
  • 4,397
  • 25
  • 36
  • 3
    All pragmas are implementation-specific -- except the #pragma STDC ... pragmas, which are standardized across all platforms (addition to C99). – Jonathan Leffler Oct 26 '08 at 17:15
6

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

TWhite
  • 737
  • 2
  • 11
  • 33
Basheer AL-MOMANI
  • 14,473
  • 9
  • 96
  • 92
3

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.

MD XF
  • 7,860
  • 7
  • 40
  • 71
suresh pareek
  • 100
  • 1
  • 12
3

#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();
Sparkzz
  • 367
  • 5
  • 14
2

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.

MD XF
  • 7,860
  • 7
  • 40
  • 71