A little about scope in C

in #scope7 years ago

Local and global variables

Local variables are variables which exists as long as the code block you are in is being executed. When this same code block ends, these local variables are removed from memory as well. That means, they cant be acessed beyond this code block, or in another code block where they have been created.


int main()
{
int a = 25;
{
int b = 50;
}
a = 17; --------> variable a is inside this code block, so it can be acesses or modified
b = 58; --------> variable b cannot be acessed or modified in this scope, since the code block it was declared is closed and no longer exists

return 0;

}


Our local scope apply to functions as well. As we have seen, functions are blocks of code, which can be called from any point in the program, as well as receive parameters and return values, like our main() function which we have seen since the beggining. Altough our functions may return values which are declared in local scope, the returned value will be stored in a memory area where the code is running currently. The memory area created to store the variable when the function is running will not hold it anymore when the function reached its end by the end of the code block, or by reaching the return instruction. Lets see an example to ilustrate well how it works:

img1.jpg

If you must work with this same memory area when passing or returning arguments from a function, like in situations we will see very soon, you may pass or return the values using pointers. Lets make a little experiment and explain it right below our image:

img2.jpg

In this case, what could this bogus value which we received after the return of the function? It was 81 inside, but what it means this crazy value in the last line of our program? In this case, it could be actually anything, and it show us something important. While the function is running, it has stored the value 81 inside it, and after the return, we can still take a peek at what is inside this very same memory area, but since the function has reached its end, it doesnt store any particular value anymore.

Now lets try another example passing values as pointers and altering them inside functions to see what happens to understando how local scope works inside functions:


#include <stdio.h>

void func();
void func2();

int main()
{

int v1 = 20;
int v2 = 15;
int *pv = &v2;

printf("variable v2 value memory area %d \n", pv);
printf("value inside the memory area %d of v2 \n", *pv);

func(v1);
func2(&v2);

printf("v1 now contains the value %d \n", v1);
printf("v2 now contains the value %d \n", v2);

return 0;

}

void func(int v1) {
printf("==========inside function=============\n");
printf("Received as parameter the value %d \n", v1);
printf("Changing value");
v1 = 155;
printf("v1 inside function contains the value %d \n", v1);
printf("======================================\n");

}
void func2(int *pv) {

printf("==========inside function2=============\n");
printf("Received as parameter the memory area %d \n", pv);
printf("pv contains the value %d \n", *pv);
printf("Changing value \n");
*pv = 155;
printf("======================================\n");

}


This is what we get from the code above:

img3.jpg

Important to note is the concept of passing variables by values and by reference. When we pass the variable v1 to func(), we are passing by value. That means we create a local copy of this variable inside the function func(), and this variable, mantains no relationship with the variable v1 declared outside the function scope. So if we change it inside function scope, we will be altering this copy created, which will cease to exist when the function ends or return(in this case when the function ends, since it was declared with the signature void, meaning it doesnt need a return, it can only execute code inside function). Our original variable v1 will not be altered in any way then, since we just changed the value of a copy inside func(). If our function receive as parameter a pointer, we will be altering the memory area directly even if we are inside a function. That means the alterations we do to the pointer inside function or local scope, will remain when the scope reach its end.

Global variables are available from the beggining to the end of the program. That means they occupy the memory as long as the program is running. To declare a global variable, all you have to do is declare them outside any code block:


int x = 53;
int y = 36;
int main()
{
{
x = 11;
}
y= 13;
return 0;
}


Since global variables, if not specified, can be freely altered, if you make a change in them, in any scope of the code, all the scopes in your code will be susceptible to this change.

Coin Marketplace

STEEM 0.17
TRX 0.15
JST 0.028
BTC 57495.98
ETH 2320.95
USDT 1.00
SBD 2.35