473,545 Members | 289 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pragma

Hello all,

I'm a beginner in C...May i like to know the difference between a
#pragma and a #define....
Also,yet i'm unclear what a pragma is all about as i can find topics on
it only in high-standard books...
Thanks in advance!

May 13 '06 #1
15 3739
muttaa wrote:
Hello all,

I'm a beginner in C...May i like to know the difference between a
#pragma and a #define....
Also,yet i'm unclear what a pragma is all about as i can find topics on
it only in high-standard books...


For a beginner, the difference is simple: #define is
something you use, but #pragma is something you avoid.

#define creates a macro. When the macro name appears
later on in the program, the text of the macro definition
replaces the name. (That's a loose description, not perfect
but good enough for all but language lawyers.) A macro is
a kind of shorthand; using the shorthand, you may be able
to write your program more briefly and more clearly.

#pragma tells the compiler to do something peculiar. It
may tell the compiler to allocate a variable on an 8192-byte
boundary, or to use a non-standard mechanism for calling a
function, or that some special action is to be taken before
main() is called, or ... With a very few exceptions, the
effect of a #pragma directive is entirely defined by the
particular compiler you happen to be using; switch to a
different compiler and the same #pragma in your source code
may have an entirely different effect. In other words, #pragma
ties your code to just one compiler, thus giving up nearly all
the portability C is famous for.

Use #define when it seems useful. When #pragma seems like
a good idea, you are probably hallucinating and should get some
bed rest.

--
Eric Sosman
es*****@acm-dot-org.invalid
May 13 '06 #2

Eric Sosman wrote:
When #pragma seems like
a good idea, you are probably hallucinating and should get some
bed rest.

So what would you suggest when stucture packing is required for a
program. As far as I know there is no standar C way to do it but it is
often required in embedded systems. Am I wrong on this?

May 14 '06 #3
>> When #pragma seems like
a good idea, you are probably hallucinating and should get some
bed rest.
So what would you suggest when stucture packing is required for a


structure packing is NOT required for a C program. I don't know
what kind of program it is, but it's not C.
program. As far as I know there is no standar C way to do it but it is
often required in embedded systems. Am I wrong on this?


Gordon L. Burditt
May 14 '06 #4
Gordon Burditt wrote:
When #pragma seems like
a good idea, you are probably hallucinating and should get some
bed rest.


So what would you suggest when stucture packing is required for a

structure packing is NOT required for a C program. I don't know
what kind of program it is, but it's not C.


It may not be required, but it is often used in embedded environments.
Packing might even be required, if its use reduced code size.

--
Ian Collins.
May 14 '06 #5
So what would you suggest when stucture packing is required for a
program. As far as I know there is no standar C way to do it but it is
often required in embedded systems. Am I wrong on this?


If you're looking for a TRULLY portable method, you can use pointer
trickery.

All the three "char" types are guaranteed to use all of their bits in
their value representation. Armed with this knowledge, plus with the help
of CHAR_BIT, you can manipulate the bits in memory directly.

If CHAR_BIT != 8, then it will be a little harder... but not impossible.

I posted an example of this within the last two days on this newsgroup. If
you want to use Google to search, search for "pointer trickery" in a post
by me.

-Tomás

May 14 '06 #6
Ian Collins wrote:
Gordon Burditt wrote:
When #pragma seems like
a good idea, you are probably hallucinating and should get some
bed rest.
So what would you suggest when stucture packing is required for a

structure packing is NOT required for a C program. I don't know
what kind of program it is, but it's not C.

It may not be required, but it is often used in embedded environments.
Packing might even be required, if its use reduced code size.


Structure packing (if it can be done at all) seldom if
ever reduces code size. In my experience, it either increases
code size and slows the program down a little, or leaves code
size unchanged and slows the program down a lot.

Structure packing can reduce data size, which is another
matter. However, there are other ways to reduce data size if
that's a concern. For example, one might transform

struct {
char x;
double y;
} thing[BIG_COUNT];

to

char thing_x[BIG_COUNT];
double thing_y[BIG_COUNT];

Structure packing is also used to make a struct match an
externally-imposed format like a packet header or some such.
This is a snare and a delusion; in the long run, It Doesn't Work.

--
Eric Sosman
es*****@acm-dot-org.invalid
May 14 '06 #7
Tomás wrote:
All the three "char" types are guaranteed to use all of their bits in
their value representation.


The signed char type isn't guaranteed to be padding free.
I don't think there's ever been a case where
there has been padding in a signed char,
but there's no guarantee.

--
pete
May 14 '06 #8
Eric Sosman wrote:
Ian Collins wrote:
Gordon Burditt wrote:
> When #pragma seems like
> a good idea, you are probably hallucinating and should get some
> bed rest.
>

So what would you suggest when stucture packing is required for a

structure packing is NOT required for a C program. I don't know
what kind of program it is, but it's not C.


It may not be required, but it is often used in embedded environments.
Packing might even be required, if its use reduced code size.

Structure packing is also used to make a struct match an
externally-imposed format like a packet header or some such.
This is a snare and a delusion; in the long run, It Doesn't Work.

It does if the externally imposed format is hardware registers.

--
Ian Collins.
May 14 '06 #9

Eric Sosman wrote:
Structure packing can reduce data size, which is another
matter. However, there are other ways to reduce data size if
that's a concern. For example, one might transform

struct {
char x;
double y;
} thing[BIG_COUNT];

to

char thing_x[BIG_COUNT];
double thing_y[BIG_COUNT];
While this does reduce data size it does nothing to maintain data
encapsulation which can be a very useful thing in increasing the
readability of the code.
Structure packing is also used to make a struct match an
externally-imposed format like a packet header or some such.
This is a snare and a delusion; in the long run, It Doesn't Work.

I am not sure that I understand why you think this. I have used, and
probably will again, structure packing for both externally imposed data
structures such as headers and to overlay something on a specific piece
of hardware. I have never had a problem with this, although I have not
had change compilers either which I will admit could cause some
problems but I was not suggesting that i would be portable. Why is it
that you say it does not work.

May 15 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2645
by: Steven T. Hatton | last post by:
Stroustrup says this: http://www.research.att.com/~bs/bs_faq2.html#macro "So, what's wrong with using macros?" "And yes, I do know that there are things known as macros that doesn't suffer the problems of C/C++ preprocessor macros. However, I have no
6
3948
by: Shri | last post by:
Can anybody tell me where i can find a detailed document on #pragma .... --shri
1
3052
by: Gustavo L. Fabro | last post by:
Greetings! Going directly to the point: myclass.h: //-------------------------------------- #pragma managed //Forward declaration
11
2851
by: ramu | last post by:
HI, Can anyone tell me about pragma? And can u give an example of how to use it?
8
7865
by: rwen2012 | last post by:
Hi, guys, I am confused with the preprocessor instruction #pragma. What does the #pragma mean? and what does this mean as follow? ==================== #pragma intterupt Timer ====================
5
3598
by: venkat | last post by:
Hi, I have come across the a pragma definition " #pragma switch direct ". I don't know any thing about pragma's. Even i when i search through google not getting exact information. what does pragma does?. what the statment "#pragma switch direct" does?. Thanks, Venkat.
26
3790
by: Rick | last post by:
I'm told that "#pragma once" has made it into the ISO standard for either C or C++. I can't find any reference to that anywhere. If it's true, do any of you have a reference I can use? Thanks...
2
3904
by: aleemakhtar1 | last post by:
wat is use of pragma directive in embedded sys ??
0
10674
debasisdas
by: debasisdas | last post by:
PRAGMA:-Signifies that the statement is a pragma (compiler directive). Pragmas are processed at compile time, not at run time. They pass information to the compiler. A pragma is an instruction to the Oracle compiler that tells it to do something. In this case you are telling Oracle to associate an error that you choose to a variable that you...
0
7465
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7805
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7752
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4944
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
701
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.