c ++中静态变量和正常变量之间的区别是什么? [英] what is the difference between static and normal variables in c++?

查看:132
本文介绍了c ++中静态变量和正常变量之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道差异,是初学者。

I need to know the difference,am beginner.

推荐答案

void func()
{
    static int static_var=1;
    int non_static_var=1;

    static_var++;
    non_static_var++;

    cout<<"Static="<<static_var;
    cout<<"NonStatic="<<non_static_var;
}

void main()
{
    clrscr();
    int i;
    for (i=0;i<5;i++)
    {
        func();
    }
    getch();
}

上述输出为:

Static=2
Nonstatic=2

Static=3
Nonstatic=2

Static=4
Nonstatic=2

Static=5
Nonstatic=2

Static=6
Nonstatic=2

静态变量保留其值,而非静态或动态变量初始化为'1'每次调用函数时。希望有所帮助。

Static variable retains its value while non-static or dynamic variable is initialized to '1' every time the function is called. Hope that helps.

这篇关于c ++中静态变量和正常变量之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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