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.
Asmit said:
9 years ago
Example program:
#include <stdio.h>
enum week{ sunday, monday, tuesday, wednesday, thursday, friday, saturday};
int main(){
enum week today;
today=wednesday;
printf("%d day",today+1);
return 0;
}
Ans is 4 days.
#include <stdio.h>
enum week{ sunday, monday, tuesday, wednesday, thursday, friday, saturday};
int main(){
enum week today;
today=wednesday;
printf("%d day",today+1);
return 0;
}
Ans is 4 days.
Mounika said:
9 years ago
How it is 0, 2, 1? Where we are declared with 0, 1, 2. So output should be 0, 1, 2.
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.
Shankar said:
10 years ago
It's really nice thanks all.
HARIPRASATH PANNEERSELAVAM said:
9 years ago
@RamKrishnan - Thanks for your kind information.
Puja said:
9 years ago
When I run this program then it produces the output only 0, 2 not 1. Why?
Mamun Rahaman said:
9 years ago
Thanks for all your kind information.
Nithya said:
8 years ago
Here, what is meant by status?
Virendrasinh chauhan said:
8 years ago
enum status { pass, fail, atkt}; {pass=0,fail=1,atkt=2}////// here enum status is a user define data type.
enum status stud1, stud2, stud3; ////////that's the reason we require the same user define data type for storing the result.
stud1 = pass; //// 0
stud2 = atkt; //// 2
stud3 = fail; //// 1
enum status stud1, stud2, stud3; ////////that's the reason we require the same user define data type for storing the result.
stud1 = pass; //// 0
stud2 = atkt; //// 2
stud3 = fail; //// 1
TUKARAM said:
8 years ago
YES, ENUM START WITH 0.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers