1

Something about switch-case statement is very unclear for me. First of all I could understand the case part as an if statement, but what dose switch do itself? I mean when we pass argument to switch, what happens there?

c=getchar();
switch(c)

2: Why there is no need to put ; after switch? What kind of the function is it?
3: How compilers implement switch-case statement at the first time? (just C or use assembly)

4: We can't use switch-case like this:

switch(string)
{
    case "aaaa":
        ...
    case "bbbb":
        ...

we use if-strcmp instead. Is it possible to create switch-case like statement for string condition? I see some library like getopt use struct to handle command line string arguments:

{"help",...,'h'},
{"version",...,'v'}
...

but I want to use string directly.

Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
hrlinc
  • 33
  • 5

4 Answers4

3

first of all I could understand "case" part as a "if" statement, but what dose switch do itself? I mean when we pass argument to switch, what happens there?

In case, there's always a constant. You need to "compare" something with this constant. And switch(a) means - "compare a with:" and it "compares" it to each case statement.


2: why there is no need to put ";" after switch? what kind of the function is it?

It's not a function. You're not calling switch function, you're just starting to write a switch statement.


3: how compilers implement switch-case statement at the first time? (just C or use assembly)

That's platform specific. Sometimes, it's implemented using jumps (asm) and offsets.


4: we can't use switch-case like this:

Because switch works only with integral types (that is int, enum, char, etc. and not double, strings, etc.)

Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
  • If we aren't calling it so how does it work? could we make something like it or we just create functions? – hrlinc Mar 18 '14 at 14:52
  • @user3433566 - it's just syntax, a statement, a keyword. Just like `if`, `return`, `for`, etc. What do you mean by "how does it work" - byte level, assembly, ..? Yes, you can use `if-else`, instead of `switch`. I don't understand the last part of your question - what functions? – Kiril Kirov Mar 18 '14 at 14:55
  • @ Kiril Kirov thanks to reply. I mean we could use goto and a jump-table to simulate a switch case but I'm not sure it be like a statement. Is that possible to create a new statement in c or not? or my question orginaly is wrong? – hrlinc Mar 18 '14 at 15:06
  • You can write your own function, that does something like this, use `jump`s, or whatever you like, but this will now be a _function_. You cannot create new keywords in `C`. Keywords are part of the language's _syntax_ and you cannot change it. Defining functions is a different thing. Not sure if this helps, but I can't find a better way to explain what i mean. – Kiril Kirov Mar 18 '14 at 15:08
0

The switch statement begins the scope of the cases. Scopes in C/C++ are always bound by curly braces ( {} ), specifying the scope of local variables (on the stack) which is used to hold the switch parameter. It hence doesn't need a ; since it is grammatically much like a while loop.

Compilers implement switchs as direct jump in assembly. This is what makes them really more efficient than ifs (which are mostly compiled as different comparisons in assembly). This is also the reason why you can't use a switch on text. The value of a char* is only a pointer, hence cannot be used to correctly identify the target section to execute.

Soravux
  • 9,653
  • 2
  • 27
  • 25
0

To use strings in switch case() in c what you can do is, to store the possible strings of switch in a string as

char * switchstr[]={"abc", "def", "ghi"};

then search for the input string in the switchstr[] and use the index of matching string as switch case parameter.
And to know why strings are not supported in switch case read this answer. IT has discussion about why string cant be used directly in switch statement in C/C++.
And also switch case checks for equality and we know in C it is not possible to check the equality of string directly by using = sign, instead we need to use strcmp() function.

Community
  • 1
  • 1
LearningC
  • 3,182
  • 1
  • 12
  • 19
0

first of all I could understand "case" part as a "if" statement, but what dose switch do itself?

The switch part sets the expression that is used to compare with the values in the associated case labels.

2: why there is no need to put ";" after switch? what kind of the function is it?

It is not a function; switch is a language keyword that is part of the switch statement (i.e. the whole switch/case construct). It is not a function just because it uses parentheses any more than while(...) or if(...) are functions.

3: how compilers implement switch-case statement at the first time? (just C or use assembly)

That is up to the compiler. How things are implemented is generally not within the purview of a language standard, only (as precisely as possible) what should happen.

In practice, a switch statements tend to be implemented with branches if there are only a few cases, and with a jump table if there are many.

4: we can't use switch-case [with strings]

Because C defines the switch statement as working with integral types. A string is not an integral type. To provide the kind of functionality you envisage would require adding (at least) “content equality” testing for strings to the core C language. To do this consistently in a way that does not break existing code is probably impossible and contrary to the “minimalist” philosophy of C.

Emmet
  • 6,192
  • 26
  • 39