C C++学习记录

复制代码

#include <stdio.h> /* 函数声明 */ void func1(void); static int count=10; /* 全局变量 - static 是默认的 */ int main() { while (count--) { func1(); } return 0; } void func1(void) { /* 'thingy' 是 'func1' 的局部变量 - 只初始化一次 * 每次调用函数 'func1' 'thingy' 值不会被重置。 */ static int thingy=5; thingy++; printf(" thingy 为 %d , count 为 %d\n", thingy, count); }
/* 输出 */
 thingy  6  count  9
 thingy  7  count  8
 thingy  8  count  7
 thingy  9  count  6
 thingy  10  count  5
 thingy  11  count  4
 thingy  12  count  3
 thingy  13  count  2
 thingy  14  count  1
 thingy  15  count  0
 
复制代码

编译器会在执行main函数的时候直接在内存中定义和初始化静态变量 之后发现内存位置不改就不再初始化了 也就是这句作废

现在突然感觉到了和脚本语言的差距.......

https://blog.csdn.net/luckyaslan/article/details/17575407

posted @   RicardoMYun  阅读(22)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示