Master C Enums and Unions: Practical Guide with Code Examples
This article explains C enumeration and union types, covering their definitions, common use cases, declaration syntax, variable creation, usage in switch statements and loops, and demonstrates how to access and correctly use union members with clear code examples.
Enum Overview
Enumeration (enum) is a data type that lists all possible values, typically used to represent a set of integer constants.
Common use cases include replacing magic constants, limiting variable ranges, implementing state machines, and defining menu options.
Declaring an Enum
Use the enum keyword followed by the type name and members. If no explicit values are given, they start at 0 and increment.
enum DAY {
MON = 1, TUE, WED, THU, FRI, SAT, SUN
};Explicit values can be assigned to any member; subsequent members continue incrementing from the last value.
enum season {
spring, summer=3, autumn, winter
};Defining Enum Variables
Three forms are possible:
Declare the enum type first, then define a variable.
Declare and define the variable in the same statement.
Omit the enum name when defining the variable.
Enum in Switch Statements
#include <stdio.h>
#include <stdlib.h>
int main(){
enum COLOR{
red = 1,
green,
blue,
};
enum COLOR favorite_color;
printf("Input your favorite color:");
scanf("%u", &favorite_color);
switch(favorite_color){
case red: printf("red."); break;
case green: printf("green."); break;
case blue: printf("blue."); break;
default: printf("None.");
}
return 0;
}Iterating Over Enum Values
Since enums are treated as integers, they can be looped only when values are sequential.
#include <stdio.h>
enum DAY{
MON=1, TUE, WED, THU, FRI, SAT, SUN // sequential
};
int main(){
for(int i=MON; i<=SUN; i++){
printf("Day is %d
", i);
}
return 0;
}Union Overview
A union allows different data types to share the same memory location; only one member is valid at a time.
Declaring a Union
union Data {
int i;
float f;
char str[20];
};Memory occupied equals the size of the largest member.
#include <stdio.h>
#include <string.h>
union Data{
int i;
float f;
char str[20];
};
int main(){
union Data data;
printf("Memory size occupied by data: %lu
", sizeof(data));
return 0;
}Accessing Union Members
#include <stdio.h>
#include <string.h>
union Data{
int i;
float f;
char str[20];
};
int main(){
union Data data;
data.i = 10;
printf("data.i : %d
", data.i);
data.f = 220.5;
printf("data.f : %f
", data.f);
strcpy(data.str, "C Programming");
printf("data.str : %s
", data.str);
return 0;
}Only the last assigned member holds a valid value; earlier values are overwritten.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
