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.