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.
Sbgore said:
2 years ago
The answer is 2 because it performs operations based 16-bit compiler.
It is 4 when it should be 32-bit compiler. An enum variable typically occupies the same amount of memory as an int type.
It is 4 when it should be 32-bit compiler. An enum variable typically occupies the same amount of memory as an int type.
Vijay said:
3 years ago
enum returns an integer value.
So its size is int size only.
In 16 bit computers or os => int = 2bytes. <= present problem in 16 bit platform so,output is 2 bytes.
in 32 & 64 bit computer => int = 4bytes.
So its size is int size only.
In 16 bit computers or os => int = 2bytes. <= present problem in 16 bit platform so,output is 2 bytes.
in 32 & 64 bit computer => int = 4bytes.
Manisha said:
5 years ago
No, the right Answer should be 4.
Mohana said:
6 years ago
Well explained, thanks @Neha.
(1)
Keerthana said:
7 years ago
Enum is an integer by default. So size will be 2bytes.
Hence the output:2.
Hence the output:2.
Sohail said:
7 years ago
Sizeof enumeration is always sizeof int. In 16-bit compiler (ie. Turbo c/c++) it will be 2 bytes and in 32-bit compiler (ie. gcc)it will be 4 byte.
Vikash said:
8 years ago
Answer will be 4 in a 32-bit compiler as the sizeof function will return the value if int.
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?
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.
There is an error in your program. You used var variable as a data type in that program to assign value in jhon.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers