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 5 of 5.

Satyaprakash said:   1 decade ago
Thanks to rohit.

Pallavi said:   1 decade ago
In C enum is internally treated as Integer.Hence size of enum will be size of an integer (ie)2 bytes(visual C or turbo C) or 4 bytes(GCC compiler Linux) depending upon the compiler.

Kiru said:   1 decade ago
Enum is keyword to define named integer constants in 16-bit compiler. The integer value take 2bytes to store the value and for GCC 4 bytes to store it. The variable can store only integer value you can answer. And if you want to initialize, initialize at part of declaration at any cost.

Ramakant said:   1 decade ago
@Pallavi

But there are 5 integer (VAL1=0, VAL2, VAL3, VAL4, VAL5)
So sizeof(var) will be 10 byte........
Please explain me?

Girija said:   1 decade ago
@ Ramakant
there is no issue of how many variable/elements are in enum.
so that Pallavi is right.
thanks Pallavi

Yakesh said:   1 decade ago
"sizeof () " operator returns the actual size of a data type.
For ex: sizeof (float) ; returns 4 bytes.
Here enum treated as integer data type hence returns 2bytes.

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

Sowjanya said:   1 decade ago
The value assigned to the 1st variable is 0 so it's integer, enum also continues its values as integers so in 16 bit compiler integer value is 2.

Cherry said:   1 decade ago
Hi guys. Initial value i.e. val1=0, remaining enumeration constants by default incremented by 1 then. At a time it (var) contains only one enumeration constant. So that answer is 2 (in 16-bit platform).

I hope you understand.

Pretti said:   1 decade ago
After enum(...) var-why we are writing it here???
whats its actual meaning is??
what it signifies??


Post your comments here:

Your comments will be displayed after verification.