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.
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.
However if 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 in Ubuntu)
So my question is, is it necessary to declare a void function with not arguments?
If you don't have forward declaration of your function before the place of usage, 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 any arguments (as Bartek noted in the comment).
For this function, 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 forward declaration of 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();
// takes no 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 definition of the function you're describing function arguments. However, declaration of the function is telling the compiler not to do any checks on the parameters.
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.
my question is, is it necessary to declare a void function with not arguments?
Yes. For this you have to put void
in the function parenthesis.
void foo(void);
Declaring a function like
void foo();
means that it can take any number of arguments.
If prime is not used, then omit the declaration.
The code won't compile as C++ because the compiler would complain that function input
is 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.
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 to few, to many or wrongly typed arguments and how to correctly handle the return value (which might be short or char instead of int).
来源:https://stackoverflow.com/questions/20091233/declare-a-void-function-in-c