我该如何摆脱这个错误

时间:2018-03-24 02:31:27

标签: c

当我将case语句作为1时,我收到错误 如何运行和使用案例陈述?

#include <stdio.h>
#include <stdlib.h>
int fib(int n)
{
   if ( n <= 1 )
      return n;
   return fib(n-1) + fib(n-2);
}

int main ()
{
  int n ;
    printf("Enter Number to Compute nth Fibonacci Series");
    scanf("%d",&n);
  printf("\n%dth term of fibonacci series number is %d",n, fib(n));

  return 0;
}

2 个答案:

答案 0 :(得分:0)

假设您希望fib函数使用switch语句,这是一个可能的解决方案:

int fib(int n)
{
    switch( n ) {
        case 0:
            return 0;
        case 1:
            return 1;
        default:
            return fib( n-1 ) + fib( n-2 );
    }
}

答案 1 :(得分:0)

我收到错误如何让它运行并使用案例陈述?哪个用例?没有编译时错误,只有用例你没有检查用户是否输入了negative值。

if(n>=0) {
     printf("\n %dth term of fibonacci series number is %d\n",n, fib(n));
}
else {
     printf("you entered -ve value \n");
}

同时使用警告启用标志-Wall编译代码

gcc -g -O -Wall test.c

警告你喜欢 忽略'scanf'的返回值,使用属性warn_unused_result声明[-Wunused-result]

printf("Enter Number to Compute nth Fibonacci Series");
int ret = scanf("%d",&n);/* always check the return 
                             value of predefined function */