#pragma usage

Posted on 2003-03-06
Medium Priority
Last Modified: 2010-05-18
Can someone explain with a simple example the usage of
#pragma directive.

Question by:arut
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 12

Expert Comment

ID: 8079244
#pragma inline_depth( [0... 255] )

Controls the number of times inline expansion can occur by controlling the number of times that a series of function calls can be expanded (from 0 to 255 times).
LVL 12

Expert Comment

ID: 8079261
Another example:

#if _M_IX86 == 500
#pragma message( "Pentium processor build" )

This will generate the message "pentium processor build", if your processor is a pentium one.

Author Comment

ID: 8079279
Is inline_depth a macro understood by a compiler?

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Accepted Solution

bharat_mane earned 300 total points
ID: 8079316
LVL 12

Expert Comment

ID: 8079340
yes its' a kind of macro.
LVL 46

Expert Comment

by:Kent Olsen
ID: 8080656

In the MicroSoft URL mentioned above, take particular note of the opening paragrpah's last line, "Pragmas are machine- or operating system-specific by definition, and are usually different for every compiler."

This means that a #pragma statement that works just fine with your MicroSoft compiler may fail with Borland, gcc, or any other compiler.  (The inverse is also true.)

The standard states that unknown pragma directives are ignored.  The intent is that, since #pragma directives are meant to control machine-specific characteristics, they will be ignored on other platforms.  The byproduct is that the program might easily compile when ported to another system, but there is no assurance that it will run.

Often, the default characteristic of a C/C++ "struct" is that items contained within the struct are word aligned for speed.  If you want the items byte aligned, you include the correct #pragma directive.

#pragma align=b
#pragma align=byte

is a perfectly valid pragma statement.  Any system that uses the "align" parameter to control data alignment will pack all subsequent definitions so that they occupy the least amount of space.  But not all systems use "align" for this parameter.  If compiled on one of these systems, the compiler will parse the statement, recognize that "align" is not in its repertoire, an continue without affecting the rest of the compilation.

#pragma pack

is used in other environments.  So you have to know which one is appropriate for your system.  You could play it safe and include both.

Unknown parameters are supposed to be ignored.  Illegal parameters are errors.

#pragma align=byte
#pragma align=word
#pragma align=hword
#pragma align=dword

All of these are valid on at least one platform.  If one of these statements is compiled on a system that doesn't support that alignment the compiler generates an error.

This ends today's lesson on #pragma.  :)

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question