#pragma – Use in Programming

#pragma is a pre-processor directive to change some default functioning of the compiler.
We simply can say this directive is used for turning ON/OFF some functions of the compiler.
It is named “Pragma” because it instructs the compiler to use pragmatic or implementation-dependent features.

There are various pragmas supported.
Let’s understand few “Pragmas” :

1. #pragma pack — It basically instructs the compiler to “pack” or align the data members in a fashion so as to efficiently use the memory.
For example, Consider a structure :
struct hackmykaam_student
{
int rollno; //4 bytes
char grade; //1 byte
int marks; //4 bytes
};

now, some compilers would by default put some padding between these elements so as to arrange these data members properly like this:

100          | 101         | 102          | 104
rollno (1) | rollno (2) | rollno (3) | rollno (4)
grade (1) | padding…….
marks (1) | marks (2) | marks (3) | marks (4)

//(1) — 1st byte
//(2) — 2nd byte
//(3) — 3rd byte
//(4) — 4th byte

so if the compiler packs the data members in this way, calling “sizeof(obj)” (where “obj” is an object of the structure “hackmykaam_student”) function will print “4×3=12 bytes”.

Now, if it is done like the following way :

#pragma pack(1)
struct hackmykaam_student
{
int rollno; //4 bytes
char grade; //1 byte
int marks; //4 bytes
};

this will pack the data members like the following way :

100
rollno (1)
rollno (2)
rollno (3)
rollno (4)
grade (1)
marks (1)
marks (2)
marks (3)
marks (4)

now after doing the above way, i.e., using “#pragma pack(1)”, if we call “sizeof(obj)” (where “obj” is an object of the structure “hackmykaam_student”) function, it will print the size : 1×9 = 9 bytes and memory is not wasted.

Similarly, using #pragma pack(2) will align the members in the following way:

100          | 101
rollno (1) | rollno (2)
rollno (3) | rollno (4)
grade (1) | padding
marks (1) | marks (2)
marks (3) | marks (4)

now after doing the above way, i.e., using “#pragma pack(2)”, if we call “sizeof(obj)” (where “obj” is an object of the structure “hackmykaam_student”) function, it will print the size : 2×5 = 10 bytes and 1 byte memory is still wasted.

2. #pragma startup
#pragma exit

startup? — if we ask ourselves, that who calls the function “main”, then, the answer is, it is th “startup assembly” which calls the main function. In brief, what is happening in the “startup routine” will, obviously, be done before the main().

So, “” would be called first even before main() function (using #pragma startup )
and “#pragma exit ” would call that function just before exiting the current code.

For example,

#pragam startup func1
#pragma exit func2
void main()
{ //function definition }

now, whatever is done in the “func1” will be done just before main() and whatever is done in the function “func2” will be done just before exiting the current code.

*Note : gcc doesn’t support this pragma and will simply ignore this (startup and exit) pragma. This pragma is supported by “Turbo C++”.

2
Leave a Reply

avatar
1 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
DevanshuBoost test supplements Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Boost test supplements
Guest

Hi there! I understand this is somewhat off-topic but I had to ask.
Does operating a well-established blog like yours take a massive amount work?

I’m brand new to operating a blog but I do
write in my journal on a daily basis. I’d like to start a blog so I can easily share
my personal experience and views online. Please let me know if you have
any suggestions or tips for new aspiring bloggers.
Appreciate it!

Devanshu
Guest
Devanshu

It’s quite easy to start and you will figure out the things easily as you grow and there are lot of people to help you out, best of luck with you blog