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;
}
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.
Amit Mohite said:
1 decade ago
We can give this bit fields only to signed or unsigned integers. !but I can't get that these bit can not applied to even integers when we try it outside the structure ?
Anamika said:
1 decade ago
#include<stdio.h>
main()
{
struct u{
int bit:1;
char ch;
}a;
printf("%d",sizeof(a));
}
Why this program is giving 4 as o/p?
Anyone please explain.
main()
{
struct u{
int bit:1;
char ch;
}a;
printf("%d",sizeof(a));
}
Why this program is giving 4 as o/p?
Anyone please explain.
Uppyupender said:
1 decade ago
#include<stdio.h>
int main()
{
struct value
{
char bit1;
int bit3;
char bit12;
}bit;
printf("%d\n",sizeof(bit));
}
There are two characters and one integer, therefore expected output is 9(4+4+1)in gcc..but actual output is 12, why?
int main()
{
struct value
{
char bit1;
int bit3;
char bit12;
}bit;
printf("%d\n",sizeof(bit));
}
There are two characters and one integer, therefore expected output is 9(4+4+1)in gcc..but actual output is 12, why?
Praveen said:
1 decade ago
@Vinay your answer may be 12 according to the concept.
Ketan said:
1 decade ago
@Ashu is right.
1st int 4 byte is allocated and for char 1 byte, total is 5 byte, but 2nd tym char came than here 3 bytes internal padding so total is 8 byte.
1st int 4 byte is allocated and for char 1 byte, total is 5 byte, but 2nd tym char came than here 3 bytes internal padding so total is 8 byte.
MAHESH said:
1 decade ago
9 bytes =>1 bit (8 bytes) +1 byte. But by padding it gives 2 bits since single bytes can't be printed.
Ayush said:
1 decade ago
int main()
{
struct value
{
int bit1;
char ch;
}bit;
printf("%d",sizeof(bit));
return 0;
}
1 byte for char and 4 bytes for int total 1+4 = 5 bytes. But instead of 5 bytes it prints 8 bytes because there will be a hole of 3 bytes in between. Some machines store integers only at address which are multiple of 4.
{
struct value
{
int bit1;
char ch;
}bit;
printf("%d",sizeof(bit));
return 0;
}
1 byte for char and 4 bytes for int total 1+4 = 5 bytes. But instead of 5 bytes it prints 8 bytes because there will be a hole of 3 bytes in between. Some machines store integers only at address which are multiple of 4.
Chetan said:
10 years ago
@Ayush.
The answer for your question is compiler dependent. If you try in turbo C the answer would be 5.
If you try in Visual C the answer would be 8. If you try in GCC in Linux the answer would be 8.
And rest of the guys please read carefully you are responding to the people who are telling about bits and not bytes.
The answer for your question is compiler dependent. If you try in turbo C the answer would be 5.
If you try in Visual C the answer would be 8. If you try in GCC in Linux the answer would be 8.
And rest of the guys please read carefully you are responding to the people who are telling about bits and not bytes.
Robert said:
9 years ago
To clarify one aspect : run in gcc the following code (remember size of int = 4 bytes = 32 bits).
#include <stdio.h>
int main()
{
struct a
{
int bit1:1;
int bit3:4;
int bit4:4;
}bit;
printf("%d", sizeof(bit));
return 0;
}
* If you do simple math : 1 + 4 + 4 (number of bits afferent for each int variable ) = 9 bits.
* Since we are speaking about a machine that "thinks" in 4 bytes, it will adjust the value of bit structure to the size of 4 bytes.
Run below code to compare :
#include <stdio.h>
int main()
{
struct a
{
int bit1:1;
int bit3:4;
int bit4:4;
int bit5:24;
}bit;
printf("%d", sizeof(bit));
return 0;
}
The number of bits that the structure occupies is exactly 33.
The output of the program will be 8 ( bytes ) why? 32 + 1 = 4 bytes + another 4 bytes ( the machine thinks in its block of memory of 4 bytes ).
#include <stdio.h>
int main()
{
struct a
{
int bit1:1;
int bit3:4;
int bit4:4;
}bit;
printf("%d", sizeof(bit));
return 0;
}
* If you do simple math : 1 + 4 + 4 (number of bits afferent for each int variable ) = 9 bits.
* Since we are speaking about a machine that "thinks" in 4 bytes, it will adjust the value of bit structure to the size of 4 bytes.
Run below code to compare :
#include <stdio.h>
int main()
{
struct a
{
int bit1:1;
int bit3:4;
int bit4:4;
int bit5:24;
}bit;
printf("%d", sizeof(bit));
return 0;
}
The number of bits that the structure occupies is exactly 33.
The output of the program will be 8 ( bytes ) why? 32 + 1 = 4 bytes + another 4 bytes ( the machine thinks in its block of memory of 4 bytes ).
Pranay kantekure said:
9 years ago
Well said @Robert.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers