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 2 of 4.
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;
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;
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;
}
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;
}
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.
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.
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.
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.
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.
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 ?
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.
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers