C Programming - Structures, Unions, Enums - Discussion
Discussion Forum : Structures, Unions, Enums - Find Output of Program (Q.No. 13)
13.
What will be the output of the program given below in 16-bit platform ?
#include<stdio.h>
int main()
{
enum value{VAL1=0, VAL2, VAL3, VAL4, VAL5} var;
printf("%d\n", sizeof(var));
return 0;
}
Discussion:
50 comments Page 1 of 5.
Prabhakar said:
2 decades ago
Value inside enum is not properly arranged then how the answer came 2. It is my doubt only.
Srividhya said:
2 decades ago
May be the first variable(VAL1=0) is initialized to zero(integer), so the total size seems to 2 bytes only. The other variables are not initialized.
Runa said:
2 decades ago
Since val1 is assigned as 0 since its a int I nthink the sizeof usage gives us 2 bytes as answer.
Shweta said:
2 decades ago
as first variable is assigned the value in the enum (VAL1=0) , so rest variables will automatically get initialized. And the anwser is correct, because at a time the object will be having the value of a single variable (i.e. 2bytes)
Sanoar said:
1 decade ago
In gcc linux platform answer is 4.
i.e. Answer is C.
i.e. Answer is C.
Sundar said:
1 decade ago
Hi Guys,
If we run this program in DOS (compiled with Turbo C), it will show the output as 2. Because it is a 16-bit platform. If you compile in a 32-bit platform like Linux (compiled with GCC compiler), it will show 4 as the output.
The online compiler given in this websites is a 32 bit (Linux) platform. Show it will show the output as 4.
Hope you understand better. Have a nice day.!
If we run this program in DOS (compiled with Turbo C), it will show the output as 2. Because it is a 16-bit platform. If you compile in a 32-bit platform like Linux (compiled with GCC compiler), it will show 4 as the output.
The online compiler given in this websites is a 32 bit (Linux) platform. Show it will show the output as 4.
Hope you understand better. Have a nice day.!
Neha said:
1 decade ago
For example:
enum status{pass,fail}var;
enum var jhon=pass;
printf("%d",sizeof(jhon));
Will print 2 bcause the values in pass, fail are treated as integer by the compiler like 0, 1, 2, ... and so on.
Thats why the size is 2 under TurboC compiler and 4 in GCC compiler.
enum status{pass,fail}var;
enum var jhon=pass;
printf("%d",sizeof(jhon));
Will print 2 bcause the values in pass, fail are treated as integer by the compiler like 0, 1, 2, ... and so on.
Thats why the size is 2 under TurboC compiler and 4 in GCC compiler.
Rohit said:
1 decade ago
The reason is that value of each enum element must lie within the range of int.
Naga Mahesh said:
1 decade ago
enum always returns integer only so size is always 2 bytes
Janak said:
1 decade ago
If we had to variables had been initialised then, wat kind of o/p we would get?.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers