search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

C scoping rules

Collection 205
Read 91674
update time 2016-09-11

In any kind of programming, the scope is the area where the variables defined in the program exist. Variables cannot be accessed beyond this area. There are three places in C language where variables can be declared:

  1. Inside a function or block Local Variables

  2. At all Global variables outside the function

  3. In the function parameter definition of the form parameter

let Let's take a look at what local variables, global variables and form parameters are.

Local variables

Variables declared inside a function or block are called local variables. They can only be used by statements inside the function or block of code. Local variables are not known outside the function. The following is an example of using local variables. Here, all variables a, b and c are local variables of main() function.

#include <stdio.h> int main (){  /* 局部变量声明 */  int a, b;  int c; 
  /* 实际初始化 */
  a = 10;
  b = 20;
  c = a + b;
 
  printf ("value of a = %d, b = %d and c = %d\n", a, b, c); 
  return 0;}

Global variables

Global variables are defined outside the function, usually at the top of the program. Global variables are valid throughout the program life cycle and can be accessed within any function.

Global variables can be accessed by any function. In other words, global variables are available throughout the program after they are declared. The following are examples of using global variables and local variables:

#include <stdio.h> /* 全局变量声明 */int g; int main (){  /* 局部变量声明 */  int a, b; 
  /* 实际初始化 */
  a = 10;
  b = 20;
  g = a + b;
 
  printf ("value of a = %d, b = %d and g = %d\n", a, b, g); 
  return 0;}

In a program, the names of local variables and global variables can be the same, but within a function, the value of the local variable will override the value of the global variable. The following is an example:

#include <stdio.h> /* 全局变量声明 */int g = 20; int main (){  /* 局部变量声明 */  int g = 10;
 
  printf ("value of g = %d\n",  g); 
  return 0;}

When the above code is compiled and executed, it will produce the following results:

value of g = 10

Formal parameters

Parameters of the function, formal parameters, Treated as local variables within the function, they will override global variables first. The following is an example:

#include <stdio.h> /* 全局变量声明 */int a = 20; int main (){  /* 在主函数中的局部变量声明 */  int a = 10;  int b = 20;  int c = 0;  int sum(int, int);

  printf ("value of a in main() = %d\n",  a);
  c = sum( a, b);
  printf ("value of c in main() = %d\n",  c);  return 0;}/* 添加两个整数的函数 */int sum(int a, int b){
    printf ("value of a in sum() = %d\n",  a);
    printf ("value of b in sum() = %d\n",  b);    return a + b;}

When the above code is compiled and executed, it will produce the following results:

value of a in main() = 10value of a in sum() = 10value of b in sum() = 20value of c in main() = 30

Initialize local variables and global variables

When local variables When defined, the system does not initialize it, you must initialize it yourself. When defining a global variable, the system will automatically initialize it as follows:

##pointerNULL
Data typeInitialization default value
int0
char'\0'
float0
double0
It is a good programming practice to initialize variables correctly, otherwise sometimes the program may produce unexpected results because uninitialized variables lead to some garbage values ​​that are already available in the memory location .

Hot AI Tools
Undress AI Tool
Undress AI Tool

Undress images for free

AI Clothes Remover
AI Clothes Remover

Online AI tool for removing clothes from photos.

Undresser.AI Undress
Undresser.AI Undress

AI-powered app for creating realistic nude photos

ArtGPT
ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT
Stock Market GPT

AI powered investment research for smarter decisions

Popular tool
Notepad++7.3.1
Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version
SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1
Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6
Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version
SublimeText3 Mac version

God-level code editing software (SublimeText3)