static改变了变量的作用域,在全局变量中使用时,会锁定该全局变量,使其只能在自己所在的源文件使用。
如下,若不加static,全局变量在不同源文件间可通用。
如下,如果加static,全局变量便不可通用
而在局部变量中,static使局部变量的生命周期变长。
即在使用完一次局部变量后不进行销毁。
如不加static,结果输出五个a=2
如加static,结果输出a=2到6
static改变了变量的作用域,在全局变量中使用时,会锁定该全局变量,使其只能在自己所在的源文件使用。
如下,若不加static,全局变量在不同源文件间可通用。
如下,如果加static,全局变量便不可通用
而在局部变量中,static使局部变量的生命周期变长。
即在使用完一次局部变量后不进行销毁。
如不加static,结果输出五个a=2
如加static,结果输出a=2到6
下一篇:extern的作用
举报文章
请选择举报类型
补充说明
0/200
上传截图
格式支持JPEG/PNG/JPG,图片不超过1.9M