SlideShare a Scribd company logo
1 of 34
Download to read offline
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

What's hot

Functions in c language
Functions in c language Functions in c language
Functions in c language tanmaymodi4
 
Input output statement in C
Input output statement in CInput output statement in C
Input output statement in CMuthuganesh S
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c languagetanmaymodi4
 
Chapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CChapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CBUBT
 
Strings in c
Strings in cStrings in c
Strings in cvampugani
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programmingprogramming9
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in CHarendra Singh
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solutionAzhar Javed
 
ITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in javaITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in javaAtul Sehdev
 
Operators in c language
Operators in c languageOperators in c language
Operators in c languageAmit Singh
 

What's hot (20)

Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Input output statement in C
Input output statement in CInput output statement in C
Input output statement in C
 
Storage classes in C
Storage classes in CStorage classes in C
Storage classes in C
 
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
 
Functions in c
Functions in cFunctions in c
Functions in c
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c language
 
C functions
C functionsC functions
C functions
 
Chapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CChapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in C
 
C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
 
Strings in c
Strings in cStrings in c
Strings in c
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
 
C function presentation
C function presentationC function presentation
C function presentation
 
Strings
StringsStrings
Strings
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
ITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in javaITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in java
 
Operators in c language
Operators in c languageOperators in c language
Operators in c language
 

Similar to C Programming Storage classes, Recursion

presentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptpresentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptSandipPradhan23
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxSangeetaBorde3
 
Storage classes in C
Storage classes in C Storage classes in C
Storage classes in C Self employed
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 FocJAYA
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C ProgrammingShuvongkor Barman
 
Presentation on function
Presentation on functionPresentation on function
Presentation on functionAbu Zaman
 
Storage classes arrays & functions in C Language
Storage classes arrays & functions in C LanguageStorage classes arrays & functions in C Language
Storage classes arrays & functions in C LanguageJenish Bhavsar
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4Saranya saran
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1AmIt Prasad
 
function in c
function in cfunction in c
function in csubam3
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4MKalpanaDevi
 

Similar to C Programming Storage classes, Recursion (20)

presentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptpresentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.ppt
 
functions
functionsfunctions
functions
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
Storage class
Storage classStorage class
Storage class
 
Fucntions & Pointers in C
Fucntions & Pointers in CFucntions & Pointers in C
Fucntions & Pointers in C
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
 
Storage classes in C
Storage classes in C Storage classes in C
Storage classes in C
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Presentation on function
Presentation on functionPresentation on function
Presentation on function
 
Storage classes arrays & functions in C Language
Storage classes arrays & functions in C LanguageStorage classes arrays & functions in C Language
Storage classes arrays & functions in C Language
 
Functions
FunctionsFunctions
Functions
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Storage class
Storage classStorage class
Storage class
 
Functions in c
Functions in cFunctions in c
Functions in c
 
7 functions
7  functions7  functions
7 functions
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
 
function in c
function in cfunction in c
function in c
 
C functions list
C functions listC functions list
C functions list
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 

More from Sreedhar Chowdam

Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesSreedhar Chowdam
 
Design and Analysis of Algorithms (Knapsack Problem)
Design and Analysis of Algorithms (Knapsack Problem)Design and Analysis of Algorithms (Knapsack Problem)
Design and Analysis of Algorithms (Knapsack Problem)Sreedhar Chowdam
 
DCCN Network Layer congestion control TCP
DCCN Network Layer congestion control TCPDCCN Network Layer congestion control TCP
DCCN Network Layer congestion control TCPSreedhar Chowdam
 
Data Communication and Computer Networks
Data Communication and Computer NetworksData Communication and Computer Networks
Data Communication and Computer NetworksSreedhar Chowdam
 
Data Communication & Computer Networks
Data Communication & Computer NetworksData Communication & Computer Networks
Data Communication & Computer NetworksSreedhar Chowdam
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem SolvingSreedhar Chowdam
 
Python Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, ExceptionsPython Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, ExceptionsSreedhar Chowdam
 
Python Programming by Dr. C. Sreedhar.pdf
Python Programming by Dr. C. Sreedhar.pdfPython Programming by Dr. C. Sreedhar.pdf
Python Programming by Dr. C. Sreedhar.pdfSreedhar Chowdam
 
Python Programming Strings
Python Programming StringsPython Programming Strings
Python Programming StringsSreedhar Chowdam
 
Programming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture NotesProgramming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture NotesSreedhar Chowdam
 
Data Structures Notes 2021
Data Structures Notes 2021Data Structures Notes 2021
Data Structures Notes 2021Sreedhar Chowdam
 
Computer Networks Lecture Notes 01
Computer Networks Lecture Notes 01Computer Networks Lecture Notes 01
Computer Networks Lecture Notes 01Sreedhar Chowdam
 
Dbms university library database
Dbms university library databaseDbms university library database
Dbms university library databaseSreedhar Chowdam
 
Er diagram for library database
Er diagram for library databaseEr diagram for library database
Er diagram for library databaseSreedhar Chowdam
 

More from Sreedhar Chowdam (20)

Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture Notes
 
Design and Analysis of Algorithms (Knapsack Problem)
Design and Analysis of Algorithms (Knapsack Problem)Design and Analysis of Algorithms (Knapsack Problem)
Design and Analysis of Algorithms (Knapsack Problem)
 
DCCN Network Layer congestion control TCP
DCCN Network Layer congestion control TCPDCCN Network Layer congestion control TCP
DCCN Network Layer congestion control TCP
 
Data Communication and Computer Networks
Data Communication and Computer NetworksData Communication and Computer Networks
Data Communication and Computer Networks
 
DCCN Unit 1.pdf
DCCN Unit 1.pdfDCCN Unit 1.pdf
DCCN Unit 1.pdf
 
Data Communication & Computer Networks
Data Communication & Computer NetworksData Communication & Computer Networks
Data Communication & Computer Networks
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
 
Big Data Analytics Part2
Big Data Analytics Part2Big Data Analytics Part2
Big Data Analytics Part2
 
Python Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, ExceptionsPython Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, Exceptions
 
Python Programming by Dr. C. Sreedhar.pdf
Python Programming by Dr. C. Sreedhar.pdfPython Programming by Dr. C. Sreedhar.pdf
Python Programming by Dr. C. Sreedhar.pdf
 
Python Programming Strings
Python Programming StringsPython Programming Strings
Python Programming Strings
 
Python Programming
Python Programming Python Programming
Python Programming
 
Python Programming
Python ProgrammingPython Programming
Python Programming
 
Programming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture NotesProgramming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture Notes
 
Big Data Analytics
Big Data AnalyticsBig Data Analytics
Big Data Analytics
 
Data Structures Notes 2021
Data Structures Notes 2021Data Structures Notes 2021
Data Structures Notes 2021
 
Computer Networks Lecture Notes 01
Computer Networks Lecture Notes 01Computer Networks Lecture Notes 01
Computer Networks Lecture Notes 01
 
Dbms university library database
Dbms university library databaseDbms university library database
Dbms university library database
 
Er diagram for library database
Er diagram for library databaseEr diagram for library database
Er diagram for library database
 
Dbms ER Model
Dbms ER ModelDbms ER Model
Dbms ER Model
 

Recently uploaded

Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 

Recently uploaded (20)

Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 

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');