C Programming - Structures, Unions, Enums - Discussion

Discussion Forum : Structures, Unions, Enums - Yes / No Questions (Q.No. 12)
12.
If a char is 1 byte wide, an integer is 2 bytes wide and a long integer is 4 bytes wide then will the following structure always occupy 7 bytes?
struct ex
{
    char ch;
    int i;
    long int a;
};
Yes
No
Answer: Option
Explanation:
A compiler may leave holes in structures by padding the first char in the structure with another byte just to ensures that the integer that follows is stored at an location. Also, there might be 2extra bytes after the integer to ensure that the long integer is stored at an address, which is multiple of 4. Such alignment is done by machines to improve the efficiency of accessing values.
Discussion:
18 comments Page 2 of 2.

Rahul said:   1 decade ago
I hope that the given option is wrong because structure can store different that types and it should be 1+2+4 = 7.

Dinesh kumar said:   1 decade ago
The answer is YES.. because they didn't mention any compiler specifications. size of struct should be 1+2+4 = 7.

Pavan said:   1 decade ago
Yes given answer is correct. I don't know the reason but structure size always multiple of 4 (gcc compiler).

Pankaj said:   1 decade ago
In some compiler eg: AVR studio, there is option to disable the padding bytes for structure.

Arj said:   1 decade ago
Adding of extra byte as wholes in structure called structure padding.

J.Mounika said:   7 years ago
Can anyone tell me the correct answer with the explanation?

Bharath said:   1 decade ago
Can any one explain what is structure padding?

Srinivas said:   8 years ago
Answer is 7 in turbo C.


Post your comments here:

Your comments will be displayed after verification.