C Tutorial

C Switch

switch picks a case from an integer or enum. Remember break, or execution falls through.

One value, several labels

A switch compares an integer (or an enum) against a list of case labels. When a label matches, execution jumps there. It is a menu: the program looks at one number and picks a branch.

You can switch on int, char, and enum. You cannot switch on adouble or on a string. For text, use if / else if. For ranges such asscore >= 80, use if as well. Switch wants exact matches.

A complete program

Each case ends with break; so the program leaves the switch after that branch.default runs when no label matches.

Example

#include <stdio.h>

int main(void) {
  int day = 3;
  switch (day) {
    case 1:
      printf("Monday\n");
      break;
    case 2:
      printf("Tuesday\n");
      break;
    case 3:
      printf("Wednesday\n");
      break;
    default:
      printf("another day\n");
      break;
  }
  return 0;
}

Change day in /c/try. Try 1, 3, and 9 to see a matching case anddefault.

case, default, break

  • case n: is a label. n must be a constant integer expression.
  • default: is the fallback. Put it last. You can omit it if every value is covered.
  • break; jumps out of the switch. Without it, execution continues into the next case.

The parentheses after switch hold the value you test. The braces after that hold every case. Case labels themselves do not use extra parentheses.

Fall-through

If you forget break, C does not stop at the end of that case. It keeps running the statements in the following cases until it hits a break or the closing brace. That is called fall-through. It is almost always a bug.

Example

#include <stdio.h>

int main(void) {
  int day = 1;
  switch (day) {
    case 1:
      printf("Mon\n");
    case 2:
      printf("Tue\n");
      break;
    default:
      printf("other\n");
      break;
  }
  return 0;
}

This program prints both Mon and Tue because case 1 has no break. Add a break after every case unless you grouped empty labels on purpose.

Sharing one body

Stacking labels without statements between them is the one common, intentional fall-through. Several values run the same block. Put a comment on the empty cases so the next reader knows it is deliberate.

Example

#include <stdio.h>

int main(void) {
  int day = 6;
  switch (day) {
    case 6:
    case 7:
      printf("weekend\n");
      break;
    default:
      printf("weekday\n");
      break;
  }
  return 0;
}

switch versus if

switchif / else if
Best forExact integer or enum valuesRanges and combinations
Strings and doublesNoYes
Easy missForgotten breakWrong order of tests

Enums belong with switch. You will meet enum later; the shape is the same: one value, named cases, a break on each branch.

Next: while — repeating a block while a condition stays true.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the C editor at /c/try. Change one measurement and check whether the result still has the right unit.

Chemistry

Solid, liquid, gas

Water’s common states at 1 atm are ice, liquid, and steam. A coded sample type is an integer because switch in C wants an integer (or enum). 1, 2, 3 are arbitrary; the names in the comments are not.

Forget break and execution falls into the next case. That is a bug here, not a feature.

Example

#include <stdio.h>

int main(void) {
  int state = 2; /* 1 solid, 2 liquid, 3 gas */
  switch (state) {
    case 1:
      printf("solid — ice\n");
      break;
    case 2:
      printf("liquid — water\n");
      break;
    case 3:
      printf("gas — steam\n");
      break;
    default:
      printf("unknown\n");
  }
  return 0;
}

Astronomy

Planet index

Old catalogues numbered planets from the Sun: 3 Earth, 4 Mars, 5 Jupiter. Pluto is not in this switch, which is historically accurate for a 2006-and-after list and also a reminder that default exists.

switch maps a small integer onto a name. If the number comes from a file, always keep default.

Example

#include <stdio.h>

int main(void) {
  int n = 4;
  switch (n) {
    case 3:
      printf("Earth\n");
      break;
    case 4:
      printf("Mars\n");
      break;
    case 5:
      printf("Jupiter\n");
      break;
    default:
      printf("not in this list\n");
  }
  return 0;
}

FAQ: C Switch

Common questions about this page.

What is the StudyGrid C tutorial?

The StudyGrid C tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, pointers, structs, files, and the standard library. Each chapter has copy-and-run examples.

Should I run c switch examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c switch in this C C lesson (C Switch).

Is the C editor the same as Try Python, Try HTML, or Try C++?

No. Try C compiles with gcc at /c/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. Try C++ stays at /cpp/try. C lessons never open those editors.

Do I need to install a compiler to learn C?

No. Open a chapter, click Try it in C, and compile in the browser. You can also download a .c file and compile locally with gcc.

Where should I start the C tutorial?

Start at C Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After pointers, open C Examples, then files and bitwise. Use Next at the bottom of each chapter.

Is the C tutorial free?

Yes. The C workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.