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.
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.
Bhimeswar said:
1 decade ago
@piyush.
Yeah you are definetly correct good explanation.
Yeah you are definetly correct good explanation.
Rohit said:
1 decade ago
@piyush.
How the total number of bits allocated are 9.
How the total number of bits allocated are 9.
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.
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
for int 2 byte memory allocated in the 16 bit cmopiler
for int 4 byte momory allocated in the 32 bit compiler
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;
Sneha said:
1 decade ago
Bitfielda can only have int, unsigned int, signed int as their data type and not float.
Shilpi said:
1 decade ago
Thanks piyush.
Strr said:
1 decade ago
When 2 bytes are enough for storing 9 bits. Why GCC takes 4 bytes ? what is the logic behind 4 bytes ?
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).
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers