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.

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

Priya said:   1 decade ago
An enumeration consists of a set of named integer constants.

In ANSI C, the expressions that define the value of an enumerator constant always have int type; thus, the storage associated with an enumeration variable is the storage required for a single int value. An enumeration constant or a value of enumerated type can be used anywhere the C language permits an integer expression.

Priya said:   1 decade ago
An enumeration consists of a set of named integer constants.

In ANSI C, the expressions that define the value of an enumerator constant always have int type; thus, the storage associated with an enumeration variable is the storage required for a single int value. An enumeration constant or a value of enumerated type can be used anywhere the C language permits an integer expression.

Rohini said:   1 decade ago
enum returns integer only so size is always 2 bytes.

Doyel said:   1 decade ago
In enum by default return type is int so it may be 2 or 4 byte based on compiler.

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


Post your comments here:

Your comments will be displayed after verification.