tags:

views:

6712

answers:

5

can u tell me usage of #pragma in c with an example

A: 

"#pragma once" (not standard c99) this header file will only be included once. An alternative to include guards (#ifndef MY_FILE #define MYFILE ... #endif)

Schildmeijer
+5  A: 

#pragma is used to do something implementation specific in C, i.e. be pragmatic for the currrent context rather than idealogically 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 ;)

Shane MacLaughlin
we would then need #karma to override (run over) the #dogma
Steven A. Lowe
LOL, #karma would be way cool. Particularly on those difficult pieces of code, e.g #karma (please work). We should definitely sit on the next language update commitee
Shane MacLaughlin
For risky, untrusted, forgotten, unreadable, but critical code sections:#include <cassert>#define karma assert
moala
+7  A: 

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

Steven A. Lowe
"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
+3  A: 

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
All pragmas are implementation-specific -- except the #pragma STDC ... pragmas, which are standardized across all platforms (addition to C99).
Jonathan Leffler
+3  A: 

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.

Adam Rosenfield
So if I want to compile the GCC code on MSVC and need to pack the structure how exactly do I do it?
Shane MacLaughlin
For gcc, it's `struct __attribute__((__packed__)) PackedStructure`
Laurent Debricon