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 2 of 6.
Rajashekar said:
9 years ago
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.
stud1 = pass (value is 0)
stud2 = atkt (value is 2)
stud3 = fail (value is 1)
Hence it prints 0, 2, 1.
Ratna Priya said:
8 years ago
Here we are also taken as pass=1, fail=2,atkt=3.
Stud1=pass;///1
Stud2=atkt;///3
Stud3=fail;///2.
Then option D is also correct.
Can anyone explain this to me?
Stud1=pass;///1
Stud2=atkt;///3
Stud3=fail;///2.
Then option D is also correct.
Can anyone explain this to me?
(2)
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.
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.
Nagesh said:
1 decade ago
Explanation given by Ram krushna is correct and by the way jeevan enum's syntax is like this that's why it start from 0
Bhavani said:
6 years ago
If we mentioned as enum starts with 1, it will starts with 1 only. Otherwise, automatically it will starts with 0.
Asaduzzzaman kanok said:
1 decade ago
If enum takes the format like {1, 2, 3....}.
So pass =1, fail = 2, atkt = 3.
I think then output will be (123).
So pass =1, fail = 2, atkt = 3.
I think then output will be (123).
Karthi said:
9 years ago
What is the use of enum status stud1,stud2,stud3; line as Vinod asked? Please explain.
Swetha said:
1 decade ago
Answer may be option D also. Why it is option C only? Can you explain in depth please.
Sindhu said:
8 years ago
Enum like array by default starts with 0, So according to that 0,2,1 is the answer.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers