Solved

Pragmas

Posted on 1997-05-01
1
456 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
[X]
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
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

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!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

752 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