C Programming - Structures, Unions, Enums - Discussion
Discussion Forum : Structures, Unions, Enums - Find Output of Program (Q.No. 6)
6.
What will be the output of the program ?
#include<stdio.h>
int main()
{
enum status {pass, fail, absent};
enum status stud1, stud2, stud3;
stud1 = pass;
stud2 = absent;
stud3 = fail;
printf("%d %d %d\n", stud1, stud2, stud3);
return 0;
}
Discussion:
24 comments Page 1 of 3.
Xyz said:
1 decade ago
Hi Priya,
look at to the program carefully:
notice in line no. 3(where enum has declared) the order of declaration is pass=0,fail=1,then absent=2. So the order of declaration of enum is important,after that wherever we will use these variable the order will remain same it doen't matter in which order we have written it.
int main()
{
enum status {pass, fail, absent};
enum status stud1, stud2, stud3;
stud1 = pass;
stud2 = absent;
stud3 = fail;
printf("%d %d %d\n", stud1, stud2, stud3);
return 0;
}
look at to the program carefully:
notice in line no. 3(where enum has declared) the order of declaration is pass=0,fail=1,then absent=2. So the order of declaration of enum is important,after that wherever we will use these variable the order will remain same it doen't matter in which order we have written it.
int main()
{
enum status {pass, fail, absent};
enum status stud1, stud2, stud3;
stud1 = pass;
stud2 = absent;
stud3 = fail;
printf("%d %d %d\n", stud1, stud2, stud3);
return 0;
}
Sundar said:
2 decades ago
Hi Ranjith,
Kindly read the program very carefully.
The order of values assigned are
stud1 = pass; // here pass = 0
stud2 = absent; // here absent = 2
stud3 = fail; // here fail = 1
So,
printf("%d %d %d\n", stud1, stud2, stud3);
produces the output as 0, 2, 1.
Hope you understand. Have a nice day!
Kindly read the program very carefully.
The order of values assigned are
stud1 = pass; // here pass = 0
stud2 = absent; // here absent = 2
stud3 = fail; // here fail = 1
So,
printf("%d %d %d\n", stud1, stud2, stud3);
produces the output as 0, 2, 1.
Hope you understand. Have a nice day!
TANU KHANDELWAL said:
1 decade ago
#include<stdio.h>
int main()
{
enum status {pass, fail, absent};
enum status stud1, stud2, stud3;
stud1 = pass;
stud2 = absent;
stud3 = fail;
printf("%d %d %d\n", stud1, stud2, stud3);
return 0;
}
Actually we initialize that pass, fail, absent in the 0, 1, 2 order so the answer will be in 0, 2 1 as they asked.
int main()
{
enum status {pass, fail, absent};
enum status stud1, stud2, stud3;
stud1 = pass;
stud2 = absent;
stud3 = fail;
printf("%d %d %d\n", stud1, stud2, stud3);
return 0;
}
Actually we initialize that pass, fail, absent in the 0, 1, 2 order so the answer will be in 0, 2 1 as they asked.
Raju Naidu said:
1 decade ago
Hai, If you are declare any variables in enum the default initalization is started from 0 and sequentially they are increasing. So they didn't give any initaliztion in our programm that's why bydefault initaliztion will be takes place.
So pass=0, fail=1, abscent=2.
So pass=0, fail=1, abscent=2.
Manu said:
1 decade ago
#include<stdio.h>
int main()
{
enum status {pass, fail, absent};
enum status stud1, stud2, stud3;
printf("%d %d %d %d \n", absent,stud1,stud2, stud3);
return 0;
}
Output: 2 51 51 8
Please explain why stud1, stud2 = 51 stud3 =8;
int main()
{
enum status {pass, fail, absent};
enum status stud1, stud2, stud3;
printf("%d %d %d %d \n", absent,stud1,stud2, stud3);
return 0;
}
Output: 2 51 51 8
Please explain why stud1, stud2 = 51 stud3 =8;
Prasad Deokar said:
1 decade ago
i = 0 1 2.
enum status {pass, fail, absent};
enum status stud1, stud2, stud3;
stud1 = pass; ->0
stud2 = absent; ->2
stud3 = fail; ->1
So 0 2 1
printf("%d %d %d\n", stud1, stud2, stud3);
enum status {pass, fail, absent};
enum status stud1, stud2, stud3;
stud1 = pass; ->0
stud2 = absent; ->2
stud3 = fail; ->1
So 0 2 1
printf("%d %d %d\n", stud1, stud2, stud3);
Keerthana said:
7 years ago
Since enum members are not pre-assigned,they will be assigned with 0,1,2 by default. So as a result pass=0,fail=1,absent=2
stud1=pass=0,
stud2=absent=2,
stud3=fail=1
Hence the output:0 2 1.
stud1=pass=0,
stud2=absent=2,
stud3=fail=1
Hence the output:0 2 1.
Sharad said:
9 years ago
@Chandan.
This is not a garbage value.
By default enum variable is an integer so, it may return 8-bit garbage value.
Please, can anybody explain Monu's program in more detail.
This is not a garbage value.
By default enum variable is an integer so, it may return 8-bit garbage value.
Please, can anybody explain Monu's program in more detail.
Shrishail chanaveer said:
1 decade ago
Hi friends look at enum status first line (pass (0), fail (1), absent (2)).
This is the reason it is displaying 0 2 1 as output, hope I am clear.
This is the reason it is displaying 0 2 1 as output, hope I am clear.
Priya said:
1 decade ago
How it assigning the value 0 for pass 2 for absent and 1 for fail. Wats the logic behind this? Please tell me in detail.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers