C: Scope

Scope

Scope C language variable is divided into:

  • Scope block (code block is a block of code between the {})
  • Function scope
  • File scope

Local variables

Local variables are also called auto Variable (auto write time to write), {} block general variables are defined inside the automatic variable, which has the following characteristics:

  • Defined within a function, the function is only valid within the range
  • Defined in the compound statement is valid only within a compound statement
  • With the declaration declares the end of the function call or the end of the local variables of the compound statement period also ends
  • If no initial value, the content is random
#include <stdio.h>

void test()
{
    //auto写不写是一样的
    //auto只能出现在{}内部
    auto int b = 10; 
}

int main(void)
{
    //b = 100; //err, 在main作用域中没有b

    if (1)
    {
        //在复合语句中定义,只在复合语句中有效
        int a = 10;
        printf("a = %d\n", a);
    }

    //a = 10; //err离开if()的复合语句,a已经不存在
    
    return 0;
}

Static (static) local variable

  • Scope of static local variables are also effective in the function definition
  • Lifecycle static local variables and to run the same period, while the value of staitc local variable is initialized only once, but can be assigned multiple times
  • If no static local variables assigned to the initial value, automatically assigned by the system: numerical variable automatic initial value 0, the character character variable nulling
#include <stdio.h>

void fun1()
{
    int i = 0;
    i++;
    printf("i = %d\n", i);
}

void fun2()
{
    //静态局部变量,没有赋值,系统赋值为0,而且只会初始化一次
    static int a;
    a++;
    printf("a = %d\n", a);
}

int main(void)
{
    fun1();
    fun1();
    fun2();
    fun2();
    
    return 0;
}

Global Variables

  • Outside the defined function, this function can be shared files and other files, other file if the function call this variable must be declared as extern
  • Life cycle global variables and running the same period
  • Global variables of different files can not be duplicated

Static (static) global variables

  • Outside the function definition, it is limited scope as defined document
  • Different file static global variables can be the same name, but do not conflict with the scope
  • Lifecycle static and global variables, like running cycle, while staitc global variable initialization value only once

extern global variable declaration

extern int a; Declare a variable that has been defined in other files, here it is just a statement, rather than a definition.

Global functions and static functions

In the C language functions are global by default, use the keyword static function can be declared as static function is defined as static means that this function can only be used in file defines this function can not be invoked in other documents, even if in other documents, the statement did not use this function.

Staitc be the same for the function name in different files.

note:

  • Allows the same variable names in different functions, they represent different objects, the different dispensing units, without disturbing each other.
  • The same source file, allowing global variables and local variables of the same name, does not function in the role of local variables in the art, the global variable.
  • All functions are global by default, meaning all functions can not be the same name, but if it is staitc function, then the scope is the file level, so different file static function name is the same.

Guess you like

Origin www.cnblogs.com/wbyixx/p/12240834.html