Programming language C language global variables and local variables difference - C language zero basic introductory tutorial
catalogue
- 1, Introduction to global and local variables
- 2, Considerations for global and local variables
- 3, Guess you like it
Zero foundation C/C + + learning route recommendation: C/C + + Learning directory >> Introduction to basic C language
1, Introduction to global and local variables
1. Introduction to global variables
At all function Externally defined variables are called Global Variable , its scope is valid from the location of the defined variable to the end of the source file by default.
int a, b; //global variable void func1(){ int c, d; //local variable //TODO: } int main(){ int e, f; //local variable //TODO: return 0; }
2. Introduction to local variables
Variables defined inside a function are called local variable (Local Variable), whose scope is limited to the inside of the function, leaving the function function If you use it again, you will report an error.
int f1(int a){ int b,c; //a. B, C are valid only in function f1() return a+b+c; } int main(){ int m,n; //m. N is valid only within the function main() return 0; }
2, Considerations for global and local variables
1.local variable (Local Variable) is also called internal variable, and its scope is limited to the function. It is illegal to use this variable after leaving the function;
2.Global Variable , its scope is valid from the location where the variable is defined to the end of the source file by default, for example:
/******************************************************************************************/ //@Author: ape programming //@Blog (personal blog address): www.codersrc.com //@File:C language tutorial - the difference between global variables and local variables in C language //@Time:2021/07/18 07:30 //@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly! /******************************************************************************************/ #include void func1(){ x += 10; y += 20; printf("function:%s in x = %d y = %d \n",__FUNCTION__,x,y); } int x = 10; int y = 20; void func2(){ x += 10; y += 20; printf("function:%s in x = %d y = %d \n",__FUNCTION__,x,y); } int main(){ func1(); func2(); printf("function:%s in x = %d y = %d \n",__FUNCTION__,x,y); return 0; } /* Output: main.cpp: In function 'void func1()': main.cpp:6:5: error: 'x' was not declared in this scope 6 | x += 10; | ^ main.cpp:7:2: error: 'y' was not declared in this scope 7 | y += 20; | ^ */
3. If the local variable name is the same as the global variable name, the default operation inside the function is the local variable, for example:
/******************************************************************************************/ //@Author: ape programming //@Blog (personal blog address): www.codersrc.com //@File:C language tutorial - the difference between global variables and local variables in C language //@Time:2021/07/18 07:30 //@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly! /******************************************************************************************/ #include int x = 10; //global variable int y = 20; //global variable void func1(){ int x = 8; //Local variable (same name as global variable) y += 20; //global variable printf("function:%s in x = %d y = %d \n",__FUNCTION__,x,y); } void func2(){ int x = 5; //Local variable (same name as global variable) y += 20; //global variable printf("function:%s in x = %d y = %d \n",__FUNCTION__,x,y); } int main(){ func1(); func2(); printf("function:%s in x = %d y = %d \n",__FUNCTION__,x,y); return 0; } /* Output: Function: x = 8 y = 40 in func1 Function: x = 5 y = 60 in func2 Function: x = 10 y = 60 in main */
3, Guess you like it
- Difference between C language array subscript out of bounds and memory overflow
- C language uses pointers to traverse arrays
- Difference between C language pointer and array
- Difference between C language pointer array and array pointer
- C language field pointer
- C language function value transfer and address transfer
- C language function indefinite length parameter
- C language function pointer
- C language pointer function
- C language callback function callback
- C language #pragma once
- Difference between C language #include < > and #include ""
- C language const modifier function parameters
- Difference between const and define in C language
- C language # operators
- C language ## operators
- C language__ VA_ARGS__
- C language##__ VA_ARGS__
- C language function indefinite length parameter##__ VA_ARGS__ Classic case
- C language va_start macro
- C language va_end macro
- C language va_arg macro
- C language vprintf function
- C language va_start / va_end / va_arg custom printf function
- C language main function
- C language main function parameter main(int argc, char *argv [])
- C language exit function
- C language abort function
- C language assert function
- C language local variables
- C language global variables
- Differences between global variables and local variables in C language
-
Baidu online disk search www.ijzcn.cn Search whiteness www.sobd.cc Search the code www.somanba.cn