Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Course Title: Applied Computer

Programming II

Course Code: GEC 225


Omega Semester
2021/2022
Module 3
Understanding Scope and
Storage Class
3.1.0 Scope of a Variable/Function
 Scope of a variable refers to the portion
of the program over which the variable is
recognized.

 Scope of a function is either local to the


file where it is defined (static) or globally
available to any file in a project (external)

3 Friday, April 22, 2022


 There are three (3) levels of scope
available in C program. They are:
i. Global Scope
ii. Function Block Scope
iii. Statement Block Scope

4 Friday, April 22, 2022


3.1.1 Global Scope
 Global Scope is when the variable is declared
outside of a function block in the current source
code file.

 Variables with global scope are not limited to


any function but can be accessed and modified
by all functions within the program.

 They are automatically initialized to zero when


declared and they are generally written before
main() function.
5 Friday, April 22, 2022
Example 3.1 – Example of a Variable with Global Scope
#include<stdio.h>
#define MAXVAL 1000
int k = 10; // scope for global variable k starts here ....

int main( ) {
setup();
return 0;
}

void setup() {
int x = 5;
serial.begin(9600);
if (x <MAXVAL) {
int temp;
temp = x*100*k;
}
printf(“ The value of temp is:” );
printf (temp);
}
6 Friday, April 22, 2022
3.1.2 Function Block Scope

 The visibility of function block


scope is from the point of the
declaration and/or definition of a
variable to the end of the function in
which it is defined.

7 Friday, April 22, 2022


Example 3.2 - Example of Variables with Function
Block Scope
void setup() {
int x = 5; //scope for x starts here…
int temp; //scope for temp starts here…
if (x <MAXVAL) {
temp = x*100;
}
printf(“ The value of temp is:” );
printf(temp);
} // …. and x and temp scopes ends here.

8 Friday, April 22, 2022


3.1.3 Statement Block Scope

 The visibility of statement block scope extends


from the point of definition of the data item to the
closing brace of the statement block in which it is
defined.

Example 3.3
If (x <MAXVAL) { //start of if statement block
int temp; //temp’s scope starts after this line
temp = x *100; //temp goes out of scope here
}
9 Friday, April 22, 2022
3.2.0 Storage Classes
 Storage class in C language is a specifier
which tells the compiler about data storage, it
describes:
i. where and how to store variables,
ii. its initial value and
iii. scope of the variables in a program.

 Syntax of a storage class:


storageclass datatype variable_name;

 A storage class defines the scope (visibility)


and life-time of variables and/or functions
within a C program.
10 Friday, April 22, 2022
 There are 4 storage class specifiers in C language as
shown in Fig. 3.1:

Storage class specifier

auto register static extern

Fig. 3.1: Storage class specifiers

11 Friday, April 22, 2022


3.2.1 Auto Storage Class
 The auto storage class is the default storage class for
all local variables.
 The keyword for declaring this storage class is auto.
 The syntax is:
auto int month;
or
int month; //Both are same
 An automatic variable is always declared within a
function and are local to the function in which they
are declared.

12 Friday, April 22, 2022


 The features of auto storage class are:-
✓ Storage: memory location.
✓ Default initial value: unpredictable value or garbage
value.
✓ Scope: local to the block or function in which the variable
is defined.
✓ Life time: Till the control remains within function or
block in which it is defined. It terminates when function is
released.

 The variable without any storage class specifier is called


automatic variable.

13 Friday, April 22, 2022


Example 3.4 – Use of auto storage class
#include<stdio.h>
void main()
{
auto int detail;
}

14 Friday, April 22, 2022


3.2.2 Register Storage Class
 The register storage class is used to define local
variables that should be stored in a register instead of
RAM.
 Register variable has faster access than a variable
that is stored in the RAM. Frequently used variables
are kept in register and only few variables can be
placed inside register.
 The keyword used to declare this storage class is
register.

Syntax:
register int number;
15 Friday, April 22, 2022
 The features of register storage class are:
✓ Storage: CPU register.
✓ Default initial value: garbage value
✓ Scope: local to the function or block in
which it is defined.
✓ Life time: controls remains within
function or blocks in which it is defined.
 Storing all variable in the CPU register is not
possible because of the limitation of the register
pair.
 When a variable is used at many places (like loop
counter), then it is better to declare it as a register
storage class.
16 Friday, April 22, 2022
Example 3.5 – Use of register storage class

#include<stdio.h>
main( )
{
register int i;
for(i=1;i<=12;i++)
{
printf(“%d”,i);
}
17 Friday, April 22, 2022
3.2.3 Static Storage Class
 A static variable tells the compiler to persist
the variable until the end of a program.

 Instead of creating and destroying a variable


every time when it comes into and goes out of
scope, static is initialized only once and
remains in existence till the end of program.

 A static variable can either be internal or


external depending upon the place of
declaration.
18 Friday, April 22, 2022
 Internal static variable remains inside the
function in which it is defined.
 External static variables remain restricted
to scope of file in which they were declared.
 They are assigned 0 (zero) as default value
by the compiler.
 The keyword used to declare static storage
class is static.
Syntax:
static int number;

19 Friday, April 22, 2022


 The features of static storage class are:-
✓ Storage: memory location.
✓ Default initial value: zero
✓ Scope: local to the block or function in
which it is defined.
✓ Life time: value of the variable persist or
remain between different function call.
 Note that if a function is declared as static,
it will only be available within the file where
it was declared, which makes it a local
function.
20 Friday, April 22, 2022
Example 3.6 – Use of static storage class
#include <stdio.h>
void test(); //Function declaration
main()
{
test();
test();
test();
}

void test()
{
static int a = 0; //Static variable
a = a+1;
printf("%d\t",a);
}
21 Friday, April 22, 2022
3.2.4 External Storage Class
 External storage class is used to declare variable
outside any function.

 Such variables remain available throughout the


entire program and can be changed by any function
in the program.

 The extern keyword is used for this storage class.

 Note that the extern declaration does not allocate


storage for variables.
22 Friday, April 22, 2022
 The features for this class are:
✓ Storage: memory area.
✓ Default initial value: zero.
✓ Scope: global.
✓ Life time: as long as program
execution remains it retains.

23 Friday, April 22, 2022


• The following illustrates the use of extern
storage class showing that a global variable from
one file can be used in other using extern
keyword

24 Friday, April 22, 2022


Example 3.7 – Use of extern storage class

main()
{
extern int x; //Tells compiler that it is defined
//somewhere else
x = 10;
printf("%d",x);
}
int x; //Global variable x

25 Friday, April 22, 2022


Assignment for Module 3
1. What does scope refer to?
2. What are the scope levels in C?
3. Why is it usually a good thing to avoid using the global
storage class?
4. What are the C storage classes?
5. What is the default scope level for a function?
6. What is the default storage class for a library function?
7. A register variable will always be placed in the register,
explain.

26 Friday, April 22, 2022


THANKS YOU LISTENING

27 Friday, April 22, 2022

You might also like