c语言中static的作用是什么

本篇内容介绍了“c语言中static的作用是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

1、隐藏,static修改的函数和全局变量。当加上了static,该变量就会对其他源文件隐藏。

2、保持持久性-static修改的局部变量。

3、默认初始化为0-存储在静态区域。

实例

#include <stdio.h>
 
void test(void)
{
int n = 0;
 
printf("n = %d\n", n);
n++;
printf("n++ = %d\n", n);
}
 
void test_static(void)
{
static int n = 0;
 
printf("static n = %d\n", n);
n++;
printf("static n++ = %d\n", n);
}
 
int main()  
{
test();
test_static();
 
printf("-------------------------\n");
test();
test_static();
 
    return 0;
}

“c语言中static的作用是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!