tags:

views:

262

answers:

5

Can anyone please tell me the use of pragma in C and Ada, with some examples if possible.

+4  A: 

In C, most pragmas are compiler/platform specific (although a few like #pragma once are implemented widely).

Here's a page on gcc pragmas and another for Microsoft VC pragmas.

R Samuel Klatchko
+2  A: 

..for the Pragmatic programmer ;-)


The `#pragma' directive is the method specified by the C standard for providing additional information to the compiler, beyond what is conveyed in the language itself. Three forms of this directive (commonly known as pragmas) are specified by the 1999 C standard. A C compiler is free to attach any meaning it likes to other pragmas.

http://gcc.gnu.org/onlinedocs/cpp/Pragmas.html

http://msdn.microsoft.com/en-us/library/d9x1s805%28VS.71%29.aspx

Good Luck!!

CVS-2600Hertz
OH MAN!! SAMUEL. ARE WE THE SAME SOUL IN TWO BODIES OR WAT!! Anyways same links, same time.. AWESOME!! My Legendary moment of the day!!
CVS-2600Hertz
+4  A: 

There are three standard pragmas in C99:

#pragma STDC FP_CONTRACT on-off-switch
#pragma STDC FENV_ACCESS on-off-switch
#pragma STDC CX_LIMITED_RANGE on-off-switch

Where 'on-off-switch' is one of ON, OFF, DEFAULT.

These can be used at compile time to modify the behaviour of the compiler in arcane ways (these ones are related to the C99 floating point behaviour). The standard reserves STDC for standard pragmas; other people can use anything else they like.

There are non-standard pragmas too - as pointed out by Samuel Klatchko.

Basically, they are a way to get the compiler to do non-standard things in a semi-standard way. One example is '#pragma pack' which means that structures are created with no padding between members, even if that means that access to those members will be sub-optimal (space is more important than time, presumably). This isn't a particularly good idea (though those who use it will object to that); but it is a commonly perceived requirement, so compilers often support it.

I've been programming in C for - oh, 25 years and a bit to spare. I've not needed to use pragma once. I've toyed with it a couple of times, but never really needed to use it. Maybe I'm lucky.

Jonathan Leffler
A: 

You can basically think of them as commands to the compiler. These commands are used during compilation process.

Example

#include<windows.h>

#pragma comment("lib","shell32.lib")

wmain(){
......
}

In the above example you are basically asking the linker to include shell32.lib automatically while linking you program. without that you have to manually specify shell32.lib in the command line of cl.exe

Another example would be you can ask the compiler to functions in the final executable...

#pragma auto_inline(on)
int functionToBeInlined()
{
//.....
}
#pragma auto_inline(off)

All occurrences of the above function will be inlined.

pragma reference of VC++ compiler

each compiler has there own specific pragmas

Vineel Kumar Reddy
+3  A: 

In Ada, "A pragma is a compiler directive." Many are defined by the language, but implementation-defined pragmas are permitted. The Rationale for Ada 2005 offers many examples.

trashgod