C Programming - Structures, Unions, Enums - Discussion

Discussion Forum : Structures, Unions, Enums - Find Output of Program (Q.No. 5)
5.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    enum days {MON=-1, TUE, WED=6, THU, FRI, SAT};
    printf("%d, %d, %d, %d, %d, %d\n", MON, TUE, WED, THU, FRI, SAT);
    return 0;
}
-1, 0, 1, 2, 3, 4
-1, 2, 6, 3, 4, 5
-1, 0, 6, 2, 3, 4
-1, 0, 6, 7, 8, 9
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
31 comments Page 1 of 4.

Keerthana said:   7 years ago
By default, enum assign 0 as the starting value and successive values will be assigned for remaining members. i.e. 0 for MON, 1 for TUE, 2 for WED and so on. But if member is pre-assigned, its successive value will be assigned for next member if it is not assigned with any value. If the next member is assigned with some value then it will be considered. So in above eg, MON=-1, TUE=-1+1=0, WED=6, THU=6+1=7, FRI=7+1=8, SAT=8+1=9.

Hence the output:-1, 0, 6, 7, 8, 9.
(10)

Ramdas said:   1 decade ago
Here the given
enum days {MON=-1, TUE, WED=6, THU, FRI, SAT};
enum has the property of incrementing integer value by 1 to every member in it.
here for first member MON they assigned -1. so for next member its value is 0.according to this for third member it should 1, but the programer assigned 6 to it. hence, for next member it becomes 7(=6+1). The compiler reads previous member value and increment it. so it is 7 and 8, 9

Karthika said:   1 decade ago
enum days={MON=-1,TUE,WED=6,THU,FRI,SAT}
so the value for monday = -1;
usually enum ill take the values sequentially, dats if enum month={a=0,b};
her the value for b=1(dats next of 1)
like wise the value for TUE=0;
den wed=6(ITS ALREADY GIVEN),therefore next value s 7 hence THU=7,DEN FRI=8,SAT=9

ANIL B RATHOD said:   2 years ago
Enums are always integral constants so we call enums as names of an integer value.

Enums will assign the values to its member automatically starting from 0 (zero). And next to increasing the variable, we have to increase the value explicitly.

Raja Sekhar said:   1 decade ago
In Enum,

Unless a value is assigned, the elements are given a default integer value which is an increment of the previous one.

So TUE=2 as the increment of the previous one(MON).

As WED is 6 the next will be THU=7, FRI=8, Sat=9

Rahul said:   1 decade ago
@Lakshmi : enum assigns value to its member's sequentially.
It will assign 0 to first member if value is not predefined.
Here MON= -1 so TUE becomes 0 WED would have become 1 but.
WED=6 is given so TUE=7 FRI=8 SAT=9.

Ashish said:   1 decade ago
Because enum takes the sequentials values by default after assigning a particular number.

When the value wed=6 initialized and others variable not initalized. It runs sequentially.

Raviraj BK said:   1 decade ago
enum variables are assigned with consective inegers.
ie if mon =-1 then tue will be assigned with o ie next vale of -1.
this is method of assignment in enum datatype.

Sudha said:   1 decade ago
I agree all the points for my friends!

Union stores in memory in same data type in same place. But continue memory allocation.

Raghav Naganathan said:   1 decade ago
@Sagarika: It will throw an error as increment and decrement operators can't be done on an enum value.

Hope this helps.


Post your comments here:

Your comments will be displayed after verification.