C Programming - Structures, Unions, Enums - Discussion

Discussion Forum : Structures, Unions, Enums - Point Out Errors (Q.No. 7)
7.
Point out the error in the program in 16-bit platform?
#include<stdio.h>

int main()
{
    struct bits
    {
        int i:40;
    }bit;

    printf("%d\n", sizeof(bit));
    return 0;
}
4
2
Error: Bit field too large
Error: Invalid member access in structure
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
19 comments Page 2 of 2.

Maheshwar Prasad said:   1 decade ago
Size is large so can't accept this "i:40" because int 32 accept 32-bit value.

Santosh said:   1 decade ago
I:30 is right and it will give output 4 in 32-bit OS.

Deepak said:   1 decade ago
If we assign like int i:30;

Is it right or wrong ?

Chanders said:   1 decade ago
Yes, you are apsolutely correct.

Vikram said:   1 decade ago
It means 'i' can store an integer which contains 40 bits in it.

Generally integer size can be either 16 bits in a 16 bit machine or 32 bits in a 32 bit machine.

But here, we are trying to store 40 bits for integer which is wrong and so compiler generates error.

Ram said:   1 decade ago
int i:40;

What does it mean the above statement?

Kamal said:   1 decade ago
'i:40' it means allocating the bit size for i.

Vimal said:   1 decade ago
What is meant by "i:40"?

Harshith M L said:   1 decade ago
The width of int is 4 bytes (32 bits) or 2 bytes (16 bits) depending upon the machine, so the allocation for int is upto 32 bits. The declaration int i:40; exceeds the width of int so the compiler generates the error:width of 'i' exceeds its type.


Post your comments here:

Your comments will be displayed after verification.