 we are going to see the scope of variables in C. Learning outcome at the end of this session, students will be able to explain the block scope, function scope and the program scope of variable. Now, first we see the scope of variables. In C, all the constants and variables have a defined scope. Scope means the accessibility and the visibility of the variables at the different points in a program. A variable in C has different types of scope, block scope, function scope and program scope. Now, first we see the block scope. As we know, a statement block is a group of statements enclosed within opening and closing curly brackets. If a variable is declared within a statement block, then as soon as the control exits from that block, the variable will ease to exist. Such a variable also known as a local variable and is said to have a block scope. In a program, the block of statements may be placed one after the other. Such blocks that are placed at the same level are known as parallel statement blocks. However, a program may also contain nested blocks like a while loop inside main function. Suppose, for example, if an integer x is declared and initialized in a main redeclared and reinitialized in a while loop, then the variable x will occupy the different memory location and will consider as a different variables. Consider this example. In this example, first we declare the main function. In main function, we create the two variables. Here, next we assign the value 20 to the x variable and value 30 to the y variable. And we display only the values of x and y on the screen using the printed statement. So, here the scope of that x and y variable is within only that particular block. So, here x and y are the local variables. Now, we see another example. Here we declare the one function, fun one. And here we declare one variable of type integer. And in that variable, we store the value 4. And we display that variable on the screen using printed statement. And we close that function. Next, we declare the main function. In main function, we create one local variable x of type integer. And we assign the value 10 to that variable. Here we observe that. Here we opening again a curly brace. In curly brace, again we declare the variable x equal to 5. And we assign the 5 value to that x variable. And we display that value on the screen. And we close that block. After closing that block, again we use the printed statement for displaying the value of x. And then we call the function in the main function. So, here it displays the value 10 and 4. So, whatever the variable declared in the function 1, the scope is only within that block. We cannot access that block, that variable outside the function 1. Here, the main function, we declare the first integer x variable. And the scope of that variable is throughout the main function. So, here it displays the output 10. And whatever we display the again integer x variable equal to 5 value. And the scope within that only opening and closing brackets. So, when the control passes to the first statement, printed statement, it displays the value 5. After that, it displays the value 10. And after that, it displays the value 4. Now, next we see the example, local variable. We see one example. Here, we declare the main function. In main function, we declare the two variables of integer type. Integer x equal to 10 and integer i. And i value, we assign to the 0. Here, we use the printed statement. And in printed statement, we display the value of x. After that, we use the while loop. In while loop, we specify the condition, if i is less than 3, then, let's go inside the loop. In while loop, we specify integer x equal to i. Here, we assign the value of i to the x variable. After that, we use the printed statement for displaying the value. And after that, we incremented i value. And we close the while loop. Next, we use the again printed statement for displaying the message. And we close that main function. So, here, what happens? First, x value is initialized to 10. Then, i value is initialized to 0. So, here, we use the printed statement. It displays the value 10 on the screen. After that, we use the while loop. Here, while loop, we specify the condition, i less than 3. Here, initially, i is the 0. 0 is less than 3. So, control goes inside the loop. Here, we assign the value of i to the x variable. So, here, x stores the value 0. After that, we display the value of x. So, here, we assign the value 0 to the x variable. So, that's why, here, it displays the output 0 on the screen. Here, we increment the i value. i value becomes 1. Again, control goes in the while loop. Here, value of i is 1. 1 less than 3, yes. Condition becomes true. Again, control goes inside the loop. Here, again, we assign the value of i to the x. Here, x value becomes 1. We display that value on the screen. Here, i value becomes 2. 2 less than 3, yes. Condition becomes true. Again, control goes inside the loop. Here, we assign the value of i to the x variable. It displays the output on the screen 2. Again, i value becomes 3. Then, control comes to the condition. Here, 3 less than 3, condition becomes fall. Control comes out of the loop and it displays the value of x outside the while loop is 10. So, with that file, by clicking on the build button, it shows the zero errors, zero warnings. After that, we run that file by clicking on the run. It shows the output. The value of x outside the while loop is 10. After that, it displays the output. The value of x inside the while loop is 0. Then, it displays the value of x inside the while loop is 1. Then, it displays the value of inside the while loop is 2. And the last display the value 10. In this way, the local variables is used in the program. Now, next we see the function scope. Function scope indicates that variable is active and it is visible from beginning to the end of the function. In C, only the go to label has a function scope. Means, it is applicable only with the go to label names. This means that there should not be the same label name inside a function. Now, here we see an example that demonstrate the function scope. In this example, the label loop is visible from the beginning to the end of the main function. And therefore, there should not be more than one label having the same name within the main function. Now, next we see the program scope. If we want a function to access some variables which are not passed to it as the arguments, then declare those variables outside any function block. Such variables are known as global variables and access from any point in the program. Now, next we see the lifetime of the global variables. Global variables are created at the beginning of program execution and it remains in existence throughout the execution of the program. Global variables are not limited to a particular function. Now, next we see the place of declaration where we declare the global variables. The global variables are declared outside all the functions including the main function and it is always recommended to declare them on top of the program code. Now, next we see the name conflict. Suppose, if we have a variable declared in a function that has the same name as that of the global variable, then the function will use the local variable declared within it and it ignores the global variable. However, programmer must not use the names of the global variables as the name of the local variables as this may lead to the confusion. Now, next we see a simple example of the global variable. Now, this is in this program. We declare the one global variable of type integer. Here we specify integer x equal to 10. This is a global variable. Next we define one function fun1 and in that we declare the one variable x of type integer and we assign the value 5 to that variable and we display the value of that x on the screen using printer statement and then we close the function. Then in main function we again use the printer statement for displaying the value of x and here we call the function. Then here what happens? When the control comes in the main function, the first printer statement displays the value 10 because it refers to the global variable because the scope of that variable is throughout the program and when the function one is executed, it displays the value 5 on the screen. Now, next we see one more example of global variable. This example we declare the one global variable x of type integer. We assign that value 10 to that variable. Next we declare the function display for displaying. Next in main function we use the printf statement for displaying the value of x. Next we declare one local variable. Here we observe that the name of that local variable is same as that to the global variable and we assign the value 2 to that local variable. Here we use the printf statement for displaying the again the value of x and here we call the display function. Next we close the main function and we define the display function. Here we use the printf statement for displaying the output. Then we compile that file by using by clicking on the build button. It shows the 0 errors, 0 warnings. After that we click on the run for running that file. It shows the output the value of x in the main function is 10. Then it shows the output the value of local variable x in the main function is 2. Then it shows the value of x in the display function is 10. In this way, the scope of the global variable is throughout the program. You state whether the local variable overrides the value of the global variables. Pause this video and think about it. The local variable always overrides the values of the global variables. This statement is true. There is one more question. Filling the plans. Dash variables can be accessed from all the functions in the program. Pause this video and think about it. Global variables can be accessed from all the functions in the program. These are the references. Thank you.