Posted on 1997-05-01
Last Modified: 2011-09-20
When do you use pragmas ? Can anyone post me a sample
piece of code explaining the use of a pragma ?
Question by:vishy050197
1 Comment

Accepted Solution

emmons earned 20 total points
ID: 1250209
The #pragma directives are a way for each C compiler to offer machine-specific features while retaining overall compatibility with the C language. Since pragmas are machine-specific by definition, they usually are different for every C compiler. There are some common pragmas, but these are not mandated by any body, it is simply a convention. There are a couple of reasons to use a pragma : override compile options or to set different values for different parts of code (there may be others, but those are the reasons that come to mind)
I am citing references here from Microsoft C
One pragma that I use a lot, is to change the packing of the code around a structure.
So, for example,
#pragma pack 1
struct fishbait { int i;    char j;    int i2;    char i3;    int k; }
#pragma pack

would result in a structure that was 8 bytes long rather than if I had said 'pack 2' which would have put extra bytes after the chars, and resulted in a 10 byte structure.

#pragma data_seg "name"
allows you to specify the name of the segment in which the following body of code is to be placed.

Microsoft has nearly 30 pragma commands,
alloc_text      inline_depth      page
auto_inline      inline_recursion      pagesize
check_pointer      init_seg      pointers_to_members
check_stack      intrinsic      setlocale
code_seg      linesize      skip
comment      message      subtitle
data_seg      native_caller      title
function      optimize      vtordisp
hdrstop      pack      warning

Borland supports a different set
#pragma argsused
#pragma anon_struct
#pragma codeseg
#pragma comment
#pragma exit
#pragma hdrfile
#pragma hdrstop
#pragma inline
#pragma intrinsic
#pragma message
#pragma option
#pragma saveregs
#pragma startup
#pragma warn

 I hope that this helps.


Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

786 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