0% found this document useful (0 votes)
114 views14 pages

Scope and Lifetime of Variables in C

Uploaded by

blacknatosha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
114 views14 pages

Scope and Lifetime of Variables in C

Uploaded by

blacknatosha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Variables in C Programming: Types, Scope, and Lifetime

Shiksha Online
Updated on Sep 11, 2023 17:58 IST
Variables in C Programming are a container to hold the value of a data type. They
help the compiler to create memory space for the type of Variable. Each variable has
a unique name.

The variable name is similar to the name of a person. Each individual has some name
to recognize him. The only purpose of the variable name is to make it human-
readable; the compiler does not use a variable name. The variables in C programming
have a scope that defines their accessibility and lifetime.

Also, Explore Data Types in C.

There are various types of variables with different scopes and lifetimes. In this
tutorial, we will understand the concepts of variables in C programming with their
different types, scopes, and lifetimes. We will use C programs to use and declare
variables.

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Before we move further, let’s go through the list of topics that we will cover in this
article:

T able Of Contents

What is a Variable in C Programming

What is the Scope and Lif etime of a Variable

T ypes of Variables in C Programming


T ypes of Variables on the Basis of Scope

T ypes of Variables on the Basis of Storage Class

Conclusion

What is a Variable in C Programming

A variable is a holder of data. In geek’s terms, a variable is a memory allocation


space to a data type. It stores a value or data so that the C compiler will create a
memory space for that data. Each variable has a separate name to make it readable
and unique.

Syntax of Variable in C is:

data_type variable_name = value

Example:

int num = 3

Here,
int is the data type

num is a variable name

3 is the variable value

A Variable in C programming consists of two parts, Variable definition, and


initialization.

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Variable definition = it consists of the data type and the variable name. T his part helps
to identif y the variable data type and its identif ier. Each data type consumes dif f erent
memory, so it is necessary to def ine the variable type.

Variable initialization = variable initialization means to provide some value to the


variable as per the data type.

Rules f or Variable Names in C Programming

There are rules for defining the variables in C Programming, and those rules are as
follows:
Variable names will always start with an alphabet and underscore. Example: num, name, a,
x, _value.

Variable names in C will never start with a digit. Example: 4name, is an invalid name.

Variables should be declared with their data type. Variable names without data type will
generate errors. For example, int a =2 is valid. a = 2 is an invalid variable.

C is a strongly typed language, you cannot change the data type of a variable af ter its
def inition.

Reserved keywords cannot be used as a variable name in C programming.

No white spaces are allowed within the variable name.

What is the Scope and Lif etime of a Variable

The scope is the variable region in which it can be used. Beyond that area, you
cannot use a variable. The local and global are two scopes for C variables. The local
scope is limited to the code or function in which the variable is declared.

Global scope is the entire program. Global variables can be used anywhere
throughout the program.

Lifetime is the life or alive state of a variable in the memory. It is the time for which a

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
variable can hold its memory. The lifetime of a variable is static and automatic. The
static lifetime variable remains active till the end of the program.

An automatic lifetime variable or global variable activates when they are called else
they vanish when the function executes.

Types of Variables in C programming

The variables in C programming can be divided into different types based on their
scope and storage classes.

T ypes of Variables on the Basis of Scope

Local Variables

Types of Variables on t he Basis of Scope

The variable that is declared in a function or code is called a local variable. The
scope of Local variables is within the defined function only. You cannot use a local
variable outside the function (in which it is declared).

Program of local variables.

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code

# include <st dio.h>

void person()
{
// Local Variables of the function
int age = 20;
f loat height = 5.6;

print f ("age is %d
", age);
print f ("height is %f ", height );
}

int main()
{
person();
ret urn 0;
}

OUT PUT

age is 20
height is 5.600000

Global Variables

The scope of the global variable is the entire program. They are not defined inside
any function and can be used in any function.

Program to use a global variable in C

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code

# include <st dio.h>

// Declaring global variable


int a = 23;

void f unct ion1()


{
// Function using global variable a
print f ("T he number is %d
", a);
}

void f unct ion2()


{
// Function using global variable a
print f ("T he number is %d
", a);
}

int main()
{
// Calling functions
f unct ion1();
f unct ion2();

ret urn 0;
}

OUTPUT

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
T he number is 23
T he number is 23

Types of Variables on t he Basis of St orage Class or Lif et ime

These Variables use keywords before their definition. The keyword defines the
purpose and use of the variable. Their scope can be local, global, or both depending
on their type. There are 4 types of keyword variables.

You can also explore: Learning About Pointers in C

Static Variable

The static variable is defined using the static keyword. Its scope depends on the
area of its declaration. If a static variable is defined within a function, it is a local
variable. If declared outside the function, its scope is global.

A static variable statically allocated memory to the variable and its lifetime
throughout the program. It holds its value whenever a program is called.

The default value of the static variable is 0.

Syntax:

static data_type variable_name = initial_value

Program to use static variable in C

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code

# include <st dio.h>

void value()
{
int a = 10; // Local variable
st at ic int b = 20; // Static variable

a = a + 10;
b = b + 10;

print f ("T he value of local variable: %d


", a);
print f ("T he value of St at ic variable: %d
", b);
}

int main()
{
value();
print f ("Calling f unct ion 2nd t ime
");
value();
print f ("Calling f unct ion 3rd t ime
");
value();

ret urn 0;
}

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
OUT PUT

T he value of local variable: 20


T he value of Static variable: 30
Calling f unction 2nd time
T he value of local variable: 20
T he value of Static variable: 40
Calling f unction 3rd time
T he value of local variable: 20
T he value of Static variable: 50

NOTE: In the above program the value of the static variable is increasing by 10 every
time when the function is called. While the value of the local variable is the same as the
function call.

You can also explore: Ternary Operator in C: Syntax, Examples and Advantages

Auto Variable

All variables declared in C programming are automatic by default. You can use the
auto keyword to declare the automatic variable. An automatic variable is a local
variable whose lifetime is within the code only.

Its default value is garbage.

Syntax:

auto data_type variable_name = initial_value;

Or

data_type variable_name = initial value;

Program to use automatic variable in C

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code

# include <st dio.h>

void value()
{
int a = 10; //local variable
aut o int b = 20; //automatic variable
print f ("T he value of local variable: %d
", a);
print f ("T he value of aut omat ic variable: %d
", b);

int main()
{
value(); //calling function
ret urn 0;
}

OUT PUT

T he value of local variable: 10


T he value of automatic variable: 20

Register Variable

Register variable uses register keyword before its definitions and it is stored in the
CPU register. It is a local variable. These variables have fast processing. The default
value of the register variable is garbage. Its lifetime is till the end of the function.

You can also explore: Understanding Logical Operators in C

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Syntax:

register data_type variable_name = initial_value

Program to declare register variable

Copy code

# include <st dio.h>

int main()
{
regist er int a = 30; // Declaring register variable
print f ("T he value of regist er variable is %d", a);

ret urn 0;
}

OUTPUT

T he value of register variable is 30

Extern Variable

The extern variables use the extern keyword before the variable definition. This
enhances the variable visibility, and the variable can be used in different C files. It is a
global variable. It is not necessary to initialize the variable at the time of its
declaration. Its lifetime is the entire program.

Syntax:

extern data_type variable_name;

Example: extern int a;

Constant Variable

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
The constant variables are special variables in C whose value does not change
throughout the program after initialization. It is a read-only variable in C. Initializing a
constant variable at the time of variable declaration is mandatory. It uses the “const”
keyword before its definition.

Syntax:

const data_type variable_name = initial_value;

We can initialize other variables anytime and anywhere in the program and can also
change their value. But constant variables have a fixed value, you cannot change
their value after their initialization. If you try to change the value of a constant
variable, the program will generate errors.

You can also explore: C Input Output: printf() and scanf()

Program to change the constant variable value.

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code

# include <st dio.h>

int main()
{
int a = 30;
// Declaring constant variable
const int b = 10;
print f ("Original value of const variable b: %d
", b);

// Changing value of constant variable using a pointer


int * pt r = (int *)&b;
*pt r = 20;

print f ("Modif ied value of const variable b: %d


", b);
ret urn 0;
}

OUTPUT

Original value of const variable b: 10


Modif ied value of const variable b: 10

You can also explore: Difference Between Array and Structure

Conclusion

In this article, we understand various types of variables in C programming. We also


understand the scope and lifetime of variables in C.

Author: Sonal Meenu Singh

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
FAQs

What are the scope and lif etime of variables?

What is lif etime of variables in C?

What is variable and its types?

What are C variables?

What are the 3 main data types?

What is the scope of a variable in C?

What is constant vs static in C?

Is lif etime a continuous variable?

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.

You might also like