C Programming - Structures, Unions, Enums - Discussion

Discussion Forum : Structures, Unions, Enums - Find Output of Program (Q.No. 4)
4.
What will be the output of the program in 16 bit platform (Turbo C under DOS) ?
#include<stdio.h>

int main()
{
    struct value
    {
        int bit1:1;
        int bit3:4;
        int bit4:4;
    }bit;
    printf("%d\n", sizeof(bit));
    return 0;
}
1
2
4
9
Answer: Option
Explanation:

Since C is a compiler dependent language, in Turbo C (DOS) the output will be 2, but in GCC (Linux) the output will be 4.

Discussion:
38 comments Page 4 of 4.

Bhimeswar said:   1 decade ago
@piyush.

Yeah you are definetly correct good explanation.

Rohit said:   1 decade ago
@piyush.

How the total number of bits allocated are 9.

Ashu said:   1 decade ago
@piyush

You are wrong.

For int 4 byte is allocated and for char 1 byte, total is 5 byte, but that should be multiple of 4-bytes here 3 bytes internal padding.

Raka said:   1 decade ago
@Ashu piyush is right
for int 2 byte memory allocated in the 16 bit cmopiler

for int 4 byte momory allocated in the 32 bit compiler

Lijina said:   1 decade ago
#include<stdio.h>

int main()
{
struct value
{
int bit1;
int bit3;
int bit4;
}bit;
printf("%d\n", sizeof(bit));
return 0;
}

// Output: 12

Your compiler taken 4 byte for interger so inside structure 3 int 3*4=12;

Sneha said:   1 decade ago
Bitfielda can only have int, unsigned int, signed int as their data type and not float.

Shilpi said:   1 decade ago
Thanks piyush.

Strr said:   1 decade ago
When 2 bytes are enough for storing 9 bits. Why GCC takes 4 bytes ? what is the logic behind 4 bytes ?


Post your comments here:

Your comments will be displayed after verification.