WeCodeAcademy

C Enums and typedef

An enum names a set of integer constants. typedef gives a type a shorter name.

ExampleC
#include <stdio.h>

enum Level { LOW, MEDIUM = 5, HIGH };

typedef struct {
  char name[20];
  int age;
} Person;

int main(void) {
  enum Level l = HIGH;
  printf("%d %d %d\n", LOW, MEDIUM, l);

  Person ada = {"Ada", 36};
  printf("%s is %d\n", ada.name, ada.age);
  return 0;
}
Output
0 5 6
Ada is 36

Enum values count up from 0 unless you set one: HIGH follows MEDIUM = 5, so it is 6.

Quick check

What is typedef used for?

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.