什么是静态变量?举个例子吗? [英] what is static varible?give me example?

查看:119
本文介绍了什么是静态变量?举个例子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是静态变量?给我举个例子吗?

what is static varible?give me example?

推荐答案

C中有两种类型的静态变量:
全局静态变量在任何函数的主体外部声明,并且仅在该源文件中可见-在其外部看不到.

局部静态变量在函数内部声明,并将在后续的函数调用中保留其值:
There are two types of static variable in C:
A global static variable is declared outside the body of any function, and is visible only within that source file - it cannot be seen outside it.

A local static variable is declared inside a function, and will retain it''s value across sucessive function calls:
void myfunc()
   {
   static int i = 1;
   printf("%d\n", i);
   i += 1;
   }
void otherFunc()
   {
   myFunc();
   myFunc();
   }

将打印:

1
2

局部静态变量在声明该函数的函数之外仍然不可用.

A local static variable is still unavailable outside the function it is declared within.


这应该提供您需要的所有信息:

http://programmingexamples.wikidot.com/c-static-variables [
This should provide all the information you need:

http://programmingexamples.wikidot.com/c-static-variables[^]


这篇关于什么是静态变量?举个例子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆