Difference Between Local And Global Variable

Prashant | Thu, 11 Jun, 2020 | 118

Difference between local and Global variable

What are variable and what are identifiers to learn about this visit :- Click here

As we know that variables are the name of memory blocks which are used to store values, in this tutorial we will learn how to declare local and global variables what are their scopes in C language?

Local Variables :- 

Before learning about the local variable, we should learn about the function block and function parts. There are two parts of the function block (block means region of the function between curly braces in C)

  1. Declaration part - Region where we declare all variables which are going to be used within the function (this part starts from starting curly brace "{").
  2. Executable part - Other statements except the declarations are the executable statements.

Local variables are the variables which are declared or defined within the declaration part of the function block.

These variables have local scope to that function only, in which they are declared. They cannot be accessed outside of the function.

Global Variables

Global variables are the variables which are declared or defined below the header files inclusion section or before the main () function.

These variables have global scope to the program in which they are declared. They can be accessed or modified in any function of the program.

 

Example :- 

// Global variable
float a = 1;

void my_test() {
  // Local variable called b. 
  // This variable can't be accessed in other functions
  float b = 77;
  println(a);
  println(b);
}

void setup() {
  // Local variable called b. 
  // This variable can't be accessed in other functions.
  float b = 2;
  
  println(a);
  println(b);
  my_test();
  println(b);
}

 

 

 

 

 

 

Related Articles :- 

1Introduction And Getting Started With C

2Basic Of C Language

3. Why We Should Use C Language

4. Applications Of C Programming

5. Basic Rules For Writting C Program

6. Keywords In C Language

7. Comments In C Programming

8. Tokens In C

9. Difference Between Int Main And Void Main

10. C Processor And Macros

11. Variables In C

12. Types Of Variables In C

13. Difference Between Local And Global Variable

14. Difference Bwtween Auto / Extern / Static Variable

15. Constant In C

16. How To Access Global Variable Using Extern Keyword In C

17. Exit And Return Staterment In C

18. Print Float Value Upto N Decimals In C Programming

19. How To Print Multiline Message Using Single Printf In C Programming ?

20. What Value Returned By Scanf Function In C Language ?

21. What Value Is Returned By Printf And Scanf In C

22. What Do 'Lvalue' And 'Rvalue' Mean In C/C++?

23. Errors In C Programming ?

0 comments
Leave a comment