Firefly开源社区

打印 上一主题 下一主题

[应用开发] 全局变量和局部变量

4

积分

0

威望

0

贡献

吃瓜的群众

积分
4

全局变量和局部变量

发表于 2022-7-12 11:02:29      浏览:4030 | 回复:0        打印      只看该作者   [复制链接] 楼主
1.全局变量(也叫外部变量)分为外部全局变量(extern修饰)和静态全局变量(static修饰)
2.静态全局变量(static修饰)------只能在被定义在的源程序使用,而外部全局变量(extern修饰)既可在被定义在的源程序使用,也可以在别的源程序的函数引用
3.在函数前定义过全局变量,那在这个函数里面使用时,可以不说明,通常要在函数前做全局变量做说明
例如:
#include<stdio.h>

int a = 0;
void print()
{
   
    printf("global variable a=%d\n",a);
}
int main()
{
   
    print();

    return 0;
}
全局变量a在print()函数前有定义,所以在print()函数中使用无需说明,但下面的代码将会运行出错。

#include<stdio.h>

void print()
{
   
    printf("global variable a=%d\n",a);
}
int a = 0;//全局变量没有在print()前声明
int main()
{
   
    print();

    return 0;
}
可以在print()函数里面的printf()语句前加一个 extern int a;


4.局部变量是相对于全局变量来说的,局部变量就是定义在函数体里面的变量。当然,由于形参相当于函数中定义的变量,所以形参也属于一种局部变量。













https://m.vk.com/public212725457
https://m.vk.com/public212730424
https://m.vk.com/public212730574
https://m.vk.com/public212730616
https://m.vk.com/public212745225
https://m.vk.com/public212746092
https://m.vk.com/public212754170
https://m.vk.com/public212754213
https://m.vk.com/public212754471
https://m.vk.com/public212754531
https://m.vk.com/public212769260
https://m.vk.com/public212769540
https://m.vk.com/public212789644
https://m.vk.com/public212789982
https://m.vk.com/public212790084
https://m.vk.com/public212790258
https://m.vk.com/public212811877
https://m.vk.com/public212812193
https://m.vk.com/public212834815
https://m.vk.com/public212834919
https://m.vk.com/public212835081
https://m.vk.com/public212835522
https://m.vk.com/public212862497
https://m.vk.com/public212862572
https://m.vk.com/public212862608
https://m.vk.com/public212878952
https://m.vk.com/public212890225
https://m.vk.com/public212890303
https://m.vk.com/public212910654
https://m.vk.com/public212910847
https://m.vk.com/product-212910847_6842462
https://m.vk.com/uslugi-21291084 ... 847_6842462%2Fquery
https://m.vk.com/public212931875
https://m.vk.com/public212931996
https://m.vk.com/public212932447
https://m.vk.com/public212932731
https://m.vk.com/public212725187
https://m.vk.com/public212730510
https://dribbble.com/xabzs
https://dribbble.com/shots/18334578-bz
https://dribbble.com/dgbjzs
https://dribbble.com/shots/18334830-1
https://dribbble.com/njbzs
https://dribbble.com/shots/18352001-1
https://dribbble.com/ncbzgs
https://dribbble.com/shots/18352286-bz
https://dribbble.com/szbzfw
https://dribbble.com/shots/18518953-bz
https://dribbble.com/kmbjzs
https://dribbble.com/shots/18519005-1
https://dribbble.com/whbzs
https://dribbble.com/shots/18567245-1
https://dribbble.com/zzbzs
https://dribbble.com/shots/18567397-bz
https://dribbble.com/csbjz
https://dribbble.com/shots/18582636-1
https://dribbble.com/jnbjz
https://dribbble.com/shots/18582783-1
https://dribbble.com/fzbzfw
https://dribbble.com/shots/18584849-bz
https://dribbble.com/shzhbjz
https://dribbble.com/shots/18585318-1
https://dribbble.com/gzbzgs
https://dribbble.com/shots/18591493-1
https://dribbble.com/sjzbjz
https://dribbble.com/shots/18591645-bz
https://dribbble.com/hdbjz
https://dribbble.com/shots/18602951-1
https://dribbble.com/cdbzs
https://dribbble.com/cqbzs
https://dribbble.com/shots/18669060-1
https://dribbble.com/hzbzs
https://dribbble.com/shots/18669240-bz
https://dribbble.com/shbjzfw
https://dribbble.com/shots/18692319-1
https://dribbble.com/tjbjz
https://dribbble.com/shots/18693341-bz

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

友情链接 : 爱板网 电子发烧友论坛 云汉电子社区 粤ICP备14022046号-2
快速回复 返回顶部 返回列表