1
int mmak = 0;
int main(void)
{
    int marks[3] [3] = {{0,5,6}, {1,5,9}, {2,9,5}};
    for (int k = 0; k < 3; k++)
    {
        int sunk = marks[k] [0];
        switch (sunk)
        {
            case '0': mmak++;
            break;
            case '1': mmak++;
            break;
            case '2': mmak++;
            break;
        }
    }
    printf("%i\n", mmak);
}

I want to update the value of global variable mmak. but the output I am still geting is 0. Can anyone help?

2 Answers2

1

Even if the issue is solved by removing single quoting of the clauses of your switch, I'd like to enrich this by adding some remarks:

  1. You should always consider a default case in a switch statement that covers unexpected situations.
  2. When you are printing an int you should use '%d' instead of '%i' for the reason explained here: ...difference between %d and %i ....
  3. Always end your main with a return [int]; statement.
#include <stdio.h>

int mmak = 0;

int main(void)
{
    int marks[3] [3] = {{0,5,6}, {1,5,9}, {2,9,5}};

    int sunk;

    for (int k = 0; k < 3; k++)
    {
        sunk = marks[k] [0];
        
        switch (sunk)
        {
            case 0:
                mmak++;
                break;

            case 1:
                mmak++;
                break;

            case 2:
                mmak++;
                break;

            default:
                printf("No value found\n");
                break;
        }
    }
    printf("%d\n", mmak);
    return 0;
}
Phazertron
  • 76
  • 6
  • 1
    ```%d``` is fine when using ```printf```. It's a different story when using ```scanf```. – picchiolu Dec 26 '22 at 08:46
  • Can you please en light me? I'm kinda new; to C specifically. – Phazertron Dec 26 '22 at 08:50
  • When used in ```printf```, both ```%i``` and ```%d``` behave in the same way, but in ```scanf``` ```%i``` is used to read hexadecimal values (if preceded by ```0x```) or octal values (if preceded by ```0```). This is explained in the very answer you linked to in your own answer. – picchiolu Dec 26 '22 at 08:54
  • Oh, yeah ok that's fine. The way you postulated your comment had me compelled that there was more in the `scanf()` I was missing. – Phazertron Dec 26 '22 at 08:56
  • *"When you are printing an int you should use '%d' instead of '%i'"* No difference for `printf()`, only for `scanf()`. – Zakk Dec 26 '22 at 09:07
  • *"Always end your main with a `return [int];` statement."* This is useless and doesn't add anything to the code. – Zakk Dec 26 '22 at 09:08
  • *"Declare the variable sunk outside the cycle and assign it on every iteration."* It's a better practice to declare variables near to where you use them. – Zakk Dec 26 '22 at 09:09
  • Regarding '%d', we already covered it's the same, but at least is good practice. So when I read code i know what to expect IMHO. About the return, I stated in the answer I'm trying to enrich the code. I thought it could come in handy to know that. – Phazertron Dec 26 '22 at 09:18
  • However regarding the position of the declaration, I've just read this: [Declaring variables inside loops, good practice or bad practice?](https://stackoverflow.com/questions/7959573/declaring-variables-inside-loops-good-practice-or-bad-practice) _italic_ I'll correct my answer. – Phazertron Dec 26 '22 at 09:22
0
case '2':

This case compares to the ASCII '2' (int 50), which is not the same as int 2. Instead use:

case 2:

The additional advice from comments for falling through cases will help when the same operation will be performed for more than one case:

case 0:
case 1:
case 2: mmak++;
break;
case 3: mmak+=2;
James Risner
  • 5,451
  • 11
  • 25
  • 47