I have a minGW GCC compiler on my system. I am writing the code in C. Something that’s otherwise normal is not getting properly compiled due to some reason.
#include <stdio.h>
#include <math.h>
int main() {
sayHi();
return 0;
}
void sayHi(){
printf("Hello bro");
}
The output in cmd is: 6:2: warning: implicit declaration of function 'sayHi' [-Wimplicit-function-declaration]
Then it says 10:6: warning: conflicting types for 'sayHi' void sayHi(){.
Furthermore, :6:2: note: previous implicit declaration of 'sayHi' was here sayHi();
I tried the same on online compiler, but still this doesn’t work and gives the same error. However, in freecodecamp tutorial, the same code was written, but it got executed perfectly. How is that possible?
>Solution :
When the compiler sees sayHi(); it has not yet "seen" void sayHi(), and therefore it considers sayHi() implicitely to be declared as int sayHi().
Then it "sees" void sayHi(), but as sayHi() is now (implicitely) declared as int sayHi(), the return type is now conflicted, hence the warning.
As a rule of thumb you should consider warnings containing the word "implicit" as an errors.
You want this:
void sayHi(); // declare explicitely before using it
int main() {
sayHi();
return 0;
}
void sayHi(){
printf("Hello bro");
}
or this:
void sayHi() { // put the whole function before using it
printf("Hello bro"); // which declares it as void
}
int main() {
sayHi();
return 0;
}
Even better:
void sayHi() should be rather be void sayHi(void), which declares that the sayHi function takes no parameters at all.