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

Raju said:   9 years ago
Internally the compiler treats enums as integers. So for a 16-bit platform the size of enum is 2 bytes and for 32-bit platform it occupies 4 bytes.

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.

Uday said:   1 decade ago
In case of enum it doesn't depends on how many values are present. Simply gives what return type present in that. Gives the sizeof (return type).

Karam said:   1 decade ago
@Karam.

Means it take only 2 byte for VAL1=0, and if we use the next variable then it use previous 2 byte for next variable;

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?

Krishna said:   9 years ago
@Neha.

There is an error in your program. You used var variable as a data type in that program to assign value in jhon.

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

Rohit said:   1 decade ago
The reason is that value of each enum element must lie within the range of int.

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

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.


Post your comments here:

Your comments will be displayed after verification.