Scope of Variables in C: C variable scope is a region of the program where a defined variable can have existence and beyond the variable, where it can access. And there are three areas where the variables can be declared. They are as follows:
- Inside the function or a block which is called local variables.
- Outside of all functions which are called global variables.
- While the definition of function parameters are called formal parameters.
Scope of Variables in C
And in the C language, every variable defined in the scope. And you can also define scope as a section or region of a program where a variable has its existence.
Type | Place declared |
---|---|
local variables | Inside a function or a block |
Global variables | Out of all functions |
formal parameters | In the function parameters |
Let us know briefly about the Scope of Variables in C such as local, global variables and formal parameters.
Scope of C Local Variables
The scope of C Local variables are nothing but, the variables which are declared within the function block and can be used only within functions. While these can be used only by statements that are inside the function or a block of code. And these local scope variables are declared within braces { }.
Example
#include<stdio.h> void test(); int main() { int m = 22,n = 44;//m, n are local variables of the main function printf("\nvalues : m = %d and n = %d", m, n); test(); } void test() { int a=50,b=80;//a, b are local variables of a test function printf("\nvalues : a = %d and b = %d", a,b); }
values : m = 22 and n = 44
values : a = 50 and b = 80
Scope of C Global Variables
The scope of C variable global will be present throughout the program. While these variables can be accessed throughout the program.
(or)
Variables which are declared outside a function block and can be accessed inside the function are called Global variables. All these variables are declared outside the main function. So, this variable is visible to the main function and all other subdomains.
Example
#include<stdio.h> void test(); int m=22,n=44; int a=50,b=80; int main() { printf("All variables are accessed from main function"); printf("\nvalues: m=%d:n=%d:a=%d:b=%d", m,n,a,b); test(); } void test() { printf("\nAll variables are accessed from" \" test function"); printf("\nvalues: m=%d:n=%d:a=%d:b=%d", m,n,a,b); }
Output
All variables are accessed from the main function
values : m = 22 : n = 44 : a = 50 : b = 80
All variables are accessed from test function
values : m = 22 : n = 44 : a = 50 : b = 80
Scope of C Formal Parameters
The C formal parameters are treated as local variables with-in-a fucntion and they will take the precedence over global variables.
Example
#include<stdio.h> int a =20;/*global variables declaration*/ int main() { /*local variables definition and declaration */ int a =10; int b=20; int c=30; printf("value of a in main( )=%d\n ", a); c = sum(a,b); printf("value of c in main( )=%d\n",c); return 0;} int sum(int a,int b) { printf("value of a in sum( )= %d\n",a); printf("value of b in sum( )=%d\n",a); return a+b; }
Output
value of a in main( )=10
value of a in sum( )=10
value of b in main( )=30
value of b in sum( )=20
Environment Variables in C
These are the variables which are available for all the C applications and programs. And we can access these variables from anywhere in the program without declaring and initializing in an application or C program. And finally, the inbuilt functions which are used to access, modify and set these environment variables are called environment functions in C.
There are 3 main functions which are used to access, modify and assign an environment variable in C. They are as follows:
- setenv( )
- getenv( )
- putenv( )
1. setenv( )
This function sets the value of an environment variable. For example, assume that the environment variable “File” is to be assigned “/usr/bin/example.c”
Example
#include <stdio.h> #include <stdlib.h> int main() { setenv("FILE", "/usr/bin/example.c",50); printf("File = %s\n", getenv("FILE")); return 0; }
Output: File = /usr/bin/example.c
2. getenv( )
While it gives the current value of the environment variable. Let us assume that environment variable DIR is assigned to “/usr/bin/test/”.
Example
#include <stdio.h> #include <stdlib.h> int main() { printf("Directory = %s\n",getenv("DIR")); return 0; }
Output: /usr/bin/test/
3. putenv( )
This modifies the environment variable. And for clear idea check the example listed below.
Example
#include <stdio.h> #include <stdlib.h> int main() { setenv("DIR", "/usr/bin/example/",50); printf("Directory name before modifying="\"%s\n", getenv("DIR")); putenv("DIR=/usr/home/"); printf("Directory name after modifying="\"%s\n", getenv("DIR")); return 0; }
Output
Directory name before modifying = /usr/bin/example/
Directory name after modifying = /usr/home/