2

How is a switch-case statement compiled/implemented in C.

Is it a linear lookup? Is it a hash table? Some other option?

If the specific compiler changes the answer, assume recent gcc version with no build flags.

In my situation the data is more that 50 sequential enums, so dense data.

4
  • 6
    Look at the compiled code. It probably depends on whether the case values are dense or sparse. Commented Mar 23, 2017 at 20:19
  • It's unlikely to be a hash table. For sparse values it's probably linear if/else, for dense values it's probably an indexed goto. Commented Mar 23, 2017 at 20:20
  • What@Barmar says.Plus: gcc sometimes generates a tree-like decision chain. Commented Mar 23, 2017 at 20:25
  • @FredLarson thanks for finding that. Commented Mar 23, 2017 at 20:26

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.