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.

Vinay said:   1 decade ago
int main()
{
struct value
{
float bit1:1;
float bit3:4;
float bit4:4;
float bit5:1;
float bit6:7;
}bit;
printf("%d",sizeof(bit));
}

why it gives error
please any one give the clear and correct answer

Sundar said:   1 decade ago
@Raja

In TurboC, it reserves 2 bytes for each integers (16 bit platform).
In GCC, it reserves 4 bytes for each ingeters (32 bit platform).

As you said, sizeof(bit) denotes the entire size of the structure.

So,

If you run your program in GCC (under DOSLinux) it will display 12 as output (3-integers x 4-bytes-each = 12-bytes).

If you run your program in TurboC (under DOS) it will display 6 as output (3 x 2 = 6).

Hope this will help you. Have a nice day!

Raju 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

Why we got output 12 in this case? anyone help me.

Singh Rajendra said:   1 decade ago
The answer depends upon compiler to compiler. But in general way using Turbo C/C++ compiler. It will provide 2 and If Dev-C++ or compiler under Linux is used then 4 will be provided.

Hari said:   2 decades ago
In windows base complier the answer is C (i.e. 4)
Sridevi explanation is correct

Rajen said:   2 decades ago
Depends upon the compiler used. turbo or any windows based compiler will return 2 but linux or cc/gcc will return 4 and it's because sizeof(int) is 2 for windows based compiler and 4 for linux based compiler.

Rajen said:   2 decades ago
Bit is a structure of int type and it's sizeof(int_var) returns 2.

Shalini gupta said:   2 decades ago
Here the concept of bit field is used.the value after colon tells the compiler that we are talking about bit fields and the number after it tells how many bits to allot for the field.


Post your comments here:

Your comments will be displayed after verification.