Solved

Pragmas

Posted on 1997-05-01
1
443 Views
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 ?
0
Comment
Question by:vishy050197
1 Comment
 
LVL 4

Accepted Solution

by:
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.

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now