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.

Nidhi said:   1 decade ago
Firstly, @Pallavi is absolutely correct. Enum is an int type. Its size will be 2 bytes for a 16-bit compiler or 4 bytes for a 32-bit compiler. So, the output depends on the compiler.

Secondly, when an element of an enum is initialized to a given value, the rest of the elements are automatically assigned values by incrementing each element by 1. e.g. Here VAL1=0, so the rest are assigned as follow: VAL2=1, VAL3=2, VAL3=4, VAL5=4.

Sachin kakatkar said:   1 decade ago
Size of enum variable is equal the size of integer for 16-bit dos it is 2.

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;

U Naveena Reddy. said:   1 decade ago
#include<stdio.h>

int main()
{
enum value{VAL1='a', VAL2, VAL3, VAL4, VAL5} var;
printf("%d\n", sizeof(var));
return 0;
}

Why the output for the above program 4 instead of 1 byte because I am thinking for giving 'a' character the value should be 1 byte.

Chinmay said:   1 decade ago
enum values are basically integer values with restriction imposed on the range of values it can take. Since they are integer values so they shall be having size same as that of an integer.

Devendra gupta said:   1 decade ago
enum value{VAL1=0, VAL2, VAL3, VAL4, VAL5} var;

The enum variable var occupies number of bytes required by the maximum numerical value of enum identifier. Here since the maximum value is 4 which is of VAL5 number of bytes required will be one.

Keerthi said:   1 decade ago
In windows enum occupies 2 where as in linum enum occupies 4.

Ram Krishan Sharma said:   10 years ago
Hello friends.

enum is integer constant so we it to integer or character. If we give it to character then it take ASCII value and printing of this variable is dependent on the %c or %d what we take.

Now things come on that why it is printing 2 or 4. So this is compiler dependent. It is a GCC or turbo C compiler.

Ashok said:   9 years ago
How it got 2?

Raji said:   9 years ago
The enum is actually user defined data type consisting of integer constants.


Post your comments here:

Your comments will be displayed after verification.