Reputation: 45
So I am a beginner in C and I have been following a book Primer Plus 6th edition. While I was running my program I noticed something really strange, compiler is not working as it is supposed to, for example
int main(){
one_three();
getchar();
return 0;
}
void one_three(void){
printf("one\n");
two();
printf("three");
}
void two(void){
printf("two\n");
}
this is my code, there are many errors as no function prototype declarations and not including header files, but some how the output is produced only by giving warnings, i dont think this should happen NOTE I HAVE INTENTIONALLY REMOVED FUNCTION PROTOTYPE DECLARTAIONS AND INCLUDING HEADER FILES
output is :
review1.c: In function 'main':
review1.c:2:5: warning: implicit declaration of function 'one_three' [-Wimplicit-function-declaration]
2 | one_three();
| ^~~~~~~~~
review1.c:3:5: warning: implicit declaration of function 'getchar' [-Wimplicit-function-declaration]
3 | getchar();
| ^~~~~~~
review1.c:1:1: note: 'getchar' is defined in header '<stdio.h>'; did you forget to '#include <stdio.h>'?
+++ |+#include <stdio.h>
1 | int main(){
review1.c: At top level:
review1.c:7:6: warning: conflicting types for 'one_three'
7 | void one_three(void){
| ^~~~~~~~~
review1.c:2:5: note: previous implicit declaration of 'one_three' was here
2 | one_three();
| ^~~~~~~~~
review1.c: In function 'one_three':
review1.c:8:5: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
8 | printf("one\n");
| ^~~~~~
review1.c:8:5: warning: incompatible implicit declaration of built-in function 'printf'
review1.c:8:5: note: include '<stdio.h>' or provide a declaration of 'printf'
review1.c:9:5: warning: implicit declaration of function 'two' [-Wimplicit-function-declaration]
9 | two();
| ^~~
review1.c: At top level:
review1.c:13:6: warning: conflicting types for 'two'
13 | void two(void){
| ^~~
review1.c:9:5: note: previous implicit declaration of 'two' was here
9 | two();
| ^~~
review1.c: In function 'two':
review1.c:14:5: warning: incompatible implicit declaration of built-in function 'printf'
14 | printf("two\n");
| ^~~~~~
review1.c:14:5: note: include '<stdio.h>' or provide a declaration of 'printf'
one
two
three
those last three lines "one" two three is the output Please help, i am using this compiler
I expect it to output errors when it has to and not add stuff itself, so that i could learn by my mistakes using visual studio code with extensions : C/C++ by microsoft, Code runner
Upvotes: 0
Views: 76
Reputation: 223689
Earlier pre-standard versions of C had the concept of a default type given to objects if they weren't explicitly given a type. Functions, if not declared, were implicitly typed as:
int func();
So a function without a prototype was assumed to return int
and take an unspecified number of arguments. Because old code was written with this in mind, many compilers are backward compatible and compile old code such as this, but issues warnings when these old constructs are used.
In your particular case, you're able to get away with the implicit function declarations of the functions you define because the functions have void
return type and no attempt is made to use the return value of these functions. For the getchar
and printf
functions, their return type is int
so they can work with the implicit declaration.
Upvotes: 2
Reputation: 1
You need to declare methods that you are using in functions above the functions you are using them in. If you reorder the functions to be two, then one_three, then main. It should work.
Upvotes: 0