C Programming Storage classes, Recursion

sreedharchowdam1 677 views 34 slides Mar 08, 2022
Slide 1
Slide 1 of 34
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34

About This Presentation

Call by value
Call by reference
Storage classes
Recursion


Slide Content

08 Mar 2022: Call by Value/Reference; Storage classes
PROGRAMMING FOR
PROBLEM SOLVING (PPS)
B.Tech I Sem CST

Today’s Topics
Functions:
call by value
call by reference
Storage class specifiers:
auto, extern, register, static
Recursion

Passing arguments to a function
There are two ways of passing arguments to a function:
Call by value
Call by reference
1. Call by value:
In call by value method, the value of the variable is passed to the function as
parameter.
The value of the actual parameter can not be modified by formal parameter.
Different Memory is allocated for both actual and formal parameters.
Because, value of actual parameter is copied to formal parameter.
Note:
Actual parameter – This is the argument which is used in function call.
Formal parameter – This is the argument which is used in function definition

Call by reference
In call by reference , the address of the variable is
passed to the function as parameter.
The value of the actual parameter can be modified
by formal parameter.
Same memory is used for both actual and formal
parameters since only address is used by both
parameters.

Call by Value
void swap(int a,int b);

swap(a,b);

void swap(int a,int b)
{ .... }
void swap(int *a, int *b);

swap(&a,&b);

void swap(int *a,int *b)
{ .... }


Call by Reference

Scope Rules
The scope of an identifier is the portion of the program in
which the identifier can be referenced.
Some identifiers can be referenced throughout program.
Others can be referenced from only portions of a
program.
Ex: When we declare a local variable in a block, it can
be referenced only in that block or in blocks nested within
that block.

Scope Rules
Global variable
declared very early stage
available at all times from
anywhere
created at the start of the
program, and lasts until
the end
difficult to debug

Local variables
simply created when they
are needed,
only available from within
the function in which they
were created
memory requirement is less
easy to debug

Global and Local declarations
void func( float );
const int a = 17;
int b=123;
int c=456;
int main()
{
b = 4;
c = 6;
printf("\na=%d",a);
printf("\nb=%d",b);
printf("\nc=%d",c);
func(42.8);
return 0;
}
void func( float d)
{
float b;
b = 2.3;
printf("\na=%d",a);
printf("\nb=%f",b);
printf("\nc=%d",c);
printf("\nd=%f",d);
}
a=17
b=4
c=6
a=17
b=2.300000
c=6
d=42.799999

14
How global variables work?
int val;
int fun1(void)
{ val += 5;
return val;
}
int fun2(void)
{ int val=0;
return val;
}
int fun3(void)
{ val+=5;
return val;
}
void main()
{ val = 5;
printf(“val= %d\n”,val);
printf(“val= %d\n”,fun1());
printf(“val= %d\n”,fun2());
printf(“val= %d\n”,fun3());
}
Output:
val=5
val=10
val=0
val=15

Life time of a variable
•Local variables: variables declared inside a function or
variables declared as function parameter.
•When function is called, memory will be allocated for
all local variables defined inside the function.
•Finally memory will be deallocated when the function
exits.
•The period of time a variable will be “alive” while the
function is executing is called “lifetime” of this
variable.

Storage Classes
•Every variable in C programming has two properties: type and
storage class.
•Type refers to the data type of variable whether it is character or
integer or floating-point value etc.
•Storage class determines how long it stays in existence.
•There are 4 types of storage class:
–Automatic
–External
–Static
–Register

Storage Classes
Set initial value of a variable or if not
specified then setting it to default value.
Defining scope of a variable.
To determine the life of a variable.
Four types: auto, extern, register, static

Automatic Storage Class
Keyword : auto
Storage Location : Main memory
Initial Value : Garbage Value
Life : Control remains in a block where it is
defined.
Scope : Local to the block in which variable is
declared.

Syntax : auto [data_type] [variable_name];
Example : auto int a;
void main()
{
auto int i=10;
{
auto int i=20;
printf("\n\t %d",i);
}
printf("\n\n\t %d",i);
}

External Storage Class
Keyword : extern
Storage Location : Main memory
Initial Value : Zero
Life : Until the program ends.
Scope : Global to the program.

Syntax : extern [data_type] [variable_name];
Example : extern int a;
Program:
extern int i=10;
void main()
{
int i=20;
void show(void);
printf("\n\t %d",i);
show();
}
void show(void)
{
printf("\n\n\t %d",i);
}

22
extern Example:
int num = 75 ;
void display();
void main()
{
extern int num ;
printf(“\nNum : %d",num);
display();
}

void display()
{
extern int num ;
printf(“\nNum : %d",num);
}

OUTPUT
Num : 75
Num : 75

File1.c

main()
{
extern int var;
var=200;
display();
}
File2.c
#include<File1.c>
int var;
void display()
{
printf("var=%d",var);
}

Register Storage Class
Keyword : register
Storage Location : CPU Register
Initial Value : Garbage
Life : Local to the block in which variable is
declared.
Scope : Local to the block.

Syntax : register [data_type] [variable_name];
Example : register int a;
void main()
{
register int i=10;
{
register int i=20;
printf("\n\t %d",i);
}
printf("\n\n\t %d",i);
}

Static Storage Class
Keyword : static
Storage Location : Main memory
Initial Value : Zero and can be initialize once only.
Life : depends on function calls and the whole
application or program.
Scope : Local to the block.

Syntax : static [data_type] [variable_name];
Example : static int a;
void main()
{
int i;
void incre(void);
for (i=0; i<3; i++)
incre();
}
void incre(void)
{
int avar=1;
static int svar=1;
avar++;
svar++;
printf(“Auto var value : %d",avar);
printf("Static var value : %d",svar);
}
OUTPUT
Auto var value : 2
Static var value : 2
Auto var value : 2
Static var value : 3
Auto var value : 2
Static var value : 4

28
Examples static
int main()
{
int i;
for(i =1; i<=4; i++)
stat();
}
void stat()
{
static int x=0;
x = x+1;
printf(“x = %d”,x);
}
Output
x=1
x=2
x=3
x=4

static: Example
void test(); //Function declaration (discussed in next topic)
main()
{
test();
test();
test();
}
void test()
{
static int a = 0; //Static variable
a = a+1;
printf("%d\t",a);
}
output :
1 2 3

Recursion
The recursive function is
a kind of function that calls itself, or
a function that is part of a cycle in the sequence of
function calls.

f1 f1 f2 fn …

Ex:
recursive Function
int multiply(int m,int n)
{
int ans;
if(n==1)
ans = m;
else
ans = m + multiply(m, n-1);
return (ans);
}

Recursion Ex: Factorial
int factorial(int i)
{
if(i <= 1)
{
return 1;
}
return i * factorial(i - 1);
}

Sum of n natural no’s using recursion
int sum(int n)
{
if(n!=0)
return n+sum(n-1);
else
return n;
}

Recursion Ex: Fibonacci
void fibonacci(int n)
{
static int n1=0,n2=1,n3;
if(n>0)
{
n3=n1+n2;
n1=n2;
n2=n3;
printf("%3d",n3);
fibonacci(n-1);
}
}
int main()
{
int n;
printf("Enter n:");
scanf("%d",&n);
printf("%3d %3d",0,1);
fibonacci(n-2);
return 0;
}

A Classical Case: Towers of Hanoi
•The towers of Hanoi problem involves moving a
number of disks (in different sizes) from one
tower (or called “peg”) to another.
–The constraint is that the larger disk can never be
placed on top of a smaller disk.
–Only one disk can be moved at each time
–Assume there are three towers available.

Towers of Hanoi

Towers of Hanoi
void TOH(int n, char s, char d, char i)
{
if (n == 1)
{
printf("\n Move disk 1 from tower %c to %c", s, d);
return;
}
TOH(n-1, s, i, d);
printf("\n Move disk %d from tower %c to %c", n, s, d);
TOH(n-1, i, d, s);
}

10-40
A Classical Case: Towers of Hanoi
The execution result of calling TOH(n, 'A','C','B');
Tags