zoukankan      html  css  js  c++  java
  • C语言 全局变量、静态全局变量、局部变量、静态局部变量

    
    
     1 //test.c
     2 
     3 #include <stdio.h>
     4 extern int global_var;
     5 
     6 void test_global_var()
     7 {
     8     global_var++;
     9     printf("global_var = %d
    ", global_var);
    10 }
    
    
    
     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <stdlib.h>
     4 
     5 void test_static_local_variable();
     6 
     7 int global_var = 1;                                //普通全局变量,随着整个程序的结束而消亡。可以在整个程序方法问
     8                                                 //可以在其他.c文件中访问
     9 static int static_global_var = 1;                //静态全局变量,限定只能在本文件内部访问
    10 
    11 int main(int argc, char** argv)
    12 {
    13     int a = 3;                                    //普通局部变量,只能在main函数内部使用,随着main函数的结束而消亡
    14 
    15     for (int i = 0; i < a; i++)                    //复合语句中定义,随着for循环的结束而消亡
    16     {
    17         printf("i = %d
    ", i);
    18     }
    19 
    20     test_static_local_variable();                //local_var = 1
    21     test_static_local_variable();                //local_var = 2
    22     test_static_local_variable();                //local_var = 3
    23 
    24     printf("global_var = %d
    ", global_var);    //global_var = 1
    25     test_global_var();                            //global_var = 2
    26     test_global_var();                            //global_var = 3
    27 
    28     system("pause");
    29     return 0;
    30 }
    31 
    32 void test_static_local_variable()
    33 {
    34     static int local_var = 0;            //静态局部变量,只能在函数test_static_local_variable内部使用
    35                                         //生命周期为整个程序,随着程序的结束而消亡
    36     local_var++;
    37     printf("local_var = %d
    ", local_var);
    38 }

       

  • 相关阅读:
    关于Python Package下的Module import方式[转]
    go指针的一个小坑
    不错的python中级电子书
    virtualenv 安装及使用[转]
    Go语言的传参和传引用[转]
    gorename: easy refactoring tool for Golang[转]
    Python上下文管理器的使用
    Python使用DB-API操作MySQL数据库
    Python类的定义、方法和属性使用
    tomcat配置通过域名访问项目
  • 原文地址:https://www.cnblogs.com/blackteeth/p/10354877.html
Copyright © 2011-2022 走看看