257 views
0 votes
0 votes
What is the Output of Following Program Snippet ?

#include <stdio.h>
  
  struct Key{
       int X;
        char P;
    };

int main()
{
  
    struct Key Values[] = {10,'A',20,'B',30,'C',40,'D'};
    printf("%d",sizeof(Values));
}

Explain your Answer ..

1 Answer

4 votes
4 votes

Consider int has 2B and Char has 1B

normally Size of Structure = 3 Bytes only ===> to make structure alignment, size of the structure should be divided by size of the highest size member ===> Size of Structure = 4 Bytes.

Values is a collection of 4 Structures ==> size of 'values' variable = 4*4 = 16 Bytes

 

Consider int has 4B and Char has 1B

normally Size of Structure = 5 Bytes only ===> to make structure alignment, size of the structure should be divided by size of the highest size member ===> Size of Structure = 8 Bytes.

Values is a collection of 4 Structures ==> size of 'values' variable = 4*8 = 32 Bytes.

 

For More information read https://www.geeksforgeeks.org/structure-member-alignment-padding-and-data-packing/

Related questions

0 votes
0 votes
0 answers
3
Rahul_Rathod_ asked Dec 28, 2018
323 views
let R be the class of recursive program and l be the class of iterative program now consider below statement S S : every program in R uses strictly more space compare to ...
1 votes
1 votes
2 answers
4