enumeration data type in c and c++

Nikumbh

Nikumbh

@nikumbh-lmTCgS Oct 25, 2024
Can anyone tell about the enumeration data type in c and c++....................................... 😒
i.e., enum

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Ankita Katdare

    Ankita Katdare

    @abrakadabra Aug 10, 2011

    @Nikumbh: Sure. What do you want to know about it?
  • Nikumbh

    Nikumbh

    @nikumbh-lmTCgS Aug 10, 2011

    AbraKaDabra
    @Nikumbh: Sure. What do you want to know about it?
    I want to know what is the use of enumeration tag.
  • Nikumbh

    Nikumbh

    @nikumbh-lmTCgS Aug 10, 2011

    I want to know the use of enum data type
  • Ankita Katdare

    Ankita Katdare

    @abrakadabra Aug 10, 2011

    Very well, 'Enum' data type variables takes values which have been previously declared.
    With an Enum, you can specify a number of valid values for that variable and descriptive constant names for the values of the Enum. These values are used instead of constants.

    In C:

    For example:

    enum month 
    { 
    jan=1, feb=2, mar=3, apr=4, may=5, jun=6, jul=7, aug=8, sep=9, oct=10, nov=11, dec=12
    };     
    enum month this_month;      
    
    this_month = aug;    
    Here 'month' is declared as the enumerated data type.
    It consists of a set of values, jan to dec. Numerically, jan is given the value 1, feb the value 2, and so on.
    The variable 'this_month' is declared to be of the same type as 'month'.
  • Nikumbh

    Nikumbh

    @nikumbh-lmTCgS Aug 14, 2011

    Is that necessary to have enum data type....?
  • ayusha patnaik

    ayusha patnaik

    @ayusha-patnaik-UIGhHT Aug 15, 2011

    can any one suggest me a good and easy book for object oriented programming for c++?
  • TheV

    TheV

    @thev-iGmS6y Aug 15, 2011

    I am explain a little more..

    When we write a number of related constant with sequencial values like I want to assign names of the color(red,green,yellow,white,black,blue,orange) with 1 to 7 then instead of writing ..

    int red=1;
    int green =2;
    int yellow =3;
    ........
    ........
    int orange = 7;
    we write ....

    enum color{
    red,green,yellow,white,black,blue,orange
    };
    here we didnot initialize any color with any number. It is automatically initialize from 0 to 6. But we want to initialize from 1 to 7 so we just initialize
    the first color i.e. red to 1 and the remaining automatically initializes from 2 to 7 . As..
    enum color{
    red=1,green,yellow,white,black,blue,orange
    };
    Again explaining little more...if we initialize green to 1 ie green =1 then the remaining 5 color will have 2 to 6
    eg-

    #include<stdio.h>
    #include<conio.h>
    main()
    {
    enum color{
    red=1,green=1,yellow,white,black,blue,orange
    };
    enum color mycolor = white;
    printf("%d",mycolor);
    printf("%d%d",red,orange);  //We can also print by their name .
    getch();
    return;
    }