C functions (variables) should be defined (declared) first, and then used

2020年3月20日
When learning C language today, I learned that functions cannot be nested definitions, but should be defined in parallel, as shown below:

#include <stdio.h> //by [C语言中文网](http://c.biancheng.net/view/1851.html)
void func2(){
    
    		//函数2
    printf("C语言小白变怪兽");
}
void func1(){
    
    		//函数1
    printf("http://c.biancheng.net");
    func2();		//调用函数2
}
int main(){
    
    		//主函数
    func1();	//调用函数1
    return 0;	
}

But when I wrote to my friend, I changed the order:

#include <stdio.h> 		
void func1(){
    
    							//这里函数func1()的位置提前了
    printf("http://c.biancheng.net");
    func2();
}
void func2(){
    
    
    printf("C语言小白变怪兽");
}
int main(){
    
    
    func1();
    return 0;
}	//此段代码报错

Compiled, and an error was reported: It
Insert picture description here
means that it is func2 未定义clearly defined, and finally I consulted with the boss, and then I wanted to understand:

The C language is executed line by line . It needs to be declared first.

void func1(){
    
    							
    printf("http://c.biancheng.net");
    func2();
}

During this program, func1call func2, but this time func2has not been declared, it is an error,
it is necessary to advance declaration under func2before calling.

Just declare in advance:

#include <stdio.h> 
void func2()		//提前声明fun2(),以供调用

void func1(){
    
    			//这里声明了func1(),故在main()中可直接调用
    printf("http://c.biancheng.net");
    //也可以在这一行进行fun2()的声明,只要是在func2()之前就可以
    func2();
}

void func2(){
    
    
    printf("C语言小白变怪兽");
}

int main(){
    
    
    func1();	
    return 0;
}

But the error code is C2371"func2()"重定义;不同的基类型 , but I don’t understand why, so I have a chance to find the reason again.
to be continue -_–

Guess you like

Origin blog.csdn.net/weixin_42417585/article/details/104988772