Enum and typedef in C: solved exercise with states

Enum and typedef in C: solved exercise with states

This exercise is scheduled for daily publication and follows the same didactic structure used across the site: clear statement, compilable code, and expected output.

Problem statement

Implement a practical example of the topic and validate the output in the console.

C solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>

typedef enum {
    ESTADO_PENDIENTE = 0,
    ESTADO_EN_PROGRESO = 1,
    ESTADO_HECHO = 2
} EstadoTarea;

const char *estado_a_texto(EstadoTarea e) {
    switch (e) {
        case ESTADO_PENDIENTE: return "Pendiente";
        case ESTADO_EN_PROGRESO: return "En progreso";
        case ESTADO_HECHO: return "Hecho";
        default: return "Desconocido";
    }
}

int main(void) {
    EstadoTarea estado = ESTADO_EN_PROGRESO;
    printf("Estado actual: %s\n", estado_a_texto(estado));
    return 0;
}

Expected output

1
Estado actual: En progreso

Common mistakes

  • Not validating input and standard-library return values.
  • Ignoring edge cases (buffers, limits, null pointers).
  • Skipping basic compile/run verification.

Practical use

Enums and typedef improve code readability and are widely used in state machines, menu options, and custom types.

Guided practice and full book

If you want a complete path with progressive difficulty:

FAQ

Is this exercise useful for C exams and technical interviews?

Yes. It targets patterns that commonly appear in practice assignments, technical interviews, and C programming exams.

Where can I keep practicing with more solved C exercises?

In Programming in C in 100 Solved Exercises and C Exercises. Kindle Unlimited: View on Amazon.

How should I practice this exercise type to improve faster?

Start with small inputs, run edge cases (empty, one item, max capacity), then rewrite the solution from scratch without copying.