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

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.

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.

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

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.

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.

Soumya S H said:   8 years ago
#include<stdio.h>

int main()
{
enum value{VAL1=0, VAL2, VAL3, VAL4, VAL5} var;
printf("%d\n", sizeof(var));
//printf("%d\n", sizeof(value)); wen i added this line gave me error as undeclared value..
return 0;
}
Answer is 4 in gcc compiler, it's right that enum is interger type.
but can anyone help in this commented line error?
Is it because of assigned values to it?

Vikash said:   8 years ago
Answer will be 4 in a 32-bit compiler as the sizeof function will return the value if int.


Post your comments here:

Your comments will be displayed after verification.