Note 1

Take Note:

Take a note while surfing.

Note With Ink

Give your Note a Colorful Tag.

Easy to Access

Stay on same information and in Sync wherever you are.

Note 2

Take Note:

Organize your information,It may take Shape.

Think With Ink

Differ your Content by Color.

Easy to Access

Easy to pull up your content from anywhere anytime.

Note 3

Take Note:

Don't Let information to miss,Because it take shape

Note With Ink

Simple an Easy Way to take a note.

Easy to Access

Get the same in next visit.

Please wait...

C Programming :: Floating Point Problems

5. What is the output of the following 'C' program ?

# pragma pack(2)
struct SIZE {
int i;
char ch ;
double db ;
} ;
main () {
printf ( "%d\n",sizeof(struct SIZE) );

Answer: Option B

Explanation :

Pragma pack instructs the compiler to pack structure members with particular alignment. Most compilers, when you declare a struct, will insert padding between members to ensure that they are aligned to appropriate addresses in memory (usually a multiple of the type's size). This avoids the performance penalty (or outright error) on some architectures associated with accessing variables that are not aligned properly. For example, given 4-byte integers and the following struct:

struct SIZE {

int i;

char ch ;

double db ;

} ;

The compiler could choose to lay the struct out in memory like this:

1 byte

2nd byte
3rd byte
4th byte
int i
char ch
double db









the size of this structure will be 16.

with pragma pack (1)

1 byte

int i(1)

int i(2)

int i(3)

int i(4)

char ch(1)

double db(1)



double db(8)

 and the size of strut will be 13.

with pragma pack (2)

1st byte        2nd byte

int i(1)             int i(2)

int i(3)             int i(4)

char ch(1)        double db(1)

.                         .

double db(6)    double db(7)

double db(8)    padding

the size of struct will be 14  (Answer). if pragma pack (4) is given then in 32 bit size of sruct will be 16.

pragma pack tells the compiler the boundary to align objects in a structure to in order to improve access times.

Asked In :: Global Edge

Post Your Answer Here:     

Reply     |    Rate This: +2 -0      |     Report     |     To Reading List

Report Error

Please Login First Click Here