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 3 of 4.

Shilpi said:   1 decade ago
Thanks piyush.

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

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;

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

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.

Rohit said:   1 decade ago
@piyush.

How the total number of bits allocated are 9.

Bhimeswar said:   1 decade ago
@piyush.

Yeah you are definetly correct good explanation.

Piyush said:   1 decade ago
Since the total no. of bits allocated are 9. since one byte consists of eight bits and the size of function only returns number of bytes allocated . so 9 bits can only fitted in two bytes rather than one byte.

Harish said:   1 decade ago
When we write the below program, the o/p executed is 8 (in GCC).

Can anyone tell me how the o/p is 8?

int main()
{
struct value
{
int bit1;
char ch;
}bit;
printf("%d",sizeof(bit));
return 0;
}

Habibali said:   1 decade ago
See, by the use of int bit:1, we are assigning bits in memory location, so in total we use 9 bits(1+4+4). Since this is a static allocation, the compiler will assign the memory space in terms of multiples of 8(1byte=8bits). so in total for 9 bits...(1byte(8)+1byte(1))=2bytes...


Post your comments here:

Your comments will be displayed after verification.