Microcontroller Embedded C Programming Lecture 34| Summary of local and global variables

  • Post author:
  • Post category:Blog

 

Summary of local and global variables

 

 

In the previous article, we discussed the variable scopes. Let’s see the Summary of local and global variables.

Local and global variables are concepts in programming that refer to the scope and accessibility of variables within a program. They determine where a variable can be accessed and modified, as well as its lifetime.

 

Global Variables

Scope→ Global variables are visible to all the functions of a program. They are everywhere. Even you can access global variables from another file of the project. 

Default value All uninitialized global variables will have 0 as the default value.

Lifetime The lifetime of these variables is till the end of the execution of the program. 

  • Global variables are declared at the top level of a program, outside of any specific functions or blocks.
  • They are accessible from any part of the program, including functions, loops, and other blocks.
  • Global variables are often used for data that needs to be shared and accessed across multiple functions or modules.
  • They have a longer lifetime compared to local variables; they are created when the program starts and persist until the program terminates.
  • Care should be taken when using global variables, as they can lead to unintended side effects and make code harder to understand and maintain. Changes to global variables can affect the behavior of various parts of the program.

 

 

Local Variables

Scope Within the body of the function. Local variables lose existence once the execution control comes out of the function body.

Default value unpredictable(garbage value). 

Lifetime  Till the end of the execution of a function in which a variable is defined. 

  • Local variables are declared within specific code blocks like functions, loops, or conditionals..
  • They are only accessible within the block where they are declared. Outside of that block, they have no meaning and cannot be accessed.
  • Local variables are typically used to store temporary data or intermediate results within a specific scope.
  • They have a limited lifetime; they are created when the block is entered and destroyed when the block is exited.
  • Local variables can have the same name as variables in other blocks without causing conflicts because they exist in different scopes.

 

In summary, local variables are confined to a specific block of code and have limited scope and lifetime, while global variables are accessible throughout the entire program and have a longer scope and lifetime. It’s generally considered good practice to use local variables whenever possible to encapsulate data within specific contexts and reduce the potential for naming conflicts and unintended consequences. Global variables should be used sparingly and only when there’s a genuine need for data to be shared across different parts of the program.

 

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.

Leave a Reply