top of page
  • Writer's pictureMARY PRINCY R

C Programming for Embedded systems 2

Updated: Feb 1, 2023

This is the second blog from the series "C programming for embedded systems."


I expect on the readers of this blog to have a fundamental understanding of the C programming language.


Learning Objectives:

  • Local, global and static data types and their scope.

Note that the IDE used here for writing sample codes is Microsoft Visual Studio 2022.


What is scope of a variable?


The scope refers to a specific area of the program where variables and constants exist and can be accessed.

Scope defines the following things:

  • Accessibility of a variable or a constant.

  • A constant's or a variable's lifetime.

  • Reserved memory space for variables and constants.

  • Deallocation time for the reserved memory space.


Local variable and its scope:


Local variables are those declared within a function or a block. Consider the following code!

Output for the above code:

Here a and b are the local variables. They are only usable within the defined function (in this case, function1()). It will produce an error if you try to use these variables outside of the function in which they were defined. Variables a and b exist only while function1() is running. They are destroyed when function function1() ends.



The variables a and b within function1() in this instance are local to function1(), whereas the variables a and b within function2() are local to function2(). They are totally independent of one another. The value of a inside function1() won't change if the value of a inside function2() is modified.


The output for the above code is:


Global variable and its scope:


Global variables are variables defined outside of any function. They are not restricted to any specific function. Global variables can be accessed and changed by any function. At the time of declaration, global variables are automatically initialized to 0. Usually, they are written before the main() function.


Consider the code below:

The variables a and b are declared as global variables of type int. Since the variable a is not initialized, it will be automatically initialized to 0. Any function can make use of variables a and b. There are local variables with the same names as global variables inside of function function2(), as you can see from the above code.


Output:

The value of local variables a and b are printed inside the function2() function because when a dispute arises between a global variable and a local variable, the local variable always takes precedence. Global variables are not immediately removed when a function completes, in contrast to local variables. They are available to any function until the program is executing.


Static variable and its scope:


A static variable retains its value between function calls. The static variable is only initialized once; if it is not initialized, it is initialized to 0 by default.


Consider the following code:


Output:

In function1(), the variable b is declared as a static. Variable b is initialized to 100 when function1() is first called, and its value is incremented on line 24.The next time function1() is run, this updated value of b will be kept; line 22 proves it by printing the value of b and once again the value of b is incremented by 1. Similarly, when the function function() is called third time, the value of b is 102. Note that only variable b is able to retain its value because it is declared as static. However, this is not the case with variable a, which is initialized every time when function1() is called. Also, note that static variable b is initialized only once when function1() is called for the first time.


Difference between local and global scope.

Local scope

Global scope

The variables that are declared in a function's local scope are called  as local variables.

The variables that are declared in global scope (outside of the main function) are known as global variables.

These variables are only accessible in the block in which they were declared.

These variables can be accessible from anywhere inside the program .

When a program's execution exits the scope, memory is released.

When the execution of the program is complete, memory is released.

Unless otherwise specified, variables declared in the local scope are stored in the stack segment.

Variables declared inside the global scope are stored in data segment.

Keys to choosing the appropriate data type


  • Choosing the right data type reduces the size and execution time of the code.

  • If possible, use unsigned type.

  • Use the smallest possible type.

  • To ensure that the data type is as small as possible, use casts within expressions.

  • Avoid using floating point if efficiency is necessary.


Link to first part of the series C programming for embedded systems-C Programming for Embedded Systems -1 (risefromashes.blog)


Happy learning!!!!!




13 views0 comments

Recent Posts

See All
bottom of page