1

I want to calculate the i-th sentence of the Fibonacci sequence:

#include <stdio.h>

int fibo(int i);

int main()
{
    return 0;
}

int fibo(int i)
{
    switch (i)
    {
    case 1:
        return 0;
        break;
    case 2:
        return 1;
        break;
    default:
        int a = 0, b = 1;
        for (int p = 3; p <= i; p++)
        {
            b += a;
            a = b - a;
        }
        return b;
        break;
    }
}

But it has an error for 18 line for int a = 0, b = 1; as this:

10.c: In function 'fibo':
10.c:18:9: error: a label can only be part of a statement and a declaration is not a statement
   18 |         int a = 0, b = 1;
      |         ^~~

I use VS Code for IDE.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Parsa Saberi
  • 105
  • 7
  • The error message is precisely correct. Case labels are labels. – rici Nov 01 '20 at 05:37
  • 1
    Maybe this is helpfull for you: https://stackoverflow.com/questions/46341364/label-can-only-be-used-as-part-of-a-statement-error – moritz Nov 01 '20 at 05:40
  • regarding: `int main() { return 0; }` This fails to call the function: `fibo()` so that function will never be executed – user3629249 Nov 03 '20 at 07:59
  • the variable `i` is never set, never declared, (this should be done in `main()` so the code will not compile. – user3629249 Nov 03 '20 at 08:01
  • note: the Fibonacci sequence starts with 1, 1, 2, 3, 5, 8,... The posted code does not implement that sequence – user3629249 Nov 03 '20 at 08:03

3 Answers3

2

in C, in a switch statement, in a case, to have a local variable, the body of the case must be enclosed in braces '{' and '}'

user3629249
  • 16,402
  • 1
  • 16
  • 17
0
  #include <stdio.h>
    int fibo(int i);
    int main()
    {
        return 0;
    }
    int fibo(int i)
    {
        int a = 0, b = 1;
        switch (i)
        {
        case 1:
            return a;
            break;
        case 2:
            return b;
            break;
        default:
            for (int p = 3; p <= i; p++)
            {
                b += a;
                a = b - a;
            }
            return b;
            break;
        }
    }

Here I used the declaration part outside the switch case as inside the label only statements are allowed not declaration.

adrsh23
  • 210
  • 2
  • 10
0

The C grammar does not allow a declaration to be labelled. default: and case <expr>: are labels. There are 3 ways to fix the problem in your code:

  • move the declarations outside of the body of the switch statement:

    int fibo(int i) {
        int a, b;
        switch (i) {
        case 1:
            return 0;
        case 2:
            return 1;
        default:
            a = 0, b = 1;
            for (int p = 3; p <= i; p++) {
                b += a;
                a = b - a;
            }
            return b;
        }
    }
    
  • enclose declarations in a block:

    int fibo(int i) {
        switch (i) {
        case 1:
            return 0;
        case 2:
            return 1;
        default: {
                int a = 0, b = 1;
                for (int p = 3; p <= i; p++) {
                    b += a;
                    a = b - a;
                }
                return b;
            }
        }
    }
    
  • add a null statement after the label:

    int fibo(int i) {
        switch (i) {
        case 1:
            return 0;
        case 2:
            return 1;
        default:;  // this is a null statement
            int a = 0, b = 1;
            for (int p = 3; p <= i; p++) {
                b += a;
                a = b - a;
            }
            return b;
        }
    }
    

Note also these remarks:

  • the break; statements in your fibo function are never reached and should be removed,

  • the case 2: is redundant: the default: code would produce the same result,

  • your code does not implement the classic Fibonacci sequence: 1 1 2 3... instead it produces 0 1 1 2... You should change the tests to this:

    int fibo(int i) {
        switch (i) {
        case 0:
            return 0;
        default:;  // this is a null statement
            int a = 1, b = 1;
            for (int p = 2; p < i; p++) {
                b += a;
                a = b - a;
            }
            return b;
        }
    }
    
  • you should probably return 0 for negative numbers too. Just simplify the code as:

    int fibo(int i) {
        int a = 1, b = 0;
        while (i-- > 0) {
            b += a;
            a = b - a;
        }
        return b;
    }
    
chqrlie
  • 131,814
  • 10
  • 121
  • 189