0% found this document useful (0 votes)
19 views

Module 7 - Part 2 - Scope and Storage Classes

The document discusses the scope and storage classes of variables in C programming. It describes four storage classes - auto, global, static, and register. Auto variables are local to the block they are declared in and are stored on the stack. Global variables have scope across the entire program and are stored in the data segment. Static variables retain their value between function calls and can be either local or global in scope. The document provides examples to illustrate the differences between these variable storage classes.

Uploaded by

f20221346
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Module 7 - Part 2 - Scope and Storage Classes

The document discusses the scope and storage classes of variables in C programming. It describes four storage classes - auto, global, static, and register. Auto variables are local to the block they are declared in and are stored on the stack. Global variables have scope across the entire program and are stored in the data segment. Static variables retain their value between function calls and can be either local or global in scope. The document provides examples to illustrate the differences between these variable storage classes.

Uploaded by

f20221346
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Module 7 – Part 2 – Scope and

Storage Classes of Variables


BITS Pilani Dr. Jagat Sesh Challa
Pilani Campus
Department of Computer Science & Information Systems
Module Overview

• Scope and Storage Class of Variables


• Memory Layout of a C Program
• Auto Variables
• Global Variables
• Static Variables
• Register Variables

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Consider this example
#include <stdio.h> int main() {
void f1(int a){ int a = 5;
printf(“a = %d\n”,a); f1(a);
a = 10; printf("a = %d",a);
printf(“a = %d\n”,a); return 0;
} }
output:
a = 5
a is local to the function f1
a = 10
a = 5 a is local to the function main

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Scope and Storage Class of a
variable
Storage Class: A storage class of a variable tells us about the
following:
• the variable’s scope, or which sections of the code where you can
access and use it

• the location where the variable will be stored inside the memory

• the initial value of a variable

• the lifetime of a variable, or how long does the variable reside in


the memory

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example - Scope
#include <stdio.h>
int y = 5; //scope of y is complete program
void main() {
int a = 5; //scope of a is main()
{
int b = 10; //scope of b is {}
}
printf("a = %d, b = %d",a,b); f1();
}
void f1(){
int x = 2, b = 5; //scope of x and b is f1()
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Scope and Storage Class of a
variable

• Auto
• Global
• Static
• Register

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus

Auto Variables
Auto Storage Class
#include <stdio.h>
Variables that are
declared within a code void main() {
block ({ … }) are known auto variable a local to
int a;
as Auto variables or local block of main() function
variables. a = 5;
auto variable b local to
• Scope: Only the block {
the block {}
in which it is declared int b = 10;
can access it }
• Initial Value: By
default, it contains a printf("a = %d, b = %d", a, b);
garbage value f1(); Error!
• Storage Location: }
Stored in the stack void f1(){ auto variable a local to
segment the function f1()
int a;
• Lifetime: Until the
execution of the block a = 20;
finishes }
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus

Global Variables
Global Storage Class
#include <stdio.h>
Global variables are int a; global variable a that is
variables that are declared void f1(){ accessible to all functions
outside all the blocks (or printf(“a = %d\n”,++a);
outside all the functions) int a = 2;
• Scope: Accessible to all the printf(“a = %d\n”, a);
functions in a program }
void main() {
• Initial Value: Default value a = a+3; f1();
is 0 int a = 5;
• Storage Location: Stored in printf("a = %d",a);
the data segment }
• Lifetime: Accessible Output:
throughout the program a = 4 value of global variable a
exeucution a = 2 value of variable a local to f1()
a = 5 value of variable a local to main()
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Functions, local and global
variables in main memory
Frame allocated to main() in
the stack #include <stdio.h>
Stack

a Frame allocated to f1() in int a;


the stack void f1(){
printf(“a = %d\n”,++a);
int a = 2;
printf(“a = %d\n”, a);
Heap

}
void main() {
local variable a resides in
the stack segment, a = a+3; f1();
Data

a specifically inside the frame int a = 5;


allocated to the function f1() printf("a = %d",a);
}
Text

global variable a resides


in data segment

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Functions, local and global
variables in main memory
Frame allocated to main() in
the stack #include <stdio.h>
Stack

a Frame allocated to f1() in int a;


the stack void f1(){
printf(“a = %d\n”,++a);
int a = 2;
printf(“a = %d\n”, a);
Heap

}
void main() {
When function f1() returns, a = a+3; f1();
frame allocated to it is
Data

a destroyed.
int a = 5;
printf("a = %d",a);
}
Text

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Functions, local and global
variables in main memory
Frame allocated to main() in
a the stack #include <stdio.h>
Stack

a Frame allocated to f1() in int a;


the stack void f1(){
local variable a created in printf(“a = %d\n”,++a);
the frame allocated to the int a = 2;
function main() printf(“a = %d\n”, a);
Heap

}
void main() {
When function f1() returns, a = a+3; f1();
frame allocated to it is
Data

a destroyed.
int a = 5;
printf("a = %d",a);
}
Text

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus

Static Variables
Static Variables

Are of two types:


• Local static variable
• Global static variable

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Local Static Variables
Static variables are declared using the
keyword static. #include <stdio.h>
• E.g.- static int a; void main() {
• Scope: Remains visible only to the int i = 0;
function or the block in which it is for(i = 0; i < 3; i++)
defined {
• Initial Value: Default value is 0 static int y = 0;
• Storage Location: Stored in the Data y += 10;
Segment printf(“y = %d\t”,y);
• Lifetime: Until the program }
terminates. The value assigned to it }
remains even after the function (or
block) where it is defined terminates
• Initialized only once
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Local Static Variables
#include <stdio.h> #include <stdio.h>
void main() { void main() {
int i = 0; int i = 0;
for(i = 0; i < 3; i++) { for(i = 0; i < 3; i++){
int y = 0; static int y = 0;
y += 10; y += 10;
printf(“y = %d\t”,y); printf(“y = %d\t”,y);
} }
} }
Output: 10 10 10 Output: 10 20 30
Local static variable
• Retains its value between function calls or block
• Remains visible only to the function or block in which it is defined.
• It remains even after the function terminates
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Local Static Variables – Another
Example
#include <stdio.h> #include <stdio.h>
void f1() { void f1() {
int i = 0; static int i = 0;
i = i+10; i = i+10;
printf(“i = %d\t”,i); printf(“i = %d\t”,i);
} }
void main() { void main() {
int i = 0; int i = 0;
for(i = 0; i < 3; i++) for(i = 0; i < 3; i++)
f1(); f1();
} }

Output: 10 10 10 Output: 10 20 30
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Local Static Variables in
Memory
#include <stdio.h>
Stack frame allocated void f1() {
j to main()
Stack

static int i = 0;
Stack frame allocated
i = i+10;
to f1()
printf(“i = %d\t”,i);
local variable j
}
resides in the stack
frame allocated to void main() {
Heap

main() int j = 0;
for(j = 0; j < 3; j++)
f1();
Data

i static variable i
resides in data }
segment even though it Note: The stack frame for f1() is created
Text

is defined inside f1() and destroyed 3 times. But variable j is


created only once and stored in the data
segment.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Global Static Variables
• Scope: Visible to all the functions in a program
• Initial Value: By default, initialized to 0
• Storage Location: Stored in the Data Segment
• Lifetime: Until the program terminates.
• Initialized only once
• Difference with global storage class: While static global
variables are visible only to the file in which it is declared, global
variables can be used in other files as well.
– We will study more about multi-file compilation in upcoming lab
sessions.

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example
#include <stdio.h>
static int a; //initialized
void f1(){
printf(“a = %d\n”,a); void main() {
int a = 2; f1();
printf(“a = %d\n”,a); int a = 5;
} printf("a = %d",a);
}
output:
a = 0 value of static global variable a
a = 2 value of auto variable a local to f1
a = 5 value of auto variable a local to main
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Memory Allocation of Static
global Variable
Stack frame allocated #include <stdio.h>
a to main() static int a; //initialized
Stack

a Stack frame allocated void f1(){


to f1() printf(“a = %d\n”,a);
int a = 2;
printf(“a = %d\n”, a);
}
Heap

void main() {
f1();
int a = 5;
Data

Global static variable printf("a = %d",a);


a
}
a resides in data
segment
Text

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus

Register Variables
Registers and Register
variables
• A Register is very high-speed memory storage that is located in the CPU.
• Typically limited in number (16 registers for Intel i7 Processor)
• They are extremely fast to access when compared to main memory
(RAM).
• The variables that are most frequently accessed can be put into registers
using the register keyword.
• The keyword register hints to compiler that a given variable can be put in
a register.
• It’s compiler’s choice to put it in a register or not.
• Generally, compilers themselves do some optimizations and put the
variables in register (even when declared without the register keyword).

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Register Variables
• Scope − They are local to the function.
• Default value − Default initialized value is the garbage value.
• Lifetime − Till the end of the execution of the block in which it is defined.
Example:
#include <stdio.h>
int main(){
register char x = ‘S’; register int a = 10;
int b = 8;
printf("The value of register variable b : %c\n",x);
printf("The sum of auto and register variable: %d",(a+b));
return 0;
}
Output:
The value of register variable b : S
The sum of auto and register variable : 18

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Caveats
• You can’t access address of a register variable with an “&”
• Can’t use register variables with scanf().
• Register is a storage class, and C doesn’t allow multiple storage
class specifiers for a variable.
• register can not be used with static or extern.
• You can’t declare global register variables
• All register variable must be declared within the functions.
• There is no limit on number of register variables in a C program.
• Compiler may put some variables in register and some not
depending upon availability as number of registers is limited.

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Static vs Register Variables
Static Variables Register Variables
Keyword used is – “static”. Keyword used is – “register”.
Static variable may be internal or external depending
Register variables are declared inside a function.
on the place of declaration.
Local static variables are similar to auto variables or
Register variables are similar to auto or local or internal
local variables. Whereas global static variables are
variables.
similar to global variables.
The register variables leads to faster execution of
The execution speed is slower than register variables.
programs.
Internal static variables are active(visibility) in the
particular function and External static variables are Register variables are active only within the function.
active in the entire program.
Internal static variables are alive(lifetime) in until the end
of the function and External static variables are alive in Register variables are alive until the end of a function.
the entire program.
Static variables stored in initialized data segments. Register variables are stored in registers.
Static variable is stored in the memory of the data In register variables, CPU itself stores the data and
segment. access quickly.

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus

You might also like