【问题标题】:Function Not Working In My Program功能在我的程序中不起作用
【发布时间】:2017-07-06 05:52:02
【问题描述】:

我的编译器说“函数中的参数太少”。我不知道出了什么问题。有人知道我做错了什么吗?

#include<stdio.h>
#include<math.h>
int show(int a, int b, int c);

main( ){
int a, b = 10, c = 24;
printf("Enter a number\n");
scanf("%d\n", &a);
show(int a, int b, int c);
system("pause");
}

int show(int a, int b, int c){
if(a>c){
    printf("a is the largest number\n");
} else if(a>b){
    printf("a is smaller than c\n");
} else if(a<b){
    printf("a is bigger than b\n");
} else{
    printf("a is the smallest number \n");
}
return;
}

【问题讨论】:

  • show(int a, int b, int c); 里面的主到show(a,b,c);
  • 代码中没有使用来自&lt;math.h&gt;的任何东西,所以你不需要包含它;你确实使用了&lt;stdlib.h&gt; 中的一个函数,所以你需要包含它。您应该在 main() 上使用显式返回类型——这是自 C99 以来所必需的。您在main() 中的show(int a, int b, int c); 声明使用了C99 特性——在代码中的任何位置声明——但不包括显式返回类型,但C99 要求这样做。该代码还有很多不足之处。而且您需要在编译器和/或更新的编译器上使用更多警告选项。
  • 要跟进@JonathanLeffler 提到的内容,main() 不再是有效的签名,您至少应该使用int main(void)
  • @JonathanLeffler 对,但是only main() 几乎是无效的,对吗?
  • 具有显式int 返回类型的函数也不能有return ;,如果它完全有效,这会调用定义不明确的行为。总的来说,这段代码有许多非常基本的问题。我会考虑获取学习 C 的新资源,您当前的资源显然没有帮助。

标签: c


【解决方案1】:

您在函数调用中声明了变量。它在 C 中无效。

所以,使用这个:

show(a, b, c);

而不是

show(int a, int b, int c);

【讨论】:

  • 它是半有效的 C99。它实际上是另一个函数声明而不是函数调用,但缺少显式返回类型int
【解决方案2】:
#include<stdio.h>
#include<math.h>
void  show(int a, int b, int c);

main( ){
int a, b = 10, c = 24;
printf("Enter a number\n");
scanf("%d\n", &a);
show(a,b,c);
return ;
}

    void show(int a, int b, int c){
    if(a>c){
        printf("a is the largest number\n");
    } else if(a>b){
        printf("a is smaller than c\n");
    } else if(a<b){
        printf("a is bigger than b\n");
    } else{
        printf("a is the smallest number \n");
    }
 }

这是一个完整的代码,请在函数中使用 void 因为如果你使用 int 你需要一些变量来处理这个函数中的返回变量你不需要 int 所以尝试使用 return 0 而不是 system()

void show(int a, int b, int c)

int show(int a, int b, int c)

【讨论】:

    【解决方案3】:

    您将函数定义为:int show(int a, int b, int c),但使用不正确的参数调用它:show(int a, int b, int c)。你应该做的是用show(a, b, c) 调用它,因为a,b,c 已经声明和定义了

    【讨论】:

    • 这实际上是函数的第二个声明——而不是对函数的调用。由于缺少返回类型,它是草率的 C90 代码。在 C99 和 C11 中无效。
    • 所以有 3 个声明?在 main 之前、内部和之后?
    • 是的——似乎有点多余,不是吗。 :D
    • 有点 :) 但如果是这样,错误不应该是重新声明 show 吗?
    • 您可以根据需要随时重新声明内容——只要新声明隐藏了之前的声明或与之前的声明一致。声明使用“隐式int”——不好。但它仍然是int的返回类型,与顶部的显式原型相同。函数中的声明部分是 C99 特性。所以这是令人困惑的代码。
    【解决方案4】:

    应该是

    #include<stdio.h>
    #include<math.h>
    int show(int a, int b, int c);
    
    main( ){
    int a, b = 10, c = 24;
    printf("Enter a number\n");
    scanf("%d\n", &a);
    show(a,b,c);
    system("pause");
    }
    
    int show(int a, int b, int c){
    if(a>c){
        printf("a is the largest number\n");
    } else if(a>b){
        printf("a is smaller than c\n");
    } else if(a<b){
        printf("a is bigger than b\n");
    } else{
        printf("a is the smallest number \n");
    }
    return;
    }
    

    【讨论】:

    • 您没有包含&lt;stdlib.h&gt;,但应该包含。而main() 需要明确的返回类型。
    猜你喜欢
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 2021-07-05
    • 2019-04-06
    • 1970-01-01
    • 2015-07-26
    相关资源
    最近更新 更多