Declare a void function in C
Solution 1
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 any arguments (as Bartek noted in the comment).
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()
.
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 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.
Solution 2
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.
yaylitzis
Updated on July 09, 2022Comments
-
yaylitzis almost 2 years
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 on Ubuntu.)
Is it necessary to declare a void function with not arguments?
-
Bartek Banachewicz over 10 yearsA function w/o arguments should be
void f(void)
, IIRC.void f()
can accept any arguments (yes that's the amazing C language). -
Constantin over 10 yearsprime() is never called, is it? so there is no prototype necessary, because it's never called
-
Andre Holzner over 10 yearsit looks like you are not calling
prime
anywhere ? If you don't call it, there is no need to declare it (before). -
Bart Friederichs over 10 yearsAlso, teach yourself to always use curly braces in
if
, etc. Even if there is only one statement. -
CB Bailey over 10 yearsYour comment is not correct.
void prime();
is just a declaration; it isn't a prototype. -
yaylitzis over 10 yearsThis code isn't mine. I just google examples function in C and I found this...
-
Klas Lindbäck over 10 yearsYou should enable warnings. If you call a function before it is declared, the compiler emits a warning.
-
yaylitzis over 10 yearshow can I enable warnings?
-
Nemanja Boric over 10 years
-
-
yaylitzis over 10 yearsOne more question. You said that
void foo()
can accept any number of arguments. In this case how I know, how will be the matching of the passing arguments with the variables inside the function? -
yaylitzis over 10 yearsOne more question. You said that void prime() can accept any arguments. In this case how I know, how will be the matching of the passing arguments with the variables inside the function? – Nat95 2 mins ago @NemanjaBoric
-
haccks over 10 years@Nat95; All the argument explicitly converts to
int
type. -
haccks over 10 yearsI suspect that this will compile. In C, redefinition is not allowed.
-
Nemanja Boric over 10 years@haccks This will compile, as there's no redefinition - first statement is declaration, and the second one is definition. Try compiling it with
-std=c99 -Wall -Wextra
. -
Nemanja Boric over 10 years@haccks Oh, sorry, I have something different here than in my editor - thank you for the suggestion!
-
Nemanja Boric over 10 years@haccks Yeah, sorry, the situation was like in that movie - lost in editing.