Internal implementation of switch-case

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

Reply to
karthikbg
Loading thread data ...

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.

Reply to
noone

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

Reply to
Michael Schnell

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.