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.
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.
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?
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?
Sundar said:
1 decade ago
Hi Guys,
If we run this program in DOS (compiled with Turbo C), it will show the output as 2. Because it is a 16-bit platform. If you compile in a 32-bit platform like Linux (compiled with GCC compiler), it will show 4 as the output.
The online compiler given in this websites is a 32 bit (Linux) platform. Show it will show the output as 4.
Hope you understand better. Have a nice day.!
If we run this program in DOS (compiled with Turbo C), it will show the output as 2. Because it is a 16-bit platform. If you compile in a 32-bit platform like Linux (compiled with GCC compiler), it will show 4 as the output.
The online compiler given in this websites is a 32 bit (Linux) platform. Show it will show the output as 4.
Hope you understand better. Have a nice day.!
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.
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.
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.
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.
Neha said:
1 decade ago
For example:
enum status{pass,fail}var;
enum var jhon=pass;
printf("%d",sizeof(jhon));
Will print 2 bcause the values in pass, fail are treated as integer by the compiler like 0, 1, 2, ... and so on.
Thats why the size is 2 under TurboC compiler and 4 in GCC compiler.
enum status{pass,fail}var;
enum var jhon=pass;
printf("%d",sizeof(jhon));
Will print 2 bcause the values in pass, fail are treated as integer by the compiler like 0, 1, 2, ... and so on.
Thats why the size is 2 under TurboC compiler and 4 in GCC compiler.
Kiru said:
1 decade ago
Enum is keyword to define named integer constants in 16-bit compiler. The integer value take 2bytes to store the value and for GCC 4 bytes to store it. The variable can store only integer value you can answer. And if you want to initialize, initialize at part of declaration at any cost.
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.
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers