Microcontroller Embedded C Programming Lecture 33| Variables scope and illustration contd

 

Variables scope and illustration

 

 

In this tutorial, we will explore variable scope in C and predict the output of a sample program. Variable scope defines where a variable can be accessed and manipulated within a program.

In this article, let’s understand some more codes. Here I have another code snippet. So, I want you to predict the output of this program.

#include<stdio.h>

/*this is a global variable*/
int a;

int main()
{
   a=25;
   {
       int my_var;
       my_var = 45;
       printf("001Value of the local variable 'my_var' is %d\n", my_var);
       printf("002value of the global variable 'a' is %d\n", a);
   }
  
   int my_var;
   my_var = my_var+10;
   printf("003value of the local variable 'my_var' is %d\n", my_var);
   return 0;
}

Code snippet

Global Variable:

The code snippet begins with the declaration of a global variable int a;. Global variables are accessible throughout the program, and they retain their values across different scopes. In this case, a is initialized to 25 within the main function.

Local Variable in a Block:

Inside the main function, a block of code is defined using curly braces { }. Within this block, a local variable my_var of type int is declared and initialized to 45. Local variables are confined to the scope in which they are declared and have no significance outside of it.

Output Prediction:

  1. The first printf statement inside the block will print the value of the local variable my_var, which is 45.
  2. The second printf statement inside the block will print the value of the global variable a, which is 25.
  3. After the block, another local variable my_var is declared, but it’s not initialized. 

Once the execution control comes to the int my_var variable(line number 13) here, please note that previous int my_var variable dies. So, it loses its existence.

So, we are creating one more variable. This is perfectly fine and legal. After that, here we are adding +10 to the value of the my_var variable.

Here, let’s understand my_var = my_var +10; statement. To understand this statement, first, you should give attention to the assignment operator equal to sign(=). That is an assignment operator in ‘C.’ 

And my_var +10 is called the right-hand side expression, and my_var is called as left-hand side expression. So, the first right-hand side expression will be evaluated. Here 10 is added to the value of the my_var variable. But what is the initial value of the my_var variable? That variable is not initialized. That’s why its value will be some garbage value; you cannot predict that. So, some garbage value is +10, and that value is assigned to the my_var variable. That’s why the output of this printf will be unpredictable. So, some garbage numbers will be printed. Let’s see how it goes.

Compile this code. You can see Figure 1. It is printing 45, 25, and it is printing 10 here.

Variables scope and illustration
Figure 1. Output

 

I think this compilation took the initial value of this variable as 0. I don’t know why that is that. But typically this printf will print some garbage value.

 

So, let’s try this code in Eclipse IDE. Let’s compile this code. The code is compiled.

Figure 3. Test the code in Eclipse IDE
Figure 2. Test the code in Eclipse IDE

 

Compiler Warning:

Here we can see that this compiler throws one warning(Figure 2). That is, ‘my_var‘ is used uninitialized in this function. The compiler is issuing a warning saying before using this my_var variable(line 20), so you didn’t initialize it. That’s a very important warning that compiler issues, and you should pay attention to that. So, for this trivial example, that’s OK. But warnings like these are very very dangerous.  

 

Now, let’s execute this code. Go to Run as, and go to Local C/C++ application.

Variables scope and illustration
Figure 3. Executing the program

 

Figure 5. Output
Figure 4. Output

 

Here, the my_var variable is printing some garbage value. So, that’s because this is a local variable, and the default value of that variable can’t be predictable. 

 

Get microcontroller Embedded C Programming Full Course on Here.

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.