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;
}
0, 1, 2
1, 2, 3
0, 2, 1
1, 3, 2
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
24 comments Page 2 of 3.

Ghufran said:   1 decade ago
Simply enum start index from 0 by default if no any value is associated.

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);

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;

Amit said:   1 decade ago
@Manu. It is compiler dependent.

Nagaraju said:   1 decade ago
How can you say pass0, pass2, pass1 @Sundar.

Manju said:   1 decade ago
How is possible? Please explain this program.

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.

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.

Chandan said:   10 years ago
@Manu.

Those are garbage value as you have not assigned any value to those variables.

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.


Post your comments here:

Your comments will be displayed after verification.