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 1 of 6.
Sushmita said:
4 years ago
What is the meaning of atkt ?
Please explain to me.
Please explain to me.
(4)
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)
Archana said:
7 years ago
@Subrato.
In enum data type, by default it gives the value 0 to the first variable ,1 to the second variable and so on.
So, here we have values, pass=0,fail=1 and atkt=2.
By assigning these values to stud1 ,stud2 and stud3 we get;
Stud1= pass (which has value 0).
Stud2=atkt (which has value 2),
Stud3=fail(which has value 1),
By printing the variables we get 021.
In enum data type, by default it gives the value 0 to the first variable ,1 to the second variable and so on.
So, here we have values, pass=0,fail=1 and atkt=2.
By assigning these values to stud1 ,stud2 and stud3 we get;
Stud1= pass (which has value 0).
Stud2=atkt (which has value 2),
Stud3=fail(which has value 1),
By printing the variables we get 021.
(2)
Ameya said:
2 years ago
Atkt ----> Allowed to keep terms.
(1)
Risha said:
6 years ago
Here status is a variable name as like arr in the given example:
Int arr[10,20,30]
And in enum status{pass,fail,atkt} ,value assign to staus is in the form of array so index starts from 0,1,2,......
While in the statement:-
enum status stud1,stud2,stud3
Here stud1,stud2,stud3 all are as value which is assign to the variable name status, but these are not in the form of array, we can understand these as like
enum status stud1
enum status stud2
enum status stud3
Now when we access the value of staus of array that are pass, fail, atkt
then we assign these as like stud1=pass, stud2=atkt,stud3=fail
After executing this program output will be as
0,2,1
Int arr[10,20,30]
And in enum status{pass,fail,atkt} ,value assign to staus is in the form of array so index starts from 0,1,2,......
While in the statement:-
enum status stud1,stud2,stud3
Here stud1,stud2,stud3 all are as value which is assign to the variable name status, but these are not in the form of array, we can understand these as like
enum status stud1
enum status stud2
enum status stud3
Now when we access the value of staus of array that are pass, fail, atkt
then we assign these as like stud1=pass, stud2=atkt,stud3=fail
After executing this program output will be as
0,2,1
(1)
Akhil said:
10 years ago
Can anyone say where we use enum?
Vinod said:
9 years ago
enum status stud1, stud2, stud3;
Can anyone explain this declaration statement?
Can anyone explain this declaration statement?
Karthi said:
9 years ago
What is the use of enum status stud1,stud2,stud3; line as Vinod asked? Please explain.
Prema said:
9 years ago
If enum takes value staring from 1 then the output will be D.
Kumar shanu said:
9 years ago
How it can possible?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers