Anon
Anon

Reputation: 421

Syntax of switch statement in C?

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?

Upvotes: 4

Views: 5149

Answers (4)

tonayy
tonayy

Reputation: 193

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

Upvotes: 2

Thomas George
Thomas George

Reputation: 54

(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.

Upvotes: 3

S.S. Anne
S.S. Anne

Reputation: 15586

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.

Upvotes: 1

Dan Green
Dan Green

Reputation: 51

(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/

Upvotes: 1

Related Questions