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;
}
0, 1, 2
1, 2, 3
0, 2, 1
1, 3, 2
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.

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
(1)

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.
(2)

JITIN JINDAL said:   1 decade ago
It is not necessary that enum types will always start from 0 and so on we can change it as enum xyz{A=2,B,C,Y=9,Z,J,K=11,M};

Here as A is declared with 2 so now B has index 3 n C has index 4 now see we have given Y with an index of 9 so Z and J will be indexed as 10 n 11 respectively and K will also share the same index as 11 and has index 12.

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

Naveen goud said:   1 decade ago
@purnima
you are absolutely correct.

@Tejas

enumerated data type is a user defined data type.we can define any kind of variables,functions and enum assigns integer numbers to those variables that we declared in sequential manner like 0,1,2,3.........etc

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.

Ram krishna said:   1 decade ago
Here enum is enumerated data type.

It will give sequential no's to variables declared in braces in program pass=0, fail=1, atkt=2.

Hence they are assigned to std1, std2, std3.

See the sequence in printf statement then determine the answer.

Akram Regard said:   1 decade ago
We know that enum start with 0, 1, 2.

Here we are seeing that at enum's 0th position we have pass so it assign value 0 and atkt at number 2th so we are getting 2 after 0 and last we have fail is 1th position so we got 1 as last.

Piyush said:   8 years ago
Can you help me?

Display a number which last and first two digits are divided with 11 and that number should be the square of a number?

Generate the output of this program and program also.

Swapnil said:   1 decade ago
@Sonu

first of all you need to observe program correctly because in program the variable atkt is used first which has value 2 assign already that's way output sequence is changed.


Post your comments here:

Your comments will be displayed after verification.