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?
void f(void), IIRC.void f()can accept any arguments (yes that's the amazing C language).primeanywhere ? If you don't call it, there is no need to declare it (before).if, etc. Even if there is only one statement.void prime();is just a declaration; it isn't a prototype.