SlideShare a Scribd company logo
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= %dn”,val);
printf(“val= %dn”,fun1());
printf(“val= %dn”,fun2());
printf(“val= %dn”,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("nt %d",i);
}
printf("nnt %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("nt %d",i);
show();
}
void show(void)
{
printf("nnt %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("nt %d",i);
}
printf("nnt %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("%dt",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');
C Programming Storage classes, Recursion

More Related Content

PPTX
Storage classes in c language
PPTX
Functions in C
PPTX
Storage classes in C
PPTX
Storage classes in C
PPTX
Functions in c
PPTX
Function in c
PPTX
Array in C
Storage classes in c language
Functions in C
Storage classes in C
Storage classes in C
Functions in c
Function in c
Array in C

What's hot (20)

PPT
One Dimensional Array
PPSX
Function in c
PDF
C Recursion, Pointers, Dynamic memory management
PPTX
Array in C
PPT
C++ Arrays
PPTX
Strings in C language
PPTX
Switch statement, break statement, go to statement
PPT
Formatted input and output
PPTX
PDF
Arrays In C
PPT
Arrays in c
PPTX
Control statements in c
PPTX
Strings in C
PPTX
Data Input and Output
PPTX
C programming - String
PPTX
User defined functions in C
PPT
Introduction to c programming
PPTX
[OOP - Lec 19] Static Member Functions
PPTX
Pointers in c - Mohammad Salman
PPTX
Member Function in C++
One Dimensional Array
Function in c
C Recursion, Pointers, Dynamic memory management
Array in C
C++ Arrays
Strings in C language
Switch statement, break statement, go to statement
Formatted input and output
Arrays In C
Arrays in c
Control statements in c
Strings in C
Data Input and Output
C programming - String
User defined functions in C
Introduction to c programming
[OOP - Lec 19] Static Member Functions
Pointers in c - Mohammad Salman
Member Function in C++
Ad

Similar to C Programming Storage classes, Recursion (20)

PPT
Functions in c
PPT
C programming is a powerful, general-purpose language used for developing ope...
PDF
Data structure scope of variables
PPT
presentation_functions_1443207686_140676.ppt
PDF
Storage Classes.pdf DJJKLF DKFF DSLKF. DSF; FD
PPTX
Presentation on function
PDF
C_Dayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 3.pdf
PDF
Function in C++
PPTX
Global variables, sorting static variables,function and arrays,
PDF
Advanced C Programming Notes
PDF
4th unit full
PDF
Unit iii
PPTX
function in c
PDF
11 2. variable-scope rule,-storage_class
PPTX
FUNCTION CPU
DOCX
Storage class
PPT
storage clas
PPTX
CH.4FUNCTIONS IN C (1).pptx
Functions in c
C programming is a powerful, general-purpose language used for developing ope...
Data structure scope of variables
presentation_functions_1443207686_140676.ppt
Storage Classes.pdf DJJKLF DKFF DSLKF. DSF; FD
Presentation on function
C_Dayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 3.pdf
Function in C++
Global variables, sorting static variables,function and arrays,
Advanced C Programming Notes
4th unit full
Unit iii
function in c
11 2. variable-scope rule,-storage_class
FUNCTION CPU
Storage class
storage clas
CH.4FUNCTIONS IN C (1).pptx
Ad

More from Sreedhar Chowdam (20)

PDF
DBMS Nested & Sub Queries Set operations
PDF
DBMS Notes selection projection aggregate
PDF
Database management systems Lecture Notes
PPT
Advanced Data Structures & Algorithm Analysi
PDF
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
PPTX
Design and Analysis of Algorithms Lecture Notes
PDF
Design and Analysis of Algorithms (Knapsack Problem)
PDF
DCCN Network Layer congestion control TCP
PDF
Data Communication and Computer Networks
PDF
DCCN Unit 1.pdf
PDF
Data Communication & Computer Networks
PDF
PPS Notes Unit 5.pdf
PDF
PPS Arrays Matrix operations
PDF
Programming for Problem Solving
PDF
Big Data Analytics Part2
PDF
Python Programming: Lists, Modules, Exceptions
PDF
Python Programming by Dr. C. Sreedhar.pdf
PDF
Python Programming Strings
PDF
Python Programming
PDF
Python Programming
DBMS Nested & Sub Queries Set operations
DBMS Notes selection projection aggregate
Database management systems Lecture Notes
Advanced Data Structures & Algorithm Analysi
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms (Knapsack Problem)
DCCN Network Layer congestion control TCP
Data Communication and Computer Networks
DCCN Unit 1.pdf
Data Communication & Computer Networks
PPS Notes Unit 5.pdf
PPS Arrays Matrix operations
Programming for Problem Solving
Big Data Analytics Part2
Python Programming: Lists, Modules, Exceptions
Python Programming by Dr. C. Sreedhar.pdf
Python Programming Strings
Python Programming
Python Programming

Recently uploaded (20)

PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
Digital Logic Computer Design lecture notes
PPT
Mechanical Engineering MATERIALS Selection
PPTX
bas. eng. economics group 4 presentation 1.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
Construction Project Organization Group 2.pptx
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Geodesy 1.pptx...............................................
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
Foundation to blockchain - A guide to Blockchain Tech
Arduino robotics embedded978-1-4302-3184-4.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Digital Logic Computer Design lecture notes
Mechanical Engineering MATERIALS Selection
bas. eng. economics group 4 presentation 1.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
UNIT 4 Total Quality Management .pptx
Construction Project Organization Group 2.pptx
Strings in CPP - Strings in C++ are sequences of characters used to store and...
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
Geodesy 1.pptx...............................................
Lecture Notes Electrical Wiring System Components
Foundation to blockchain - A guide to Blockchain Tech

C Programming Storage classes, Recursion

  • 1. 08 Mar 2022: Call by Value/Reference; Storage classes PROGRAMMING FOR PROBLEM SOLVING (PPS) B.Tech I Sem CST
  • 2. Today’s Topics  Functions:  call by value  call by reference  Storage class specifiers:  auto, extern, register, static  Recursion
  • 3. 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
  • 4. 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.
  • 5. 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
  • 6. 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.
  • 7. 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
  • 8. 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
  • 9. 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= %dn”,val); printf(“val= %dn”,fun1()); printf(“val= %dn”,fun2()); printf(“val= %dn”,fun3()); } Output: val=5 val=10 val=0 val=15
  • 10. 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.
  • 11. 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
  • 12. 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
  • 13. 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.
  • 14. Syntax : auto [data_type] [variable_name]; Example : auto int a; void main() { auto int i=10; { auto int i=20; printf("nt %d",i); } printf("nnt %d",i); }
  • 15. External Storage Class  Keyword : extern  Storage Location : Main memory  Initial Value : Zero  Life : Until the program ends.  Scope : Global to the program.
  • 16. Syntax : extern [data_type] [variable_name]; Example : extern int a; Program: extern int i=10; void main() { int i=20; void show(void); printf("nt %d",i); show(); } void show(void) { printf("nnt %d",i); }
  • 17. 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
  • 19. 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.
  • 20. Syntax : register [data_type] [variable_name]; Example : register int a; void main() { register int i=10; { register int i=20; printf("nt %d",i); } printf("nnt %d",i); }
  • 21. 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.
  • 22. 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
  • 23. 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
  • 24. 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("%dt",a); } output : 1 2 3
  • 25. 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 …
  • 26. 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); }
  • 27. Recursion Ex: Factorial int factorial(int i) { if(i <= 1) { return 1; } return i * factorial(i - 1); }
  • 28. Sum of n natural no’s using recursion int sum(int n) { if(n!=0) return n+sum(n-1); else return n; }
  • 29. 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; }
  • 30. 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.
  • 32. 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); }
  • 33. 10-40 A Classical Case: Towers of Hanoi The execution result of calling TOH(n, 'A','C','B');