Solved

Pragmas

Posted on 1997-05-01
1
455 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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Programming Arduino to control a Max7219 using C 2 148
Global Keyboard Hooks Blocked 4 88
Computer slow / BSOD 10 70
Can someone explain the following statement in C? 4 31
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 opening and reading files 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.

685 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