The location where the function declaration is implemented in C language, and the function declaration in C Language
When I was learning C language, I encountered this problem, because the C # language I learned first, in the C # compiler, the declared position of the function will not affect the compilation result, however, an error occurred in the C language.
First look at a piece of code:
# Include <stdio. h> int main () {fun1 (); fun1 (); fun1 (); fun2 (); return 0;}/* method 1. used to output messages 1 */void fun1 () {printf ("For he's a jolly good fellow! \ N ");}/* method 2, used to output the Message 2 */void fun2 () {printf (" Which nobody can deny! \ N ");}
At this time, the compiler prompts the following:
-------------------- Configuration: mingw5-CUI Debug, compiler type: MinGW --------------------
Compiling C: \ Users \ yinhe \ Documents \ C-Free \ Temp \ Practice2-12-4.c...
[Error] C: \ Users \ yinhe \ Documents \ C-Free \ Temp \ Practice2-12-4.c: 27: error: conflicting types for 'fun1'
[Error] C: \ Users \ yinhe \ Documents \ C-Free \ Temp \ Practice2-12-4.c: 10: error: previous implicit declaration of 'fun1' was here
[Error] C: \ Users \ yinhe \ Documents \ C-Free \ Temp \ Practice2-12-4.c: 33: error: conflicting types for 'fun2'
[Error] C: \ Users \ yinhe \ Documents \ C-Free \ Temp \ Practice2-12-4.c: 13: error: previous implicit declaration of 'fun2' was here
[Warning] C: \ Users \ yinhe \ Documents \ C-Free \ Temp \ Practice2-12-4.c: 35: 2: warning: no newline at end of file
Compile abort C: \ Users \ yinhe \ Documents \ C-Free \ Temp \ Practice2-12-4.c: 4 errors, 1 warning
An error is prompted. Why is the function not found? If I am not good at E, it's hard to break me down. What should I do...
Try another method.
# Include <stdio. h>
/* Method 1, used to output the Message 1 */void fun1 () {printf ("For he's a jolly good fellow! \ N ");}/* method 2, used to output the Message 2 */void fun2 () {printf (" Which nobody can deny! \ N ") ;}int main () {fun1 (); fun1 (); fun1 (); fun2 (); return 0 ;}
After compilation, the result is displayed.
-------------------- Configuration: mingw5-CUI Debug, compiler type: MinGW --------------------
Compiling C: \ Users \ yinhe \ Documents \ C-Free \ Temp \ Practice2-12-4.c...
C: \ Users \ yinhe \ Documents \ C-Free \ Temp \ Practice2-12-4.c: 0 errors, 0 warnings
Generate C: \ Users \ yinhe \ Documents \ C-Free \ Temp \ Practice2-12-4.o
Note: The previous Guesses are correct, so let's think about the function declaration mentioned during C language learning. Do we need to try again the Code with the previous compilation errors inMainThe function is declared once before to check whether the compilation is successful.
1 # include <stdio. h> 2 3 void fun1 (void); 4 void fun2 (void); 5 6 int main () 7 {8 fun1 (); 9 fun1 (); 10 fun1 (); 11 fun2 (); 12 return 0; 13} 14 15/* method 1, used to Output Message 1 */16 void fun1 () 17 {18 printf ("For he's a jolly good fellow! \ N "); 19} 20 21/* method 2, used to Output Message 2 */22 void fun2 () 23 {24 printf (" Which nobody can deny! \ N "); 25}
Compiler compilation result:
-------------------- Configuration: mingw5-CUI Debug, compiler type: MinGW --------------------
Compiling C: \ Users \ yinhe \ Documents \ C-Free \ Temp \ Practice2-12-4.c...
C: \ Users \ yinhe \ Documents \ C-Free \ Temp \ Practice2-12-4.c: 0 errors, 0 warnings
Generate C: \ Users \ yinhe \ Documents \ C-Free \ Temp \ Practice2-12-4.o
The compilation is successful, indicating the differences between C and C #. Therefore, we need to remember to use custom functions in the future C Programming journey, so try to make the declaration and implementation of user-defined functionsMainBefore the function.
Here I want to say, Niang, I regret learning English when I go to school ~~