How to Use Enum in Embedded C: Syntax and Examples
In Embedded C, use
enum to define named integer constants that improve code readability and maintainability. Declare an enum with a list of identifiers, and the compiler assigns integer values starting from zero by default. You can use these named constants in your code instead of raw numbers for clearer logic.Syntax
An enum defines a set of named integer constants. Each name represents a unique integer value starting from 0 by default, but you can assign specific values if needed.
- enum: keyword to declare an enumeration.
- Name: optional name for the enum type.
- { ... }: list of identifiers separated by commas.
- Each identifier can optionally have an assigned integer value.
c
enum Color {
RED, // 0
GREEN, // 1
BLUE // 2
};Example
This example shows how to declare an enum for days of the week and use it in a function to print the day name. It demonstrates default numbering and using enums for readable code.
c
#include <stdio.h>
enum Day {
SUNDAY, // 0
MONDAY, // 1
TUESDAY, // 2
WEDNESDAY, // 3
THURSDAY, // 4
FRIDAY, // 5
SATURDAY // 6
};
void printDay(enum Day day) {
switch(day) {
case SUNDAY: printf("Sunday\n"); break;
case MONDAY: printf("Monday\n"); break;
case TUESDAY: printf("Tuesday\n"); break;
case WEDNESDAY: printf("Wednesday\n"); break;
case THURSDAY: printf("Thursday\n"); break;
case FRIDAY: printf("Friday\n"); break;
case SATURDAY: printf("Saturday\n"); break;
default: printf("Invalid day\n");
}
}
int main() {
enum Day today = WEDNESDAY;
printDay(today);
return 0;
}Output
Wednesday
Common Pitfalls
Common mistakes when using enum in Embedded C include:
- Assuming enum values are strings instead of integers.
- Not assigning explicit values when needed, leading to unexpected numbering.
- Using enum variables without specifying the enum type, which can cause confusion.
- Forgetting that enums are just integers and can be assigned any int value, which may cause bugs.
Always use enum names for clarity and assign explicit values if the default numbering does not fit your needs.
c
/* Wrong: Using enum as string */ // printf("%s", RED); // Error: RED is int, not string /* Right: Use enum value with switch or print integer */ int color = RED; printf("Color code: %d\n", color);
Output
Color code: 0
Quick Reference
| Concept | Description |
|---|---|
| enum | Keyword to declare enumeration type |
| Identifiers | Named constants inside enum, default values start at 0 |
| Explicit values | Assign specific integer values to identifiers if needed |
| Usage | Use enum variables for readable code instead of raw integers |
| Type safety | Enums are integers; use carefully to avoid invalid values |
Key Takeaways
Use
enum to create named integer constants for clearer embedded C code.Enum values start at 0 by default but can be assigned specific integers explicitly.
Enums improve readability and reduce magic numbers in your code.
Remember enums are integers, not strings, so print or compare accordingly.
Assign explicit values in enums when default numbering does not match your logic.