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.
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 ).
Sundar said:
1 decade ago
@Raja
In TurboC, it reserves 2 bytes for each integers (16 bit platform).
In GCC, it reserves 4 bytes for each ingeters (32 bit platform).
As you said, sizeof(bit) denotes the entire size of the structure.
So,
If you run your program in GCC (under DOSLinux) it will display 12 as output (3-integers x 4-bytes-each = 12-bytes).
If you run your program in TurboC (under DOS) it will display 6 as output (3 x 2 = 6).
Hope this will help you. Have a nice day!
In TurboC, it reserves 2 bytes for each integers (16 bit platform).
In GCC, it reserves 4 bytes for each ingeters (32 bit platform).
As you said, sizeof(bit) denotes the entire size of the structure.
So,
If you run your program in GCC (under DOSLinux) it will display 12 as output (3-integers x 4-bytes-each = 12-bytes).
If you run your program in TurboC (under DOS) it will display 6 as output (3 x 2 = 6).
Hope this will help you. Have a nice day!
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.
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.
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...
Raju 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
Why we got output 12 in this case? anyone help me.
int main()
{
struct value
{
int bit1;
int bit3;
int bit4;
}bit;
printf("%d\n", sizeof(bit));
return 0;
}
// Output: 12
Why we got output 12 in this case? anyone help me.
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?
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).
Vinay said:
1 decade ago
int main()
{
struct value
{
float bit1:1;
float bit3:4;
float bit4:4;
float bit5:1;
float bit6:7;
}bit;
printf("%d",sizeof(bit));
}
why it gives error
please any one give the clear and correct answer
{
struct value
{
float bit1:1;
float bit3:4;
float bit4:4;
float bit5:1;
float bit6:7;
}bit;
printf("%d",sizeof(bit));
}
why it gives error
please any one give the clear and correct answer
Ramya said:
8 years ago
#include<stdio.h>
int main()
{
struct value
{
int bit1:2;
int bit3:9;
int bit4:6;
int d:1;
}bit;
printf("%d\n", sizeof(bit));
return 0;
}
output:4
How? Please explain.
int main()
{
struct value
{
int bit1:2;
int bit3:9;
int bit4:6;
int d:1;
}bit;
printf("%d\n", sizeof(bit));
return 0;
}
output:4
How? Please explain.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers