C语言 在 switch case 中调用函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34714834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 12:17:47  来源:igfitidea点击:

Calling a function within a switch case

c

提问by Salim Albusaidi

I have been trying to create a calculator or a semi calculator, I want to know what went wrong with my code and why?

我一直在尝试创建一个计算器或半计算器,我想知道我的代码出了什么问题,为什么?

This is my code where I get errors while compiling it, in one machine I get different error while in other machine, the error is different .

这是我在编译时出错的代码,在一台机器上我得到不同的错误,而在另一台机器上,错误是不同的。

I hope you guys can help me sort it out. Its a small program to understand C programming little bit better.

希望大家帮我整理一下。它是一个小程序,可以更好地理解 C 编程。

#include<stdio.h>
int main()
{
    int operation,fc,v1,v2;
    double fcd;
    printf("input 2 values\n");
    scanf("%d%d",&v1,&v2);
    /* telling the user to choose any type of calculator */
      printf("please choose what you want to do with your values\n");
      printf("1- Sum\n");
      printf("2- substracttion\n"); 
      printf("3- multiplay\n");
      printf("4- devision\n"); 
      scanf("%d",&operation);//input a
      switch(operation)
    {
      case 1:
      int fc = sum(int v1, int v2);
        printf("sum of two values = %d\n",fc);
        break;

    case 2:
       int fc = substact(int v1, int v2);
       printf("substract of two values = %d",fc);
        break;

    case 3:
        int fc = multiplay(int v1, int v2);
        printf("multiply of two values = %d\n",fc);
        break;

    case 4:
       int fcd = devision(int v1, int v2);
       printf("division of two values = %d\n",fc);
        break;
    default:
        printf("wrong choice\n");
    }
return 0;}

int sum(int a,int b)
{
    int sum=0;
    um=a+b;
    return sum;
}
int substact(int a,int b)
{
    int sub=0;
    sub=a-b;
    return sub;
}
int multiplay(int a,int b)
{
    int mult=1;
    mult=a*b;
    return mult;
}
double devision(int a,int b)
{
    double  devi=1;
    devi=a/b;
    return devi;
}

采纳答案by Assaf Stoler

Several Issues:

几个问题:

You need to declare functions before you use them.

在使用它们之前,您需要声明函数。

You can not repeat the type declaration. for example, you declare fc at the beginning of your program, and you repeat it the switch statement cases: "int fc = ...", just use "fc = ..."

您不能重复类型声明。例如,您在程序的开头声明 fc,然后重复 switch 语句案例:“int fc = ...”,只需使用“fc = ...”

Last, do not declare the type when calling a function. below is a minimally corrected code:

最后,在调用函数时不要声明类型。下面是一个最小修正的代码:

#include<stdio.h>
int sum(int a,int b);
int substact(int a,int b);
int multiplay(int a,int b);
double devision(int a,int b);

int main()
{
    int operation,fc,v1,v2;
    double fcd;
    printf("input 2 values\n");
    scanf("%d%d",&v1,&v2);
      /* telling the user to choose any type of calculator */
    printf("please choose what you want to do with your values\n");
    printf("1- Sum\n");
    printf("2- substracttion\n"); 
    printf("3- multiplay\n");
    printf("4- devision\n"); 
    scanf("%d",&operation);//input a
    switch(operation)
    {
      case 1:
        fc = sum(v1, v2);
        printf("sum of two values = %d\n",fc);
        break;

      case 2:
        fc = substact(v1, v2);
        printf("substract of two values = %d",fc);
        break;

      case 3:
        fc = multiplay(v1, v2);
        printf("multiply of two values = %d\n",fc);
        break;

      case 4:
        fcd = devision(v1, v2);
        printf("division of two values = %d\n",fc);
        break;
      default:
        printf("wrong choice\n");
    }
return 0;
}

int sum(int a,int b)
    {
    int sum=0;
    sum=a+b;
    return sum;
    }
int substact(int a,int b)
    {
    int sub=0;
    sub=a-b;
    return sub;
    }
int multiplay(int a,int b)
    {
    int mult=1;
    mult=a*b;
    return mult;
    }
double devision(int a,int b)
    {
        double  devi=1;
    devi=a/b;
    return devi;
    }

回答by Mohit Jain

Problem 1
C (pre C99) doesn't allow the declaration of variables in local scope anywhere except start of block. (Before any statement is executed). Also in your case it can cause multiple variables with same name in same scope which is not allowed.

问题 1
C(C99 之前)不允许在除块开始之外的任何地方在局部范围内声明变量。(在执行任何语句之前)。同样在您的情况下,它可能会导致在同一范围内具有相同名称的多个变量,这是不允许的。

int fcand int fcdbreak above condition so should be moved to the top of function. (They are already present, just remove intfrom cases)

int fcint fcd打破上述条件,因此应移至函数顶部。(它们已经存在,只需int从案例中删除)

Problem 2
While passing arguments, you don't need to give the type. For example sum(int v1, int v2);is bad. (Remove intfrom the arguments)

问题 2
传递参数时,不需要给出类型。例如sum(int v1, int v2);坏。(int从论据中删除)

Warning
Always declare (or define) the functions before their first usage. In your program, functions like sum, substract etc are used before declaration in main.

警告
在第一次使用之前总是声明(或定义)函数。在您的程序中,在声明之前使用 sum、substract 等函数main

Further readings:
Variable inside switch
function call in C
Function in C
Why do functions need to be declared before they are used?

进一步阅读:C 中的
switch
函数调用中的变量C 中的
函数
为什么函数需要在使用前声明?

回答by Meraj Hussain

The below statement in your code might have caused compilation errors for you.

您代码中的以下语句可能会导致编译错误。

 int fc = sum(int v1, int v2);

You have already declared fc variable then why are you declaring it here again. Declaring variables multiple times within a function can cause compilation errors. One more thing is that you cannot make a call to a function in the below way int fc = sum(int v1, int v2). Go through books related to C programming on how to make a funcction call.

您已经声明了 fc 变量,那为什么还要在这里再次声明它。在一个函数中多次声明变量会导致编译错误。还有一件事是,您不能通过以下方式调用函数 int fc = sum(int v1, int v2)。阅读与 C 编程相关的书籍,了解如何进行函数调用。

SO modify the above C statement as follows.

SO修改上面的C语句如下。

       fc = sum(v1,v2);

I have not compiled your code but i think that above change should resolve your compilation errors

我还没有编译您的代码,但我认为上述更改应该可以解决您的编译错误

回答by SHIVANAND RASKONDA

This is another little bit different program and easy way to understand the calling a function within a switch case. This is one of the easiest ways to declare and call the function

这是另一个稍微不同的程序,也是理解在 switch case 中调用函数的简单方法。这是声明和调用函数的最简单方法之一

Declare all the functions and call from the outside of a function

声明所有函数并从函数外部调用

#include<stdio.h>
#include<conio.h>
int x,y;
int addi(int,int);//when call the function. The function must be integer type.
int multi(int,int);
int divi(int,int);
int square(int);
int evenOdd(int);
int substracn(int,int);

int main()
{
   int choice;   

printf("*********************************************************************\n");
printf("\t\tCalculator\n");
printf("*********************************************************************\n");

printf("Enter a choice what you want to perform");
printf("\n\t1.Addition");
printf("\n\t2.Multiplication");
printf("\n\t3.Division");


printf("\n\t4.Square");
printf("\n\t5.Even or Odd");
printf("\n\t6.Substraction\n");
scanf("%d",&choice);



switch(choice)//switch case use when more than 2 options are there. Then it is the best 
{
case 1: printf("\n\tAddition");
        printf("\nEnter two no for addition");
        scanf("\n%d\t%d",&x,&y);
        addi(x,y);//function by passing paramenter
        break;//break the statement after the execution of the function definition

case 2: printf("Enter two no's for multiplication");//multiplication
        scanf("\n%d\t%d",&x,&y);
        multi(x,y);
        break;

case 3: printf("Enter two no's for division");
        scanf("%d%d",&x,&y);
        divi(x,y);
        break;


case 4: printf("Enter one no for square of no");
        scanf("%d",&x);
        square(x);
        break;

case 5: printf("Enter the no for even odd");
        scanf("%d",&x);
        evenOdd(x);
        break;

case 6: printf("Enter the two no's for Substraction");
        scanf("%d%d",&x,&y);
        subtracn(x,y);
        break;
default:
    printf("Enter a valid option");

}

getch();
}

int addi(int a,int b)
{
    int c;
    c=a+b;
    printf("sum of no %d + %d is: %d",a,b,c);

}

int multi(int a,int b)
{
    int c;
    c=a*b;
    printf("multiplication of no %d x %d is: %d",a,b,c);

}

int divi(int a,int b)
{
    int c;
    c=a/b;
    printf("division of no's is: %d",c);

}

int square(int a)
{
    int c;
    c=a*a;
    printf("square of no %d is: %d",a,c);

}

int evenOdd(int a)
{
    if(a%2==0)
    {
    printf("The no is even : %d",a);
    }
    else
    {
    printf("The no is odd : %d",a);    
    }

}
int subtracn(int a,int b)
{
    int c;
    c=a-b;
    printf("Substraction of no's is: %d",c);

}`

回答by vcp

Compilation error is most likely about declarations of functions sum, substract etc. Code you have written have defined the functions after they have been used in the main function. Please "declare" all functions BEFORE main function, if you plan to call them from main function.

编译错误最有可能与函数 sum、substract 等的声明有关。您编写的代码在主函数中使用后定义了这些函数。如果您打算从 main 函数调用它们,请在 main 函数之前“声明”所有函数。

回答by Jar Yit

I hope this solution will help you. Before you call the function, you must declare first. Here is sample source code.

我希望这个解决方案能帮助你。在调用函数之前,必须先声明。这是示例源代码。

   #include <stdio.h>

  /* function declaration */
  int max(int num1, int num2);

 int main () {

 /* local variable definition */
 int a = 100;
 int b = 200;
 int ret;

 /* calling a function to get max value */
 ret = max(a, b);

 printf( "Max value is : %d\n", ret );

return 0;

}

}

 /* function returning the max between two numbers */
 int max(int num1, int num2) {

 /* local variable declaration */
int result;

if (num1 > num2)
  result = num1;
else
  result = num2;

return result; 

}

}