代码改变世界

linux c随手记

2021-12-18 19:48  虎背熊腰  阅读(26)  评论(0)    收藏  举报

1: 变量定义重复

 

重复include 了相同的文件会报 redefinition 错误

比如

==============

test.c

#include <stdio.h>

#include "test1.c"

#include "test2.c"

 

==============

test1.c

#include "test2.c"

 

==============

test2.c

int A=1;

====================================================================================

2: extern的使用

 

test.c

#include <stdio.h>

extern int A;

int main(){

    printf("%d",A);

}

============

test1.c

 

int A=1;

============

编译gcc test.c test1.c

 

3:  重复定义问题:

test.c

#include <stdio.h>
int A=2;
int main(){
printf("%d",A);
printf("%s","adsdsa");
}

test1.c

int A=1;

gcc test1.c test2.c 报错

 

=========== 

 

test.c

#include <stdio.h>
int A;
int main(){
printf("%d",A);
printf("%s","adsdsa");
}

test1.c

int A=1;

gcc test1.c test2.c 就不会报错

 

以上问题没理解,理论上两个应该都会报错

 

4: 函数的定义和申明

 函数申明: int rename() 等于 extern int rename();

 

#include <stdio.h>

int aa();

int aa();

int aa(){

printf("%d",1);

return 1;

}

int main(){

 int b= aa();

}

 

 

5: 结构体的定义和申明

 

申明:

extern struct STUDENT;这样也可以但是一般没这么操作的

 

extern struct STUDENT A; 常见操作