11.C language variable scope

First, the scope of variables

Scope is defined in the presence of variables in the program (or active) area over the variable region can not be accessed. C
language There are four local variables can be defined.

1) defined in all of the external function is a global variable.

2) defined in the header file is a global variable.

3) inside the function block definition statement or local variables.

4) the parameters of the function is a function of local variables.

By way of explanation, the definition of the function declarations and function of the two concepts, however, declare and define variables variables is the same concept or definition of variables and variable declaration also, depending upon the expression used to programmers.

Second, global variables

Global variables are defined in the external function, usually at top of the program (may be elsewhere). Global variables throughout the application life cycle are valid, can be accessed at any function after a defined position.

Global variables in the main program exits to recover memory space by the system.

Example (book37.c)

/*
 * 程序名:book37.c,此程序用于演示全局变量的作用域
 * 作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h> 

double pi=3.141592; // 全局变量声明

void func1(); // 声明一个自定义函数

int main() 
{ 
  printf("位置一 pi=%f\n",pi);
  func1();
  printf("位置二 pi=%f\n",pi);
}

void func1()
{
  printf("位置三 pi=%f\n",pi);
  pi=3.141593;  // 改变pi的值
  printf("位置四 pi=%f\n",pi);
}

In the above code, pi as a global variable defined in main () and func1 () outside, located at the top of the program, the main () and func1 () function can access it.

running result

Here Insert Picture Description

Third, local variables

Variable declared inside a function or a block of statements are known as local variables, they can only be used in the function block statement or statements inside.

Local variables or external function statement blocks are unavailable.

When the function returns the local variables or the end of a block of statements to recover the system memory.

Fourth, the parameters of the function

And the parameter is treated as local variables in the function.

I suggest that re-read the "C language function" "Nine, transfer function parameters" section.

V. Notes

Name of local and global variables can be the same, within a statement or function blocks, if the local variable names with the same name as a global variable, it will shield the global variables local variables.

Example (book39.c)

/*
 * 程序名:book39.c,此程序演示局部变量与全局变量重名的情况.
 * 作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h> 

double pi=3.141592; // 全局变量声明

void func1(); // 声明一个自定义函数

int main() 
{ 
  printf("位置一 pi=%f\n",pi);
  func1();
  printf("位置二 pi=%f\n",pi);
}

void func1()
{
  double pi=3.141593; // 全局变量声明
  printf("位置三 pi=%f\n",pi);
}

In the above code defines global variables pi, in func1 () function, the definition of local variables pi, func1 () function when running, it uses its own local variables pi, do not use global variables pi, so the main a second output value function of the position pi is still 3.141592.

running result

Here Insert Picture Description

Sixth, after-school job

Write the sample program, this section describes the knowledge of all the demo again, the demo program can deepen your understanding and mapping.

Seven, copyright notice

C Language Technology Network original article, reproduced please indicate the source link to the article, the author and original.
Source: C Language Technology Network (www.freecplus.net)
Author: Ethics code Agriculture

Published 29 original articles · won praise 2 · Views 685

Guess you like

Origin blog.csdn.net/m0_45133894/article/details/104635325