Internal implementation of switch-case

Dec 17, 2006 2 Replies

Hi,



What is the internals of a 'switch-case' satement ? Does it use 'if' internally or what is the logic internally ?



Could someone here tell the design of 'switch-case' statement .



Kindly share your views/ideas.



Tonnes of Thx in advans, Karthik Balaguru



write a test program and compile it to intermediate assembler code instead of making an executable. Then read the assembler code.

cc -S test.c

and understand that the output may vary depending upon the compiler used and the target processor type.

A good compiler such as gnu optimizes the implementation according to the cases in question

- either a series of compare and jump if equal (small case statements)

- or search a list, find the n'th element and use the n'th element of a jump table (big case statements with arbitrary values)

- or calculate a jump from the case variable (big case statements with a range of consecutive case values)

-Michael

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required