C语言 在 C 中声明一个 void 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20091233/
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
Declare a void function in C
提问by yaylitzis
I am learning C and I am studying functions. So, I read that when I implement my own function I have to declare it before the main(). If I miss the declaration the compiler will get an error message.
我正在学习 C 并且我正在学习函数。所以,我读到当我实现自己的函数时,我必须在 main() 之前声明它。如果我错过了声明,编译器将收到一条错误消息。
As I was studying this example (finds if the number is a prime number),
当我在研究这个例子时(查找数字是否为质数),
#include <stdio.h>
void prime(); // Function prototype(declaration)
int main()
{
int num, i, flag;
num = input(); // No argument is passed to input()
for(i=2,flag=i; i<=num/2; ++i,flag=i)
{
flag = i;
if(num%i==0)
{
printf("%d is not prime\n", num);
++flag;
break;
}
}
if(flag==i)
printf("%d is prime\n", num);
return 0;
}
int input() /* Integer value is returned from input() to calling function */
{
int n;
printf("\nEnter positive enter to check: ");
scanf("%d", &n);
return n;
}
I noticed that a function prime() is declared, but in the main, a function, input(), is called and also the function input() is implemented at the bottom. Ok, I thought it was a mistake and I change the name from prime to input.
我注意到声明了一个函数 prime(),但在主函数中,调用了一个函数 input(),并且函数 input() 在底部实现。好的,我认为这是一个错误,我将名称从素数更改为输入。
Howeverif I delete the declaration and I don't put any there, the program is compiled without errors and it runs smoothly. (I compile and run it on Ubuntu.)
但是,如果我删除声明并且我没有在那里放置任何声明,则程序编译时不会出错并且运行顺利。(我在 Ubuntu 上编译并运行它。)
Is it necessary to declare a void function with not arguments?
是否有必要声明一个没有参数的 void 函数?
回答by Nemanja Boric
If you don't have a forward declaration of your function before the place of usage, the compiler will create implicit declaration for you - with the signature int input(). It will take the name of the function you called, it will assume that the function is returning int, and it can accept anyarguments (as Barteknoted in the comment).
如果您在使用位置之前没有函数的前向声明,编译器将为您创建隐式声明 - 带有签名int input()。它将采用您调用的函数的名称,假定该函数正在返回int,并且可以接受任何参数(如Bartek在评论中指出的)。
For this function, the implicit declaration matches the real declaration, so you don't have problems. However, you should always be careful about this, and you should always prefer forward declarations instead of implicit ones(no matter if they are same or not). So, instead of just having forward declaration of the void prime()function (assuming that you will use it somewhere), you should also have a forward declaration of int input().
对于此函数,隐式声明与真实声明匹配,因此您没有问题。但是,您应该始终小心这一点,并且您应该始终更喜欢前向声明而不是隐式声明(无论它们是否相同)。因此,不仅要对void prime()函数进行前向声明(假设您将在某处使用它),还应该对int input().
To see how can you pass any number of the arguments, consider this:
要了解如何传递任意数量的参数,请考虑:
#include <stdio.h>
// Takes any number of the arguments
int foo();
// Doesn't takes any arguments
int bar(void)
{
printf("Hello from bar()!\n");
return 0;
}
int main()
{
// Both works
// However, this will print junk as you're not pushing
// Any arguments on the stack - but the compiler will assume you are
foo();
// This will print 1, 2, 3
foo(1, 2, 3);
// Works
bar();
// Doesn't work
// bar(1, 2, 3);
return 0;
}
// Definition
int foo(int i, int j, int k)
{
printf("%d %d %d\n", i, j, k);
return 0;
}
So, inside the definitionof the function you're describing function arguments. However, declarationof the function is telling the compiler not to do any checks on the parameters.
因此,在函数的定义中,您正在描述函数参数。但是,函数的声明告诉编译器不要对参数进行任何检查。
回答by haccks
Not declaring a prototype and relying on default argument/return type promotion is dangerous and was a part of old C. In C99 and onward it is illegal to call a function without first providing a declaration or definition of the function.
不声明原型并依赖默认参数/返回类型提升是危险的,并且是旧 C 的一部分。在 C99 及以后的版本中,在没有首先提供函数的声明或定义的情况下调用函数是非法的。
my question is, is it necessary to declare a void function with not arguments?
我的问题是,是否有必要声明一个没有参数的 void 函数?
Yes. For this you have to put voidin the function parenthesis.
是的。为此,您必须放入void函数括号中。
void foo(void);
Declaring a function like
声明一个函数,如
void foo();
means that it can take any number of arguments.
意味着它可以接受任意数量的参数。
回答by Werner Henze
If primeis not used, then omit the declaration.
如果不使用prime,则省略声明。
The code won't compile as C++, because the compiler would complain that function inputis used but not declared. A C compiler might issue a warning, but C is more relaxed and does an implicit declaration of input as int input()which means that you can pass any value to input and input returns an int.
代码不会编译为 C++,因为编译器会抱怨函数input已使用但未声明。AC 编译器可能会发出警告,但 C 更轻松,并且对 input 进行隐式声明,因为int input()这意味着您可以将任何值传递给 input 并且 input 返回一个int。
It is good style to always provide a function declaration before using the function. Only if you do this the compiler can see if you are passing too few, too many or wrongly typed arguments and how to correctly handle the return value (which might be shortor charinstead of int).
在使用函数之前总是提供函数声明是一种很好的风格。只有当你这样做时,编译器才能看到你是否传递了太少、太多或错误类型的参数,以及如何正确处理返回值(可能是short或char而不是int)。

