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.

Chandana said:   8 years ago
Then the answer to given question should be 9 but it is given 2 why?

The amd size of the function returns the number of bytes and it is in bits.

Niks said:   8 years ago
4 also the correct answer because as mentioned it depends on the compiler.

Ramya said:   8 years ago
#include<stdio.h>

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

How? Please explain.

Abhishek said:   8 years ago
Thanks @Piyush.

Ramya said:   8 years ago
But in bit field we can't apply size of operator.

Then how it is?

Prakash said:   7 years ago
Actually, this is a bit field program in that, the bit field only accepts the int and char member not a float that's wise giving an error.

Shahid said:   6 years ago
9 bits = 2 bytes.

Dhruv said:   5 years ago
Total 4+4+1 = 9 bits (not bytes).
1 byte = 8 bits.
So the answer is 2 bytes.
(4)


Post your comments here:

Your comments will be displayed after verification.