【问题标题】:Cannot declare a void function before the main() in C不能在 C 中的 main() 之前声明 void 函数
【发布时间】:2020-04-07 06:50:06
【问题描述】:

我是 C 语言的初学者,在我的 previous question 中,我询问了 C 中正确的函数声明顺序。有人告诉我,在标准 C 中,有必要在调用函数之前声明函数。

但是对于下面的示例代码,我不能在 main() 之前声明函数 times2p。我试图在 main() 之前将其声明为:void times2(int in_data);,但出现错误。下面是示例代码:

#include <stdio.h>
#include <stdlib.h>

 void times2p(int *in_data);
 void times2(int in_data);


int main()
{

    int y = 5;
    int s = times2(y);
    printf("%d\n", y);
    printf("%d\n", s);

    printf("-------------\n");

    int yp = 5;
    times2p(&yp);
    printf("%d\n", yp);

    return 0;
}
//Multiplies the input argument by two
void times2(int in_data){
    in_data = in_data*2;
}

void times2p(int *in_data){
    *in_data = *in_data*2;
}

在这种情况下应该如何以及在哪里声明 times2p 函数?如果我不声明代码仍然编译没有错误,但我被告知我必须在上一个问题中声明 C 中的函数。

这是错误:

||=== Build: Debug in test1 (compiler: GNU GCC Compiler) ===|
C:\Users\nnn\Documents\CodeBlocks\test1\main.c||In function 'main':|
C:\Users\nnn\Documents\CodeBlocks\test1\main.c|16|error: void value not ignored as it ought to be|
C:\Users\nnn\Documents\CodeBlocks\test1\main.c|13|warning: unused variable 'read_x' [-Wunused-variable]|
||=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|

【问题讨论】:

  • 是因为您应该将其声明为void times2p(int *in_data); 以匹配函数定义吗?缺少*。
  • 那些错误是什么?
  • 如果你不声明函数,你编辑了代码“编译没有错误”。不过应该有警告吗?
  • 请显示您遇到的确切错误。
  • 一个 void 函数不返回任何值。这就是问题所在:int s = times2(y);

标签: c function declaration


【解决方案1】:

您在当前版本的问题中的代码不正确,但不是因为您询问的原因。

main 之前的 void 函数声明没有任何问题,编译器也不会抱怨它们。问题是void 函数没有返回值,而您试图像调用它们一样调用它们:

int s = times2(y);

只有当times2(y) 返回一个int 值(或某种可隐式转换为int 的类型的值)时,这才是正确的。

还有,看函数的定义:

void times2(int in_data){
    in_data = in_data*2;
}

参数in_data是函数内的一个局部变量。有效地改变它的价值没有任何作用。

您的times2p 函数看起来不错,应该可以工作。如果你想让times2() 有用,它需要返回一个值:

int times2((int in_data) {
    return in_data * 2;
}

【讨论】:

    【解决方案2】:

    (问题已经改变,所以这个答案可能不再完美,抱歉)。

    在使用之前,您至少需要声明函数的原型。像这样:

    #include <stdio.h>
    #include <stdlib.h>
    
    //How should the function times2p be declared here?
    void times2p(int *in_data);
    
    int main()
    {
        int y = 5;
        times2p(&y);
        printf("%d\n", y);
    
        return 0;
    }
    
    //Multiplies the input argument by two
    void times2p(int *in_data){
        *in_data = *in_data*2;
    }
    

    或者声明函数本身,像这样:

    #include <stdio.h>
    #include <stdlib.h>
    
    //Multiplies the input argument by two
    void times2p(int *in_data){
        *in_data = *in_data*2;
    }
    
    int main()
    {
        int y = 5;
        times2p(&y);
        printf("%d\n", y);
    
        return 0;
    }
    

    【讨论】:

    • 我编辑了我的代码,对不起,我没想到会得到这个快速的答案。你能编译我的新代码吗?我收到错误。
    • 为了改进答案,请提供一些解释,而不仅仅是代码转储。
    • 我在问题中添加了错误:无效值没有被忽略,因为它应该是
    • 我已经回答了@floppy380
    • @EduardoFernandes 在 C99 及更高版本中,调用未声明的函数不仅仅是未定义的行为,而是违反约束。 (C99 删除了“隐式int”规则。)
    猜你喜欢
    • 2017-08-26
    • 2019-09-22
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多