Programming language C language global variables and local variables difference - C language zero basic introductory tutorial

Keywords: C R Language

Programming language C language global variables and local variables difference - C language zero basic introductory tutorial

catalogue

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

  1. Difference between C language array subscript out of bounds and memory overflow
  2. C language uses pointers to traverse arrays
  3. Difference between C language pointer and array
  4. Difference between C language pointer array and array pointer
  5. C language field pointer
  6. C language function value transfer and address transfer
  7. C language function indefinite length parameter
  8. C language function pointer
  9. C language pointer function
  10. C language callback function callback
  11. C language #pragma once
  12. Difference between C language #include < > and #include ""
  13. C language const modifier function parameters
  14. Difference between const and define in C language
  15. C language # operators
  16. C language ## operators
  17. C language__ VA_ARGS__
  18. C language##__ VA_ARGS__
  19. C language function indefinite length parameter##__ VA_ARGS__ Classic case
  20. C language va_start macro
  21. C language va_end macro
  22. C language va_arg macro
  23. C language vprintf function
  24. C language va_start / va_end / va_arg custom printf function
  25. C language main function
  26. C language main function parameter main(int argc, char *argv [])
  27. C language exit function
  28. C language abort function
  29. C language assert function
  30. C language local variables
  31. C language global variables
  32. Differences between global variables and local variables in C language
  33. Baidu online disk search
    www.ijzcn.cn
    Search whiteness
    www.sobd.cc
    Search the code
    www.somanba.cn

Posted by direland on Sun, 26 Sep 2021 17:24:30 -0700