2

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?

9
  • 1
    A function w/o arguments should be void f(void), IIRC. void f() can accept any arguments (yes that's the amazing C language). Commented Nov 20, 2013 at 8:55
  • prime() is never called, is it? so there is no prototype necessary, because it's never called Commented Nov 20, 2013 at 8:55
  • it looks like you are not calling prime anywhere ? If you don't call it, there is no need to declare it (before). Commented Nov 20, 2013 at 8:56
  • 2
    Also, teach yourself to always use curly braces in if, etc. Even if there is only one statement. Commented Nov 20, 2013 at 8:58
  • Your comment is not correct. void prime(); is just a declaration; it isn't a prototype. Commented Nov 20, 2013 at 9:03

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

One 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
I suspect that this will compile. In C, redefinition is not allowed.
@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.
@haccks Oh, sorry, I have something different here than in my editor - thank you for the suggestion!
@haccks Yeah, sorry, the situation was like in that movie - lost in editing.
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.

2 Comments

One 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?
@Nat95; All the argument explicitly converts to int type.
0

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 too few, too many or wrongly typed arguments and how to correctly handle the return value (which might be short or char instead of int).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.