3

I am trying to understand the switch statement in C (I am using gcc from Ubuntu v16.04). I am able to understand its semantics, but have the following 2 questions about its syntax:

  1. I have noticed after reading a few examples of usage of switch statement that the symbol after case is sometimes enclosed in '' and sometimes it isn't. eg: case 1 or case 'a'. I checked the Linux manpage for the switch statement (https://linux.die.net/man/1/switch) and there they have not used ' ' for a string. So I don't know what to do.

  2. Sometimes the block of code within a single case is enclosed in a { } and sometimes it is not. I had read before that multi-line statements need to be enclosed in a { }, but not necessarily for single-line statements as in a for loop,while loop with single line statements etc. But sometimes a case statement has 1 line of code (for eg a *= 5;) followed by break statement (so total 2 statements) and yet both lines are not enclosed in { }. The Linux manpages haven't mentioned this. Can someone clarify this?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Anon
  • 381
  • 1
  • 2
  • 13
  • 3
    Using `case 1:` looks for the switch expression to be equal to `1`. Using `case '1':` looks for the switch expression to be equal to the code point for the character `'1'`, which is typically equivalent to (but much clearer than) `case 49:`. – Jonathan Leffler Sep 28 '19 at 19:39
  • 2
    The braces after the case label are optional, but necessary if you want to declare variables immediately afterwards — you can't label a variable declaration. In C99 or later, you can declare variables in the middle of the sequence of statements after the case label, but doing so without braces has interesting consequences for the scope of the variable. – Jonathan Leffler Sep 28 '19 at 19:41
  • 5
    The linux man page for `switch` has *nothing* to do with the C language. – Marco Bonelli Sep 28 '19 at 19:41
  • Note `'a'` is not a string but a single character of type `int`. In C you can't use a string for a `case` statement. – Weather Vane Sep 28 '19 at 19:43
  • If you want to see some weird use of the switch statement, have a look at Duff's device. – klutt Sep 28 '19 at 20:38

4 Answers4

2

To answer your questions:
q1: 1 and '1' are not the same. The latter is surrounded by single-quotes, which in C always represent a character. Depending on the C implementation, this character would be stored in the ASCII format, with the numerical representation 49. Seeing as a character in ASCII format is guaranteed the ability to be represented numerically, but a number isn't, the comparison '1' == 1 is legal, because the character will be implicity converted to an integer.

q2: Enclosing a case in curly braces is optional. You can declare a scope at any time using curly braces. More information: https://www.geeksforgeeks.org/scope-rules-in-c/, C Switch-case curly braces after every case

tonayy
  • 173
  • 2
  • 12
2

(1) 'a' is ascii value 97. Ascii is a standard way of encoding characters and it's used in many other languages as well. Essentially, each character is represented as a numerical value. So when you have:

...
case 'a':
...

you are actually executing the code below the case if the switch variable is equal to 97. In your example:

case '1':

checks if the switch variable is equal to char '1', which is ascii value 49.

(2) Enclosing a case statement with braces changes the scope of the variables between the braces. Consider the following example:

switch (sw) {
    case 1:
        int b = 2;
        sw += b;
        break;
    case 2:
        int b = 3;
        sw += b;
        break;
    default:
        break;
}

This is because in case 1 and case 2, you are instantiating an integer called "b". Since both case statements are in the same variable scope (the switch statement's scope), the compiler gives you an error since you are instantiating a variable with the same name and type twice. Now consider the code below:

    switch (sw) {
    case 1: {
        int b = 2;
        sw += b;
        break;
    } case 2: {
        int b = 3;
        sw += b;
        break;
    } default: {
        break;
    }
}

This code compiles. By enclosing each case's code in braces, you are giving each case its own variable scope, where it could redefine the same variable once in each scope.

1

There's no man page for the switch used in C. What you're looking at is the man page for the switch command (as per the (1) in the name), something totally different.

  1. A case needs to use single quotes for a character constant and no single quotes for a non-character constant.

    For example, case '1': checks for the character '1' (typically with an integer value of 49), and case 1: checks for the integer value 1.

  2. The curly braces are mostly a preference of style. Some people think the curly braces makes their code look nicer and/or clearer, and some don't. There's no difference except for scope.

    This allows you to define a variable within an individual case label so you don't make it available to the whole function or switch statement.

Community
  • 1
  • 1
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
1

(1) You're asking about the switch statement in C (specifically, gcc), but the link you included is for the switch statement in a Linux shell. These are two different languages! In C, the single quote '' is used for characters. A character is a single letter/number/symbol/etc, as opposed to a string which is one or more characters. So case 1: will match the number 1, and case '1': will match the character '1'. A number has a type like int or long. A character has the type char. So whether you use '' or not depends on if you're trying to match a character or a number.

(2) The { } are not necessary. You might choose to use them to visually group the block of code, but you don't have to. You also can use { } to limit the scope of a variable. Variable scope is a big topic, see this for more info: https://www.w3schools.in/c-tutorial/variable-scope/

Dan Green
  • 51
  • 3