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;
}
1
2
4
10
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
50 comments Page 4 of 5.

Sanoar said:   1 decade ago
In gcc linux platform answer is 4.

i.e. Answer is C.

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.

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?.

Sundar said:   1 decade ago
@Janak

It will display a junk/garbage value (the value stored by other programs previously in that memory location reserved for the variable in the current program).

Kunal said:   1 decade ago
In borland c++ compiler ans is also 4...i run the program...

Lovely said:   1 decade ago
Thanks sundar. Thank you very much.

Prabhakar said:   2 decades ago
Value inside enum is not properly arranged then how the answer came 2. It is my doubt only.

Sridhar said:   1 decade ago
As it is an int type it gives 2 as answer.


Post your comments here:

Your comments will be displayed after verification.