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 1 of 2.

Shalivahana nandish said:   9 years ago
Yes, @Kavi it is possible to assign i value througth structure see given below example.

#include <stdio.h>
int main()
{
struct value
{
int bit1:4;
}bit;
struct value bit2={13};
printf("%d\n", bit2.bit1);
return 0;
}
its given op:-3

Sol: Binary of 13 is 1101(left most bit is 1 considered negative value)
do the 2's comple ment and get the op:
1101
0010 1's complement
+1
0011
-3

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.

Puja Bharti said:   10 years ago
i = 40 means 40 is being assigned to i. Whenever i will be used, it will be 40. But i:40 means, we are trying to define i which will store memory of 40 bit which is equal to 5 byte.

So, it will generate an error because maximum size of int datatype is of 4 byte.
(1)

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.

Srikanth atthota said:   8 years ago
We can specify bits up to 16 bits in turbo c and 32 bits in a 32-bit machine. However, 40 bits is not compatible size number of bits with either 16 bit or 32-bit machine it results in an error called BIT FIELD TOO LARGE.

Gowtham said:   1 decade ago
int i:40;
40 bit field is required which means 5 bytes of memory.
In a 16-bit machine max int datatype can take 2 bytes of memory.

So is the Error: Bit field too large.

@Sugandh:
int i:2; should work fine.

Prakash said:   1 decade ago
@Flamenfire is wrong here.

int i:40;
is not the same as
int i=40;

On a 16-bit machine, one can declare
int i:16
int i:15
...
int i:1

But
int i:17
int i:18
...
Is not supported on 16-bit machines.

FlameNfire said:   1 decade ago
When we write "int i=40" the compiler does it "int i=0; i=40;" (compiler does it in 2 steps) ,

When we write "int i:40" the compiler just declare an integer and assign 40 to it, which saves time.

Kavi said:   10 years ago
Suppose i value is assigned in this program.

Is it possible to print I value? If yes how can I write the code? Please anyone explain it.

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


Post your comments here:

Your comments will be displayed after verification.