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 1 of 4.
Dhruv said:
5 years ago
Total 4+4+1 = 9 bits (not bytes).
1 byte = 8 bits.
So the answer is 2 bytes.
1 byte = 8 bits.
So the answer is 2 bytes.
(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 ?
Vijay vora said:
1 decade ago
@Harish.
See in structure if 1 st element is int and second one is character then in linux padding is done,
Means int size is 4 byte, char size is 2 byte so, actually size will be done 6 bytes but compiler make this as multiple of 4.
So, (4+4=8).
See in structure if 1 st element is int and second one is character then in linux padding is done,
Means int size is 4 byte, char size is 2 byte so, actually size will be done 6 bytes but compiler make this as multiple of 4.
So, (4+4=8).
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers