C Programming - Declarations and Initializations - Discussion
Discussion Forum : Declarations and Initializations - Find Output of Program (Q.No. 1)
1.
What is the output of the program given below ?
#include<stdio.h>
int main()
{
enum status { pass, fail, atkt};
enum status stud1, stud2, stud3;
stud1 = pass;
stud2 = atkt;
stud3 = fail;
printf("%d, %d, %d\n", stud1, stud2, stud3);
return 0;
}
Answer: Option
Explanation:
enum takes the format like {0,1,2..) so pass=0, fail=1, atkt=2
stud1 = pass (value is 0)
stud2 = atkt (value is 2)
stud3 = fail (value is 1)
Hence it prints 0, 2, 1
Discussion:
57 comments Page 5 of 6.
Ashu said:
1 decade ago
What is the need of this datatype ?
Shafi said:
1 decade ago
What is the need of stud1,stud2, stud3 declared as enum type ?
declaration of stud1,stud2, stud3 as int type is Ok. it will made to confuse.
declaration of stud1,stud2, stud3 as int type is Ok. it will made to confuse.
JITIN JINDAL said:
1 decade ago
It is not necessary that enum types will always start from 0 and so on we can change it as enum xyz{A=2,B,C,Y=9,Z,J,K=11,M};
Here as A is declared with 2 so now B has index 3 n C has index 4 now see we have given Y with an index of 9 so Z and J will be indexed as 10 n 11 respectively and K will also share the same index as 11 and has index 12.
Here as A is declared with 2 so now B has index 3 n C has index 4 now see we have given Y with an index of 9 so Z and J will be indexed as 10 n 11 respectively and K will also share the same index as 11 and has index 12.
Subhabrata said:
1 decade ago
What is "status" here for? can anyone tell me?
Pinky said:
1 decade ago
Thanks Rama Krishna nice explanation
Swapnil said:
1 decade ago
@Sonu
first of all you need to observe program correctly because in program the variable atkt is used first which has value 2 assign already that's way output sequence is changed.
first of all you need to observe program correctly because in program the variable atkt is used first which has value 2 assign already that's way output sequence is changed.
Sonu said:
1 decade ago
But while declaring the sequence is ok but in the output why the sequence change ?
Naveen goud said:
1 decade ago
@purnima
you are absolutely correct.
@Tejas
enumerated data type is a user defined data type.we can define any kind of variables,functions and enum assigns integer numbers to those variables that we declared in sequential manner like 0,1,2,3.........etc
you are absolutely correct.
@Tejas
enumerated data type is a user defined data type.we can define any kind of variables,functions and enum assigns integer numbers to those variables that we declared in sequential manner like 0,1,2,3.........etc
Tejas said:
1 decade ago
Please anyone explain me about enumerated data type.
Purnima said:
1 decade ago
YES as we know that the array begins with a[0], like this only the in enum it is initialized to 0 first.
Am I correct? Please say if anything wrong.
Am I correct? Please say if anything wrong.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers