Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Pragmas

Posted on 1997-05-01
1
Medium Priority
?
458 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 40 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

721 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