Pragma Document

B

Ben Pfaff

Can anybody tell me where i can find a detailed document on #pragma ....

There aren't many details to tell. #pragma is (in C90) entirely
or (in C99) mostly implementation-dependent. If you want to know
how your compiler interprets #pragma, refer to its documentation.
 
V

Vijay Kumar R Zanvar

Shri said:
Can anybody tell me where i can find a detailed document on #pragma ....

--shri

The ANSI C standard declares #pragma as implementation-defined.
There is one small story, which I came across while reading the book,
Deep C Secrets:

When the ANSI C standard was under development, the
pragma directive was introduced. Borrowed from Ada, #pragma
is used to convey hints to the compiler, such as the desire
to expand a particular function as in-line or suppress some
range checks. #pragma met with some initial resistancee from
a gcc implementor, who took the "implementation-defined" effect
very literally. In gcc version 1.34, the use of pragma caused
the compiler to stop compiling and launch a computer game instead.
The gcc version 1.34 manual said:

"The #pragma command is specified in the ANSI standard to have
an arbitrary implementation-defined effect. In GNU C preprocessor,
#pragma first attempts to run the game "rogue"; if that fails,
it tries to run the game "hack"; if that fails, it tries to run
GNU Emacs displaying Tower Of Hanoi; if that fails, it reports
a fatal error. In any case, preprocessing does not continue."

[ The "#pragma once" directive is employed as a guard for header
files. Its main purpose is to avoid multiple inclusions.
This equivalent to:

#ifdef __HEADER_FILE__
#define __HEADER_FILE__

/* some declarations go here ... */

#endif /* __HEADER_FILE__ */ ]
 
N

Neil Kurzman

since #pragma is a command to the compiler it is compiler specific.
Therefore it will vary by compiler or compiler version.
 
E

Emmanuel Delahaye

In said:
Can anybody tell me where i can find a detailed document on #pragma ....

On you compilers documentation only.

(Don't use #pragma to stay portable)
 
A

Arthur J. O'Dwyer

The ANSI C standard declares #pragma as implementation-defined.
There is one small story, which I came across while reading the book,
Deep C Secrets:

[by Peter van der Linden, BTW]
[ The "#pragma once" directive is employed as a guard for header
files. Its main purpose is to avoid multiple inclusions.

It is not standard, and IMHO probably never will be, due to the
difficulty of specifying exactly what it is that '#pragma once' is
supposed to do. (Consider two identical copies of a header file
in different source directories, and a TU that #includes both of
them. What is the effect of '#pragma once' here? Make sure to
give it implementation-independent and well-defined semantics. :)
This [is] equivalent to:

#ifdef __HEADER_FILE__
#define __HEADER_FILE__

/* some declarations go here ... */

#endif /* __HEADER_FILE__ */

Except that no extra macro-names are introduced into either the
header file itself *or* the translation unit with the '#include'
directive in it, and of course the sense of the implicit "comparison"
is reversed from what Vijay has written.
The Standard C method of "header guards" looks like this. Note
particularly the '#ifndef' and the name 'H_FOO', which is guaranteed
to be in the user's namespace and not the implementation's (unlike
'__HEADER_FILE__', which makes Vijay's example non-conforming).

#ifndef H_FOO /* for a file "foo.h" */
#define H_FOO

/* header contents: type definitions and extern declarations */

#endif

There is also some C++ magic that usually gets wrapped around
that, in case the library gets used in a C++ program, but I can
never remember all the details of it. Check Google.

-Arthur
 
V

Vijay Kumar R Zanvar

Arthur J. O'Dwyer said:
The ANSI C standard declares #pragma as implementation-defined.
There is one small story, which I came across while reading the book,
Deep C Secrets:

[by Peter van der Linden, BTW]
[ The "#pragma once" directive is employed as a guard for header
files. Its main purpose is to avoid multiple inclusions.

It is not standard, and IMHO probably never will be, due to the
difficulty of specifying exactly what it is that '#pragma once' is
supposed to do. (Consider two identical copies of a header file
in different source directories, and a TU that #includes both of
them. What is the effect of '#pragma once' here? Make sure to
give it implementation-independent and well-defined semantics. :)
This [is] equivalent to:

Yes. It *ees*.
Except that no extra macro-names are introduced into either the
header file itself *or* the translation unit with the '#include'
directive in it, and of course the sense of the implicit "comparison"
is reversed from what Vijay has written.
The Standard C method of "header guards" looks like this. Note
particularly the '#ifndef' and the name 'H_FOO', which is guaranteed
to be in the user's namespace and not the implementation's (unlike
'__HEADER_FILE__', which makes Vijay's example non-conforming).

#ifndef H_FOO /* for a file "foo.h" */
#define H_FOO

/* header contents: type definitions and extern declarations */

#endif

There is also some C++ magic that usually gets wrapped around
that, in case the library gets used in a C++ program, but I can
never remember all the details of it. Check Google.

-Arthur

Thank you sir for pointing out my mistakes.

Vijay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Pragma Document 3
Regarding #pragma 5
Pragma 15
#pragma cancel_handler 8
pragma 11
Digital Signature field form in PDF generated document from HTML 5
C Macro define contain symbol # 19
Why #pragma pack not take effect? 5

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top