Another way to learn New Technology

What is a Variable and its Scope in C Programming

0 50

What is Variable and its Scope in C Programming

Variable :

A variable is a name given to some known or unknown quantity. It is a means of symbolically representing a data storage location and its content, a quantity which could change numerous times during the course program execution.

Scope of Variable:

The scope of a variable describes where in a program’s text, the variable may be used, while the extent (or lifetime) describes when in a program’s execution a variable has a (meaningful) value. The scope of a variable is actually a property of the name of the variable and the extent is a property of the variable itself.

The scope of a variable can be defined as an area in a segment of program statement in which that variable is particularly used.

For Example:

#include <stdio.h>

#include <conio.h>

function();

int global = 20;

main()

{

int local = 20;

printf(“\n Local = %d”, local);
local = local + 10;
printf(“\n Global = %d”, global);
printf(“\n Local = %d”, local); 
global = global + 10; 
printf(“\n Global = %d”, global); 

function();

printff“\n Bank to Main”); 
printf(“\n Local = %d”, local); 
printf(“\n Global = %d”, global); 

getch();

}

function()

{

int local = 50;

printf(“This is a function”); 
printff“\n Local = %d”, local); 
local = local + 10; 
global = global + 10; 
printf(“\n Local = %d”, local); 
printff“\n Global = %d”, global);

}

Output

local = 20 
global = 20 
local = 30 
global = 30 

This is a function 
local = 50 
global = 50 
local = 60 
global = 40 

Back to Main! 

local = 30 global = 40

This program clearly shows the scope of variables.

Click here: for more tutorials of c programming

Hope you like this articles! if you really like this article so don’t forget to give us a small feedback or review

Comments
Loading...