Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
201 views
in C Programming by (106k points)
Can you assign values to enum constants?

Please log in or register to answer this question.

1 Answer

0 votes
by (106k points)

Yes, you can assign values to enum constants in C. By default, if you don't explicitly assign values to the enum constants, they will be assigned consecutive integer values starting from 0. However, you have the flexibility to assign specific values to enum constants if desired.

Here's an example that demonstrates assigning values to enum constants:

#include <stdio.h>

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

int main() {
    enum Month currentMonth = MAR;

    printf("Current month: %d\n", currentMonth);

    return 0;
}
 

In this example, we define an enumeration called Month, which represents the months of the year. We explicitly assign values to each enum constant, starting from 1 for JAN and going up to 12 for DEC.

Inside the main() function, we declare a variable currentMonth of type enum Month and assign the value MAR to it. When we print the value of currentMonth, it will output 3, which corresponds to the assigned value for MAR.

By assigning specific values to enum constants, you can have more control over the underlying representation of the enum constants and use meaningful values that align with your application's requirements.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
2 answers
asked May 26, 2023 in C Programming by kvdevika (106k points)

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...