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;
}
Discussion:
50 comments Page 1 of 5.
Mohana said:
6 years ago
Well explained, thanks @Neha.
(1)
Ashok said:
9 years ago
How it got 2?
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.
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).
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.
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;
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers