C中变量的声明与定义

来源:互联网 发布:美国 大数据公司 data 编辑:程序博客网 时间:2024/05/29 17:17

在C中,变量的定义主要可分为两种状况:在函数内和在函数外。

但变量在函数内定义时,其属性只能分为static和无static,而该变量便无法被外部函数所引用。而定义为static时表示该变量只能用来初始化一次。

而变量在函数外定义时,其属性也只能分为static和extern,而如果不以static来修饰变量,编译器便会默认将该变量的属性设为extern(等同于使用extern来定义)。

若为static,则表示该变量只能在定义的文件里使用,无法被别的文件里的程序调用。

关于声明:变量的声明默认着需要通过extern来进行声明,而且声明可以进行多次,而被声明的变量应为extern属性,即其属性不能被声明为static。

而声明可以在函数内部,也可在函数外部。

1、 变量在同一个文件里定义

(1)变量在函数外定义

#include <stdio.h>

int i = 12;

int main()

{

       printf(“thei in the 1.c is %d\n”, i);

}

结果为

the i in the 1.c is 12.

(2)变量在函数内也定义

#include <stdio.h>

int i = 12;

int main()

{

       inti = 123;

       printf(“thei in the 1.c is %d\n”, i);

}

结果为

the i in the 1.c is 123.

对比上面可知,如果函数内定义了与函数外同一名字的变量(即函数的全局变量和局部变量同名),则局部变量会覆盖全局变量。

(3)、变量在函数内进行extern引用

#include <stdio.h>

int i = 12;

int main()

{

       externint i;

       inti = 123;

       printf(“thei in the 1.c is %d\n”, i);

}

结果为:


编译器提示出错,i被重复定义了。

(4)变量在函数外进行extern声明

#include <stdio.h>

extern int i = 12;

int main()

{

       externint i;

       inti = 123;

       printf(“thei in the 1.c is %d\n”, i);

}

结果:


可以发现,会有警告,其仍可运行,且结果正确。

2、 变量在多个文件间

(1)在1.c中:

#include <stdio.h>

int i = 12;

int main()

{

       inti = 123;

       printf(“thei in the 1.c is %d\n”, i);

       a2();

}

       在2.c中:

void a2(void)

{

       printf(“thei in the 2.c is %d\n”, i);

}

结果:


可以发现在1.c中定义的i仅在1.c可用,而2.c中不可用。

(2)在1.c中:

#include <stdio.h>

extern int i = 12;

int main()

{

       inti = 123;

       printf(“thei in the 1.c is %d\n”, i);

       a2();

}

       在2.c中:

void a2(void)

{

       printf(“thei in the 2.c is %d\n”, i);

}

结果:


在1.c中对变量是否加extern无法影响2.c中的i。

(3)在1.c中:

#include <stdio.h>

int i = 12;

int main()

{

       inti = 123;

       printf(“thei in the 1.c is %d\n”, i);

       a2();

}

       在2.c中:

void a2(void)

{

extern int i;

       printf(“thei in the 2.c is %d\n”, i);

}

结果:


(4)在1.c中:

#include <stdio.h>

int i = 12;

int main()

{

       inti = 123;

       printf(“thei in the 1.c is %d\n”, i);

       a2();

}

       在2.c中:

extern int i;

void a2(void)

{

       printf(“thei in the 2.c is %d\n”, i);

}

结果:


(5)在1.c中:

#include <stdio.h>

int i = 12;

int main()

{

       inti = 123;

       printf(“thei in the 1.c is %d\n”, i);

       a2();

}

       在2.c中:

extern int i;

void a2(void)

{

       inti = 2123;

       printf(“thei in the 2.c is %d\n”, i);

}

结果:


(6)在1.c中:

#include <stdio.h>

int i = 12;

int main()

{

       inti = 123;

       printf(“thei in the 1.c is %d\n”, i);

       a2();

}

       在2.c中:

extern int i =212;

void a2(void)

{

       inti = 2123;

       printf(“thei in the 2.c is %d\n”, i);

}

结果:


可以发现通过externint进行定义的话,会与1.c中的int i冲突而出错。

(7)在1.c中:

#include <stdio.h>

int i = 12;

int main()

{

       inti = 123;

       printf(“thei in the 1.c is %d\n”, i);

       a2();

}

       在2.c中:

int i = 212;

void a2(void)

{

       inti = 2123;

       printf(“thei in the 2.c is %d\n”, i);

}

结果:


可以发现如果变量在函数外定义,其默认已加上了extern。

(8)在1.c中:

#include <stdio.h>

static int i = 12;

int main()

{

       inti = 123;

       printf(“thei in the 1.c is %d\n”, i);

       a2();

}

       在2.c中:

int i = 212;

void a2(void)

{

       inti = 2123;

       printf(“thei in the 2.c is %d\n”, i);

}

结果:


在1.c中在变量前加上static便可避免了冲突,因为static将该变量变为了局部的变量。

(9)在1.c中:

#include <stdio.h>

static int i = 12;

int main()

{

       inti = 123;

       printf(“thei in the 1.c is %d\n”, i);

       a2();

}

       在2.c中:

extern int i;

void a2(void)

{

       printf(“thei in the 2.c is %d\n”, i);

}

结果:


可以发现如果变量被声明为static后,便无法通过extern来对之进行调用。

(10)在1.c中:

#include <stdio.h>

int main()

{

       externint i = 123;

       printf(“thei in the 1.c is %d\n”, i);

       a2();

}

       在2.c中:

extern int i;

void a2(void)

{

       printf(“thei in the 2.c is %d\n”, i);

}

结果:


可以发现函数内部的变量无法定义为extern类型。

(11)在1.c中:

#include <stdio.h>

extern int i = 1123;

int main()

{

       inti = 123;

       printf(“thei in the 1.c is %d\n”, i);

       a2();

}

       在2.c中:

extern int i;

extern int i;

void a2(void)

{

       printf(“thei in the 2.c is %d\n”, i);

}

结果:


可以发现,变量的声明可以多次声明,程序可正常运行。

(12)在1.c中:

#include <stdio.h>

extern int i = 1123;

int main()

{

       printf(“thei in the 1.c is %d\n”, i);

       a2();

printf(“the i inthe 1.c is %d\n”, i);

 

}

       在2.c中:

extern int i;

extern int i;

void a2(void)

{

       printf(“thei in the 2.c is %d\n”, i);

       i= 212;

}

结果:


看了一个百度知道的解释不错,在此附上对应网址:https://zhidao.baidu.com/question/570728680.html

 

原创粉丝点击