SlideShare a Scribd company logo
Introduction to modular programming, writing functions, formal
parameters, actual parameters .Pass by Value, Recursion, Arrays
as Function Parameters ,structure, union, Storage Classes, Scope
and life time of variables
MODULE 4
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 1
Functions
• A function is a group of statements
that together perform a task.
• Every C program has at least one
function, which is main().
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 2
Why to use Functions?
• Development can be divided
• Readable programs
• Programming errors are easy to detect
• Allows re-use of codes
• Improves manageability
• Collaboration
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 3
Types of Functions
There are two types of functions in C programming:
• Standard library functions
• User-defined functions
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 4
Standard Library Functions
• The standard library functions are built-in functions
in C programming.
• These functions are defined in header files.
For example,
• The printf() is defined in the stdio.h header file.
• The sqrt() function is defined in the math.h header
file.
• The strlen() function is defined in the string.h
header file.6/30/2020 NIMMY RAJU,AP,VKCET,TVM 5
User-defined function
• User can also create functions as per their own
needs.
• Such functions created by users are known as
user-defined functions.
Advantages of user-defined function
• easier to understand, maintain and debug.
• Reusable codes
• A large program can be divided among many
programmers.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 6
• Three parts of a user defined functions are:
– Function Declaration or Prototype
– Function Definition
– Function Call
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 7
Function Declaration or function prototype
• A function prototype or function declaration is simply
the declaration of a function that specifies function's
name, parameters and return type.
• It doesn't contain function body.
• A function prototype gives information to the compiler
that the function may later be used in the program.
Syntax
• return_type function_name( parameter list );
• int add(int a, int b);6/30/2020 NIMMY RAJU,AP,VKCET,TVM 8
Function Definition
• A function definition in C programming consists
of a function header and a function body. Here
are all the parts of a function:
Return Type:
• A function may return a value.
• The return_type is the data type of the value the
function returns.
• Some functions perform the desired operations
without returning a value.
• In this case, the return_type is the keyword void.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 9
Function Definition
• Function Name: The actual name of the
function.
• Arguments: When a function is invoked, you
pass a value to the parameter. This value is
referred to as argument.
• Function Body: The function body contains a
collection of statements that define what the
function does.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 10
• Syntax:
return_type function_name( argument list )
{
body of the function
}
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 11
Function Call
• When a program calls a function from the
main function , the program control is
transferred to the called function.
• A called function performs a defined task and
when its return statement is executed or
when its function-ending closing brace is
reached, it returns the program control back
to the main program.
Syntax:
• functionName(parameter list);
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 12
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 13
• Return Statement
• The return statement terminates the
execution of a function and returns a value to
the calling function.
• The program control is transferred to the
calling function after the return statement.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 14
Types of user defined functions
Types INPUT OUTPUT
No arguments passed and
no return value
Sub function Sub function
Arguments passed but no
return value
Main function Sub function
No arguments passed but
return value
Sub function Main function
Arguments passed with
return value
Main function Main function
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 15
Without function Using function(No arguments passed and
no return value
)
#include<stdio.h>
void main()
{
int a,b,sum;
printf("Enter the values of
a & b");
scanf("%d%d",&a,&b);
sum=a+b;
printf("Sum=%d",sum);
}
#include<stdio.h>
void add();
void main()
{
add();
}
// function to read two number and
print its sum
void add()
{
int a,b,sum;
printf("Enter the values of a & b");
scanf("%d%d",&a,&b);
sum=a+b;
printf("Sum=%d",sum);
}
Program to find the sum of two numbers
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 16
Without function Using function(arguments passed and no
return value
)
#include<stdio.h>
void main()
{
int a,b,sum;
printf("Enter the values of a
& b");
scanf("%d%d",&a,&b);
sum=a+b;
printf("Sum=%d",sum);
}
#include<stdio.h>
void add(int ,int );
void main()
{
int a,b;
printf("Enter the values of a & b");
scanf("%d%d",&a,&b);
add(a,b);
}
// function that takes two arguments
and print their sum.
void add(int a,int b)
{
int sum= a + b;
printf("Sum=%d",sum);
}
Program to find the sum of two numbers
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 17
Without function Using function(No arguments passed and but
return value
)
#include<stdio.h>
void main()
{
int a,b,sum;
printf("Enter the values of a
& b");
scanf("%d%d",&a,&b);
sum=a+b;
printf("Sum=%d",sum);
}
#include<stdio.h>
int add( );
void main()
{
int S;
S=add();
printf("Sum=%d",S);
}
// function to read two number and
return its sum
int add()
{
int a,b,sum;
printf("Enter the values of a & b");
scanf("%d%d",&a,&b);
sum = a+b;
return sum;
}6/30/2020 NIMMY RAJU,AP,VKCET,TVM 18
Without function Using function(arguments passed with return
value
)
#include<stdio.h>
void main()
{
int a,b,sum;
printf("Enter the values of a
& b");
scanf("%d%d",&a,&b);
sum=a+b;
printf("Sum=%d",sum);
}
#include<stdio.h>
int add(int ,int );
void main()
{
int a,b,S;
printf("Enter the values of a & b");
scanf("%d%d",&a,&b);
S=add(a,b);
printf("Sum=%d",S);
}
// function that takes two arguments
and print their sum.
int add(int a,int b)
{
int sum;
sum = a+b;
return sum;
}
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 19
No arguments
passed and no
return value
No arguments
passed and no return
value
No arguments passed
and no return value
No arguments passed
and no return value
#include<stdio.h>
void large();
void main()
{
large();
}
void large()
{
int a,b;
printf("Enter the
values of a & b");
scanf("%d%d",&a,&b)
;
If(a>b)
printf(“Largest=%d",a
);
else
printf(“Largest=%d“,a
);
}
#include<stdio.h>
void large(int,int);
void main()
{
int a,b;
printf("Enter the values
of a & b");
scanf("%d%d",&a,&b);
large(a,b);
}
void large(int x,int y)
{
If(x>y)
printf(“Largest=%d",x);
else
printf(“Largest=%d“,y);
}
#include<stdio.h>
int large();
void main()
{
int l;
l=large();
printf(“Large=%d”,l);
}
int large()
{
int a,b;
printf("Enter the values of
a & b");
scanf("%d%d",&a,&b);
If(a>b)
return a;
else
return b;
}
#include<stdio.h>
int large(int,int);
void main()
{
int a,b,l;
printf("Enter the values
of a & b");
scanf("%d%d",&a,&b);
l=large(a,b);
printf(“Large=%d”,l);
int large(int a,int b)
{
If(a>b)
return a;
else
return b;
}
Largest of two numbers
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 20
• Formal and Actual Parameters
• Let us assume that a function B() is called
from another function A().
• In this case A is called the “caller functionor
calling function” and B is called the “called
function or callee function”.
• Also, the arguments which A sends to B are
called actual arguments and the parameters
of B are called formal arguments.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 21
• Formal Parameter: A variable and its type as
they appear in the prototype of the function
or method.
• Actual Parameter: The variable or expression
corresponding to a formal parameter that
appears in the function or method call in the
calling environment.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 22
Formal and Actual parameters
 Let us assume that a function B() is called from another function
A(). In this case A is called the “caller function or calling
function” and B is called the “called function or callee
function”.
 The arguments which A sends to B are called actual arguments
(parameters)and the arguments of B are called formal
arguments(parameters).
 Formal Parameter: A variable that appears in the prototype of
the function.
 Actual Parameter: The variable corresponding to a formal
parameter that appears in the function call in the calling function.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 23
Example:
#include<stdio.h>
int swap(int a,int b); //function declaration
void main()
{
int x=10,y=20;
printf("Before swapping : x=%dty=%dn",x,y);
swap(x,y); //function call
printf("After swapping : x=%dty=%d",x,y);
}
int swap(int a,int b) //function definition
{
int temp=a;
a=b;
b=temp;
printf("a=%dt b=%d",a,b);
}
Output
Before swapping: x=10 y=20
a=20 b=10
After swapping : x=10 y=20
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 24
Pass by value(Call by value)
• In this parameter passing technique, a copy of
actual parameter is passed to formal parameter.
• As a result, any changes or modification
happened to formal parameter won’t reflect
back to actual parameter
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 25
Example:
#include<stdio.h>
int swap(int a,int b); //function declaration
void main()
{
int x=10,y=20;
printf("Before swapping : x=%dty=%dn",x,y);
swap(x,y); //function call –pass by value
printf("After swapping : x=%dty=%d",x,y);
}
int swap(int a,int b) //function definition
{
int temp=a;
a=b;
b=temp;
printf("a=%dt b=%d",a,b);
}
Output
Before swapping: x=10 y=20
a=20 b=10
After swapping : x=10 y=20
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 26
Pass by reference(Call by reference)
• Both the actual and formal parameters refer to
same locations, so any changes made inside
the function are actually reflected in actual
parameters of caller.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 27
Example:
#include<stdio.h>
int swap(int *a,int *b); //function declaration
void main()
{
int x=10,y=20;
printf("Before swapping : x=%dty=%dn",x,y);
swap(&x,&y); //function call –pass by reference
printf("After swapping : x=%dty=%d",x,y);
}
int swap(int *a,int *b) //function definition
{
int temp=*a;
*a=*b;
*b=temp;
printf("a=%dt b=%d",*a,*b);
}
Output
Before swapping: x=10 y=20
a=20 b=10
After swapping : x=20 y=10
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 28
Recursive Function
• A function that calls itself is known as a
recursive function. And, this technique is known as
recursion.
• While using recursion, programmers need to be
careful to define an exit condition from the
function, otherwise it will go into an infinite loop.
• Recursion makes program elegant.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 29
Write a program to find factorial of number using recursive function
#include<stdio.h>
int fact(int n)
{
if( n == 0)
return 1;
return n*fact(n-1);
}
void main()
{
int n,f;
printf("Enter the number:");
scanf("%d",&n);
f-=fact(n);
printf("factorial=%d",f);
}
Output
Enter the number:5
factorial=120
fact(5) fact(4) fact(3) fact(2) fact(1) fact(0)
fact(5)
{
return
5*fact(4);
}
fact(4)
{
return
4*fact(3);
}
fact(3)
{
return
3*fact(2);
}
fact(2)
{
return
2*fact(1);
}
fact(1)
{
return
1*fact(0);
}
fact(0)
{
return 1;
}
5*24=120 4*6=24 3*2=6 2*1=2 1*1=1 1
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 30
Write a program to find sum of first n natural numbers using recursive function
#include<stdio.h>
int sum(int n)
{
if( n == 0)
return 0;
return n+sum(n-1);
}
void main()
{
int n,f;
printf("Enter the limit:");
scanf("%d",&n);
printf(“n Sum=%d",sum(n));
}
Output
Enter the limit:5
factorial=15
sum(5) sum(4) sum(3) sum(2) sum(1) sum(0)
sum(5)
{
return
5+sum(4);
}
sum(4)
{
return
4+sum(3);
}
sum(3)
{
return
3+sum(2);
}
sum(2)
{
return
2+sum(1);
}
sum(1)
{
return
1+sum(0);
}
sum(0)
{
return 0;
}
5+10=15 4+6=10 3+3=6 2+1=3 1+0=1 0
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 31
Passing an array as Parameter
 Like the value of simple variables, it is also
possible to pass the values of an array to a
function.
 To pass a single dimensional array to a
function, it is sufficient to list the name of the
array without any subscripts and the size of
the array as arguments
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 32
Rules to pass an Array to Function
 The function must be called by passing only the
name of the array.
 In function definition, formal parameter must be
an array type; the size of the array does not need
to be specified.
 The function prototype must show that the
argument is an array.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 33
Write a function to sort an array.
#include<stdio.h>
int sort(int A[],int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(A[j]>A[j+1])
{
temp = A[j];
A[j] = A[j+1];
A[j+1]= temp;
}
}
}
void main()
{
int A[30],i,n;
printf("Enter the limit:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the
element:");
scanf("%d",&A[i]);
}
sort(A,n);
printf("Sorted Arrayn");
for(i=0;i<n;i++)
printf("%dt",A[i]);
}
Output
Enter the limit:5
Enter the element:17
Enter the element:23
Enter the element:5
Enter the element:2
Enter the element:9
Sorted Array
2 5 9
17 23
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 34
• Passing 2 D array as parameter
• While passing 2D as parameter we need to
mention the max size of element of each row
that is column
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 35
Write a program to pass a 2 D matrix as parameter and find its sum of all the elements
#include<stdio.h>
// function that takes a 2 D
and its order
int sum(int A[][30],int m,int
n)
{
int i,j,sum =0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
sum = sum + A[i][j];
}
}
printf("Sum=%d",sum);
}
} void main()
{
int A[30][30];
int i,n,m,j;
printf("Enter the order of
matrix:");
scanf("%d%d",&m,&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("Enter the element:");
scanf("%d",&A[i][j]);
}
}
sum(A,m,n);
}
Output
Enter the order of
matrix:2 2
Enter the element:1
Enter the element:2
Enter the element:3
Enter the element:4
Sum=10
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 36
STRUCTURE
• A structure is a user defined data type in C.
• A structure creates a data type that can be
used to group items of possibly different types
into a single type.
• ‘struct’ keyword is used to create a structure.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 37
• Consider we want to create the structure of a
person with following variable name, age and
address.
• Then such a structure can be created as
struct Person
{
char name[30];
int age;
char addr[50];
}p;
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 38
The general format of a structure definition is as
follows
struct structure_name
{
data_type member1;
data_type member2;
---------------------
---------------------
}S;
Here S is called structure variable.6/30/2020 NIMMY RAJU,AP,VKCET,TVM 39
• A structure variable can either be declared
with structure declaration or as a separate
declaration like basic types.
struct structure_name
{
data_type member1;
data_type member2;
---------------------
---------------------
}:
void main()
{
struct structure_name S;
}
struct structure_name
{
data_type member1;
data_type member2;
---------------------
---------------------
}S;
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 40
Declare a structure namely Student to store the details (roll number,
name, mark_for_C) of two students and display the data.
#include<stdio.h>
struct Student
{
char name[30];
int rollnum;
int mark;
};
void main()
{
struct Student s1,s2;
printf("Enter the Student name:");
scanf("%s",s1.name);
printf("Enter the Student rollnum:");
scanf("%d",&s1.rollnum);
printf("Enter the Student Mark for C:");
scanf("%d",&s1.mark);
printf("Enter the Student name:");
scanf("%s",s2.name);
printf("Enter the Student rollnum:");
scanf("%d",&s2.rollnum);
printf("Enter the Student Mark for C:");
scanf("%d",&s2.mark);
printf("NametRoll NumbertMark for Cn");
printf("%st%dt%dn",s1.name,s1.rollnum,s1.m
ark);
printf("%st%dt%dn",s2.name,s2.rollnum,s2.m
ark);
}
Output
Enter the Student name:Sangeeth
Enter the Student rollnum:
Enter the Student Mark for C:35
Enter the Student name:Pratheesh
Enter the Student rollnum:24
Enter the Student Mark for C:40
Name Roll Number Mark for C
Sangeeth 27 35
Pratheesh 24 40
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 41
Array of Structures
 DeclareastructurenamelyStudenttostorethedetails(roll number, name, mark_for_C)
of a student. Then, write a program in C to find the average mark obtained by the
students in a class for the subject Programming in C (using the field mark_for_C). Use
array of structures to store the required data.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 42
Declare a structure namely Student to store the details (roll number,
name, mark_for_C) of two students and display the data.
#include<stdio.h>
struct Student
{
char name[30];
int rollnum;
int mark;
};
void main()
{
struct Student s1,s2;
printf("Enter the Student name:");
scanf("%s",s1.name);
printf("Enter the Student rollnum:");
scanf("%d",&s1.rollnum);
printf("Enter the Student Mark for C:");
scanf("%d",&s1.mark);
printf("Enter the Student name:");
scanf("%s",s2.name);
printf("Enter the Student rollnum:");
scanf("%d",&s2.rollnum);
printf("Enter the Student Mark for C:");
scanf("%d",&s2.mark);
printf("NametRoll NumbertMark for Cn");
printf("%st%dt%dn",s1.name,s1.rollnum,s1.m
ark);
printf("%st%dt%dn",s2.name,s2.rollnum,s2.m
ark);
}
Output
Enter the Student name:Sangeeth
Enter the Student rollnum:
Enter the Student Mark for C:35
Enter the Student name:Pratheesh
Enter the Student rollnum:24
Enter the Student Mark for C:40
Name Roll Number Mark for C
Sangeeth 27 35
Pratheesh 24 40
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 43
#include<stdio.h>
struct Student
{
char name[30];
int rollno;
int mark_for_C;
};
void main()
{
struct Student s[30];
int i,n,sum=0;
float avg;
printf("Enter the no
of Student:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the Student
name:");
scanf("%s",s[i].name);
printf("Enter the Student
rollno:");
scanf("%d",&s[i].rollno);
printf("Enter the Mark for C:");
scanf("%d",&s[i].mark_for_C);
}
printf("NametRoll
NumbertMark for Cn");
for(i=0;i<n;i++)
{
printf("%st%dt%dn",s[i].name,
s[i].rollnum,s[i].mark_for_C);
sum = sum + s[i].mark_for_C;
}
avg = (float)sum /n;
printf("Average Mark=%fn",avg);
}
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 44
Output
Enter the no of Student:3
Enter the Student name:Sangeeth
Enter the Student rollnum:27
Enter the Student Mark for C:35
Enter the Student name:Pratheesh
Enter the Student rollnum:24
Enter the Student Mark for C:40
Enter the Student name:Poornima
Enter the Student rollnum:26
Enter the Student Mark for C:45
Name Roll Number Mark for C
Sangeeth 27 35
Pratheesh 24 40
Poornima 26 45
Average Mark=40.00
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 45
UNION
 A union is a user-defined type similar to struct in C
programming.
 We use the union keyword to define unions.
Example of Employee Union creation and declartion
union Employee
{
char name[30]; int age;
double salary;
};
union Employee e;
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 46
Difference between structure and union
Structure Union
struct keyword is used to define a structure union keyword is used to define a union
Members do not share memory in a structure. Members share the memory space in a union
Any member can be retrieved at any time in a
structure
Only one member can be accessed at a time
in a union.
Several members of a structure can be
initialized at once.
Only the first member can be initialized.
Sizeofthestructureisequalto thesumofsize
of the each member.
Size of the union is equal to the size of the
largest member.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 47
#include<stdio.h>
struct Person
{
char pincode[6]; // Size = 6 bytes
int age; // Size = 4 bytes
double salary;// Size = 8 bytes
};
union Employee
{
char pincode[6];
int age;
double salary;
};
void main()
{
struct Person p;
union Employee e;
printf("Size of Structure Person=%dn",sizeof(p));
printf("Size of Union Employee=%d",sizeof(e));
}
Output
Size of Structure Person=18
Size of Union Employee=86/30/2020 NIMMY RAJU,AP,VKCET,TVM 48
Storage Classes, Scope and life
time of variables
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 49
• In C, not only do all the variables have a data
type, they also have a storage class.
• The following variable storage classes are
most relevant to functions
– Automatic Variables
– External or Global Variables
– Static Variables
– Register Variables
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 50
Automatic Variables
• Automatic variables are declared inside a
function in which they are to be utilized.
• They are created when the function is called
and destroyed automatically when the
function is exited, hence the name automatic.
• Automatic variables are therefore private or
local to the function in which they are
declared.
• Because of this property, automatic variables
are also referred to as local or internal
variables.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 51
Automatic Variables
• A variable declared inside a function without
storage class specification is by default an
automatic variable.
void main()
{
int num;
}
is same as
void main()
{
auto int num;
}
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 52
• Example
#include<stdio.h>
void func1()
{
int max=10;
printf("Max in func1()=%dn",max);
}
void func2()
{
int max=20;
printf("Max in func2()=%dn",max);
}
void main()
{
int max=30;
func1();
func2();
printf("Max in main()=%dn",max);
}
Output
Max in func1()=10
Max in func2()=20
Max in main()=30
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 53
External Variables
• Variables that are both alive and active
throughout the entire program are known as
external variables.
• They are called global variables.
• Unlike local variables, global variables can be
accessed by any function in the program.
• External global variables are global variables
visible to all file other than in which it is
declared6/30/2020 NIMMY RAJU,AP,VKCET,TVM 54
#include<stdio.h>
int max;
void func1()
{
printf("Max in func1()=%dn",max);
}
void func2()
{
printf("Max in func2()=%dn",max);
}
void main()
{
max=30;
func1();
func2();
printf("Max in main()=%dn",max);
}
Max in func1()=30
Max in func1()=30
Max in main()=30
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 55
Static Variables
• As the name suggests, the value of static
variables persists until the end of the
program.
• A variable can be declared static using the
keyword static like
static int x;
• Static global variables are global variables
visible only to the file in which it is declared.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 56
Register Variables
• We can tell the compiler that a variable should be
kept in one of the machine’s registers instead of
keeping in the memory.
• Since a register access is fast than a memory
access, keep in the frequently accessed variables
in the register will lead to faster execution of
programs.
• This is done as follows
register int count;
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 57
• The scope of a variable is the part of the
program within which the variable can be
used. So, the scope describes the visibility of
an identifier within the program.
• The lifetime of a variable or function is the
time duration for which memory is allocated
to store it, and when that memory is released.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 58
StorageClass Wheredeclared Visibility Lifetime
extern
Beforeallfunctionsinafile
Entire file plus other
files where variable is
declared.
Global
Static
Beforeallthefunctioninafile Onlyinthatfile
Global
Noneorauto
Inside afunction
Only in that function
orblock
Until end of
function
Register
Inside afunctionorblock
Onlyinthatfunction
orblock
Until end of
function
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 59

More Related Content

What's hot

Mitchell's Face Recognition
Mitchell's Face RecognitionMitchell's Face Recognition
Mitchell's Face Recognitionbutest
 
Longest Common Subsequence
Longest Common SubsequenceLongest Common Subsequence
Longest Common Subsequence
Swati Swati
 
Push down automata
Push down automataPush down automata
Push down automata
Ratnakar Mikkili
 
Lesson 03
Lesson 03Lesson 03
Lesson 03
maamir farooq
 
Dfa h11
Dfa h11Dfa h11
Dfa h11
Rajendran
 
Boosting Algorithms Omar Odibat
Boosting Algorithms Omar Odibat Boosting Algorithms Omar Odibat
Boosting Algorithms Omar Odibat
omarodibat
 
Introduction to the Theory of Computation, Winter 2003 A. Hevia and J. Mao S...
 Introduction to the Theory of Computation, Winter 2003 A. Hevia and J. Mao S... Introduction to the Theory of Computation, Winter 2003 A. Hevia and J. Mao S...
Introduction to the Theory of Computation, Winter 2003 A. Hevia and J. Mao S...
parmeet834
 
Unit 1 chapter 1 Design and Analysis of Algorithms
Unit 1   chapter 1 Design and Analysis of AlgorithmsUnit 1   chapter 1 Design and Analysis of Algorithms
Unit 1 chapter 1 Design and Analysis of Algorithms
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
Plsql lab mannual
Plsql lab mannualPlsql lab mannual
Plsql lab mannual
Harish Khodke
 
P, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-HardP, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-Hard
Animesh Chaturvedi
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
ASHOK KUMAR REDDY
 
Basic Blocks and Flow Graphs
Basic Blocks and Flow GraphsBasic Blocks and Flow Graphs
Basic Blocks and Flow Graphs
Jenny Galino
 
Simplification of cfg ppt
Simplification of cfg pptSimplification of cfg ppt
Simplification of cfg ppt
Shiela Rani
 
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
Madhu Bala
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
Dr Sandeep Kumar Poonia
 
Mealy moore machine model
Mealy moore machine modelMealy moore machine model
Mealy moore machine model
deepinderbedi
 
Artificial Neural Networks (ANNs) - XOR - Step-By-Step
Artificial Neural Networks (ANNs) - XOR - Step-By-StepArtificial Neural Networks (ANNs) - XOR - Step-By-Step
Artificial Neural Networks (ANNs) - XOR - Step-By-Step
Ahmed Gad
 
Pda
PdaPda
Lesson 04
Lesson 04Lesson 04

What's hot (20)

Mitchell's Face Recognition
Mitchell's Face RecognitionMitchell's Face Recognition
Mitchell's Face Recognition
 
Longest Common Subsequence
Longest Common SubsequenceLongest Common Subsequence
Longest Common Subsequence
 
Push down automata
Push down automataPush down automata
Push down automata
 
Lesson 03
Lesson 03Lesson 03
Lesson 03
 
Dfa h11
Dfa h11Dfa h11
Dfa h11
 
Boosting Algorithms Omar Odibat
Boosting Algorithms Omar Odibat Boosting Algorithms Omar Odibat
Boosting Algorithms Omar Odibat
 
Introduction to the Theory of Computation, Winter 2003 A. Hevia and J. Mao S...
 Introduction to the Theory of Computation, Winter 2003 A. Hevia and J. Mao S... Introduction to the Theory of Computation, Winter 2003 A. Hevia and J. Mao S...
Introduction to the Theory of Computation, Winter 2003 A. Hevia and J. Mao S...
 
Unit 1 chapter 1 Design and Analysis of Algorithms
Unit 1   chapter 1 Design and Analysis of AlgorithmsUnit 1   chapter 1 Design and Analysis of Algorithms
Unit 1 chapter 1 Design and Analysis of Algorithms
 
Plsql lab mannual
Plsql lab mannualPlsql lab mannual
Plsql lab mannual
 
P, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-HardP, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-Hard
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Turing machine by_deep
Turing machine by_deepTuring machine by_deep
Turing machine by_deep
 
Basic Blocks and Flow Graphs
Basic Blocks and Flow GraphsBasic Blocks and Flow Graphs
Basic Blocks and Flow Graphs
 
Simplification of cfg ppt
Simplification of cfg pptSimplification of cfg ppt
Simplification of cfg ppt
 
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
 
Mealy moore machine model
Mealy moore machine modelMealy moore machine model
Mealy moore machine model
 
Artificial Neural Networks (ANNs) - XOR - Step-By-Step
Artificial Neural Networks (ANNs) - XOR - Step-By-StepArtificial Neural Networks (ANNs) - XOR - Step-By-Step
Artificial Neural Networks (ANNs) - XOR - Step-By-Step
 
Pda
PdaPda
Pda
 
Lesson 04
Lesson 04Lesson 04
Lesson 04
 

Similar to EST 102 Programming in C-MODULE 4

unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
Venkatesh Goud
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
JVenkateshGoud
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
LPU
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
KarthikSivagnanam2
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
Amarjith C K
 
Function
Function Function
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman
 
eee2-day4-structures engineering college
eee2-day4-structures engineering collegeeee2-day4-structures engineering college
eee2-day4-structures engineering college
2017eee0459
 
PSPC-UNIT-4.pdf
PSPC-UNIT-4.pdfPSPC-UNIT-4.pdf
PSPC-UNIT-4.pdf
ArshiniGubbala3
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
UMA PARAMESWARI
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdf
JAVVAJI VENKATA RAO
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
Ashwini Raut
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Function C programming
Function C programmingFunction C programming
Function C programming
Appili Vamsi Krishna
 
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
SangeetaBorde3
 
Functions.pptx, programming language in c
Functions.pptx, programming language in cFunctions.pptx, programming language in c
Functions.pptx, programming language in c
floraaluoch3
 
Functions.pptx
Functions.pptxFunctions.pptx
Functions.pptx
AhmedHashim242567
 
U19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).pptU19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).ppt
Manivannan837728
 
Chap 5 c++
Chap 5 c++Chap 5 c++

Similar to EST 102 Programming in C-MODULE 4 (20)

unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
 
Function
Function Function
Function
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
eee2-day4-structures engineering college
eee2-day4-structures engineering collegeeee2-day4-structures engineering college
eee2-day4-structures engineering college
 
PSPC-UNIT-4.pdf
PSPC-UNIT-4.pdfPSPC-UNIT-4.pdf
PSPC-UNIT-4.pdf
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdf
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
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
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
Functions.pptx, programming language in c
Functions.pptx, programming language in cFunctions.pptx, programming language in c
Functions.pptx, programming language in c
 
Functions.pptx
Functions.pptxFunctions.pptx
Functions.pptx
 
U19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).pptU19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).ppt
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 

More from NIMMYRAJU

EST 102 Programming in C-MODULE 1
 EST 102 Programming in C-MODULE 1 EST 102 Programming in C-MODULE 1
EST 102 Programming in C-MODULE 1
NIMMYRAJU
 
EST 102 Programming in C-MODULE 2
EST 102 Programming in C-MODULE 2EST 102 Programming in C-MODULE 2
EST 102 Programming in C-MODULE 2
NIMMYRAJU
 
CS 402 DATAMINING AND WAREHOUSING -PROBLEMS
CS 402 DATAMINING AND WAREHOUSING -PROBLEMSCS 402 DATAMINING AND WAREHOUSING -PROBLEMS
CS 402 DATAMINING AND WAREHOUSING -PROBLEMS
NIMMYRAJU
 
CS 402 DATAMINING AND WAREHOUSING -MODULE 6
CS 402 DATAMINING AND WAREHOUSING -MODULE 6CS 402 DATAMINING AND WAREHOUSING -MODULE 6
CS 402 DATAMINING AND WAREHOUSING -MODULE 6
NIMMYRAJU
 
CS 402 DATAMINING AND WAREHOUSING -MODULE 5
CS 402 DATAMINING AND WAREHOUSING -MODULE 5CS 402 DATAMINING AND WAREHOUSING -MODULE 5
CS 402 DATAMINING AND WAREHOUSING -MODULE 5
NIMMYRAJU
 
CS 402 DATAMINING AND WAREHOUSING -MODULE 4
CS 402 DATAMINING AND WAREHOUSING -MODULE 4CS 402 DATAMINING AND WAREHOUSING -MODULE 4
CS 402 DATAMINING AND WAREHOUSING -MODULE 4
NIMMYRAJU
 
CS 402 DATAMINING AND WAREHOUSING -MODULE 3
CS 402 DATAMINING AND WAREHOUSING -MODULE 3CS 402 DATAMINING AND WAREHOUSING -MODULE 3
CS 402 DATAMINING AND WAREHOUSING -MODULE 3
NIMMYRAJU
 
CS 402 DATAMINING AND WAREHOUSING -MODULE 2
CS 402 DATAMINING AND WAREHOUSING -MODULE 2CS 402 DATAMINING AND WAREHOUSING -MODULE 2
CS 402 DATAMINING AND WAREHOUSING -MODULE 2
NIMMYRAJU
 

More from NIMMYRAJU (8)

EST 102 Programming in C-MODULE 1
 EST 102 Programming in C-MODULE 1 EST 102 Programming in C-MODULE 1
EST 102 Programming in C-MODULE 1
 
EST 102 Programming in C-MODULE 2
EST 102 Programming in C-MODULE 2EST 102 Programming in C-MODULE 2
EST 102 Programming in C-MODULE 2
 
CS 402 DATAMINING AND WAREHOUSING -PROBLEMS
CS 402 DATAMINING AND WAREHOUSING -PROBLEMSCS 402 DATAMINING AND WAREHOUSING -PROBLEMS
CS 402 DATAMINING AND WAREHOUSING -PROBLEMS
 
CS 402 DATAMINING AND WAREHOUSING -MODULE 6
CS 402 DATAMINING AND WAREHOUSING -MODULE 6CS 402 DATAMINING AND WAREHOUSING -MODULE 6
CS 402 DATAMINING AND WAREHOUSING -MODULE 6
 
CS 402 DATAMINING AND WAREHOUSING -MODULE 5
CS 402 DATAMINING AND WAREHOUSING -MODULE 5CS 402 DATAMINING AND WAREHOUSING -MODULE 5
CS 402 DATAMINING AND WAREHOUSING -MODULE 5
 
CS 402 DATAMINING AND WAREHOUSING -MODULE 4
CS 402 DATAMINING AND WAREHOUSING -MODULE 4CS 402 DATAMINING AND WAREHOUSING -MODULE 4
CS 402 DATAMINING AND WAREHOUSING -MODULE 4
 
CS 402 DATAMINING AND WAREHOUSING -MODULE 3
CS 402 DATAMINING AND WAREHOUSING -MODULE 3CS 402 DATAMINING AND WAREHOUSING -MODULE 3
CS 402 DATAMINING AND WAREHOUSING -MODULE 3
 
CS 402 DATAMINING AND WAREHOUSING -MODULE 2
CS 402 DATAMINING AND WAREHOUSING -MODULE 2CS 402 DATAMINING AND WAREHOUSING -MODULE 2
CS 402 DATAMINING AND WAREHOUSING -MODULE 2
 

Recently uploaded

Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
AhmedHussein950959
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
Kamal Acharya
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
Kamal Acharya
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
ShahidSultan24
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
PrashantGoswami42
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
MuhammadTufail242431
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
Jayaprasanna4
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 

Recently uploaded (20)

Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 

EST 102 Programming in C-MODULE 4

  • 1. Introduction to modular programming, writing functions, formal parameters, actual parameters .Pass by Value, Recursion, Arrays as Function Parameters ,structure, union, Storage Classes, Scope and life time of variables MODULE 4 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 1
  • 2. Functions • A function is a group of statements that together perform a task. • Every C program has at least one function, which is main(). 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 2
  • 3. Why to use Functions? • Development can be divided • Readable programs • Programming errors are easy to detect • Allows re-use of codes • Improves manageability • Collaboration 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 3
  • 4. Types of Functions There are two types of functions in C programming: • Standard library functions • User-defined functions 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 4
  • 5. Standard Library Functions • The standard library functions are built-in functions in C programming. • These functions are defined in header files. For example, • The printf() is defined in the stdio.h header file. • The sqrt() function is defined in the math.h header file. • The strlen() function is defined in the string.h header file.6/30/2020 NIMMY RAJU,AP,VKCET,TVM 5
  • 6. User-defined function • User can also create functions as per their own needs. • Such functions created by users are known as user-defined functions. Advantages of user-defined function • easier to understand, maintain and debug. • Reusable codes • A large program can be divided among many programmers. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 6
  • 7. • Three parts of a user defined functions are: – Function Declaration or Prototype – Function Definition – Function Call 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 7
  • 8. Function Declaration or function prototype • A function prototype or function declaration is simply the declaration of a function that specifies function's name, parameters and return type. • It doesn't contain function body. • A function prototype gives information to the compiler that the function may later be used in the program. Syntax • return_type function_name( parameter list ); • int add(int a, int b);6/30/2020 NIMMY RAJU,AP,VKCET,TVM 8
  • 9. Function Definition • A function definition in C programming consists of a function header and a function body. Here are all the parts of a function: Return Type: • A function may return a value. • The return_type is the data type of the value the function returns. • Some functions perform the desired operations without returning a value. • In this case, the return_type is the keyword void. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 9
  • 10. Function Definition • Function Name: The actual name of the function. • Arguments: When a function is invoked, you pass a value to the parameter. This value is referred to as argument. • Function Body: The function body contains a collection of statements that define what the function does. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 10
  • 11. • Syntax: return_type function_name( argument list ) { body of the function } 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 11
  • 12. Function Call • When a program calls a function from the main function , the program control is transferred to the called function. • A called function performs a defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns the program control back to the main program. Syntax: • functionName(parameter list); 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 12
  • 14. • Return Statement • The return statement terminates the execution of a function and returns a value to the calling function. • The program control is transferred to the calling function after the return statement. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 14
  • 15. Types of user defined functions Types INPUT OUTPUT No arguments passed and no return value Sub function Sub function Arguments passed but no return value Main function Sub function No arguments passed but return value Sub function Main function Arguments passed with return value Main function Main function 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 15
  • 16. Without function Using function(No arguments passed and no return value ) #include<stdio.h> void main() { int a,b,sum; printf("Enter the values of a & b"); scanf("%d%d",&a,&b); sum=a+b; printf("Sum=%d",sum); } #include<stdio.h> void add(); void main() { add(); } // function to read two number and print its sum void add() { int a,b,sum; printf("Enter the values of a & b"); scanf("%d%d",&a,&b); sum=a+b; printf("Sum=%d",sum); } Program to find the sum of two numbers 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 16
  • 17. Without function Using function(arguments passed and no return value ) #include<stdio.h> void main() { int a,b,sum; printf("Enter the values of a & b"); scanf("%d%d",&a,&b); sum=a+b; printf("Sum=%d",sum); } #include<stdio.h> void add(int ,int ); void main() { int a,b; printf("Enter the values of a & b"); scanf("%d%d",&a,&b); add(a,b); } // function that takes two arguments and print their sum. void add(int a,int b) { int sum= a + b; printf("Sum=%d",sum); } Program to find the sum of two numbers 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 17
  • 18. Without function Using function(No arguments passed and but return value ) #include<stdio.h> void main() { int a,b,sum; printf("Enter the values of a & b"); scanf("%d%d",&a,&b); sum=a+b; printf("Sum=%d",sum); } #include<stdio.h> int add( ); void main() { int S; S=add(); printf("Sum=%d",S); } // function to read two number and return its sum int add() { int a,b,sum; printf("Enter the values of a & b"); scanf("%d%d",&a,&b); sum = a+b; return sum; }6/30/2020 NIMMY RAJU,AP,VKCET,TVM 18
  • 19. Without function Using function(arguments passed with return value ) #include<stdio.h> void main() { int a,b,sum; printf("Enter the values of a & b"); scanf("%d%d",&a,&b); sum=a+b; printf("Sum=%d",sum); } #include<stdio.h> int add(int ,int ); void main() { int a,b,S; printf("Enter the values of a & b"); scanf("%d%d",&a,&b); S=add(a,b); printf("Sum=%d",S); } // function that takes two arguments and print their sum. int add(int a,int b) { int sum; sum = a+b; return sum; } 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 19
  • 20. No arguments passed and no return value No arguments passed and no return value No arguments passed and no return value No arguments passed and no return value #include<stdio.h> void large(); void main() { large(); } void large() { int a,b; printf("Enter the values of a & b"); scanf("%d%d",&a,&b) ; If(a>b) printf(“Largest=%d",a ); else printf(“Largest=%d“,a ); } #include<stdio.h> void large(int,int); void main() { int a,b; printf("Enter the values of a & b"); scanf("%d%d",&a,&b); large(a,b); } void large(int x,int y) { If(x>y) printf(“Largest=%d",x); else printf(“Largest=%d“,y); } #include<stdio.h> int large(); void main() { int l; l=large(); printf(“Large=%d”,l); } int large() { int a,b; printf("Enter the values of a & b"); scanf("%d%d",&a,&b); If(a>b) return a; else return b; } #include<stdio.h> int large(int,int); void main() { int a,b,l; printf("Enter the values of a & b"); scanf("%d%d",&a,&b); l=large(a,b); printf(“Large=%d”,l); int large(int a,int b) { If(a>b) return a; else return b; } Largest of two numbers 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 20
  • 21. • Formal and Actual Parameters • Let us assume that a function B() is called from another function A(). • In this case A is called the “caller functionor calling function” and B is called the “called function or callee function”. • Also, the arguments which A sends to B are called actual arguments and the parameters of B are called formal arguments. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 21
  • 22. • Formal Parameter: A variable and its type as they appear in the prototype of the function or method. • Actual Parameter: The variable or expression corresponding to a formal parameter that appears in the function or method call in the calling environment. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 22
  • 23. Formal and Actual parameters  Let us assume that a function B() is called from another function A(). In this case A is called the “caller function or calling function” and B is called the “called function or callee function”.  The arguments which A sends to B are called actual arguments (parameters)and the arguments of B are called formal arguments(parameters).  Formal Parameter: A variable that appears in the prototype of the function.  Actual Parameter: The variable corresponding to a formal parameter that appears in the function call in the calling function. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 23
  • 24. Example: #include<stdio.h> int swap(int a,int b); //function declaration void main() { int x=10,y=20; printf("Before swapping : x=%dty=%dn",x,y); swap(x,y); //function call printf("After swapping : x=%dty=%d",x,y); } int swap(int a,int b) //function definition { int temp=a; a=b; b=temp; printf("a=%dt b=%d",a,b); } Output Before swapping: x=10 y=20 a=20 b=10 After swapping : x=10 y=20 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 24
  • 25. Pass by value(Call by value) • In this parameter passing technique, a copy of actual parameter is passed to formal parameter. • As a result, any changes or modification happened to formal parameter won’t reflect back to actual parameter 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 25
  • 26. Example: #include<stdio.h> int swap(int a,int b); //function declaration void main() { int x=10,y=20; printf("Before swapping : x=%dty=%dn",x,y); swap(x,y); //function call –pass by value printf("After swapping : x=%dty=%d",x,y); } int swap(int a,int b) //function definition { int temp=a; a=b; b=temp; printf("a=%dt b=%d",a,b); } Output Before swapping: x=10 y=20 a=20 b=10 After swapping : x=10 y=20 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 26
  • 27. Pass by reference(Call by reference) • Both the actual and formal parameters refer to same locations, so any changes made inside the function are actually reflected in actual parameters of caller. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 27
  • 28. Example: #include<stdio.h> int swap(int *a,int *b); //function declaration void main() { int x=10,y=20; printf("Before swapping : x=%dty=%dn",x,y); swap(&x,&y); //function call –pass by reference printf("After swapping : x=%dty=%d",x,y); } int swap(int *a,int *b) //function definition { int temp=*a; *a=*b; *b=temp; printf("a=%dt b=%d",*a,*b); } Output Before swapping: x=10 y=20 a=20 b=10 After swapping : x=20 y=10 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 28
  • 29. Recursive Function • A function that calls itself is known as a recursive function. And, this technique is known as recursion. • While using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an infinite loop. • Recursion makes program elegant. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 29
  • 30. Write a program to find factorial of number using recursive function #include<stdio.h> int fact(int n) { if( n == 0) return 1; return n*fact(n-1); } void main() { int n,f; printf("Enter the number:"); scanf("%d",&n); f-=fact(n); printf("factorial=%d",f); } Output Enter the number:5 factorial=120 fact(5) fact(4) fact(3) fact(2) fact(1) fact(0) fact(5) { return 5*fact(4); } fact(4) { return 4*fact(3); } fact(3) { return 3*fact(2); } fact(2) { return 2*fact(1); } fact(1) { return 1*fact(0); } fact(0) { return 1; } 5*24=120 4*6=24 3*2=6 2*1=2 1*1=1 1 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 30
  • 31. Write a program to find sum of first n natural numbers using recursive function #include<stdio.h> int sum(int n) { if( n == 0) return 0; return n+sum(n-1); } void main() { int n,f; printf("Enter the limit:"); scanf("%d",&n); printf(“n Sum=%d",sum(n)); } Output Enter the limit:5 factorial=15 sum(5) sum(4) sum(3) sum(2) sum(1) sum(0) sum(5) { return 5+sum(4); } sum(4) { return 4+sum(3); } sum(3) { return 3+sum(2); } sum(2) { return 2+sum(1); } sum(1) { return 1+sum(0); } sum(0) { return 0; } 5+10=15 4+6=10 3+3=6 2+1=3 1+0=1 0 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 31
  • 32. Passing an array as Parameter  Like the value of simple variables, it is also possible to pass the values of an array to a function.  To pass a single dimensional array to a function, it is sufficient to list the name of the array without any subscripts and the size of the array as arguments 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 32
  • 33. Rules to pass an Array to Function  The function must be called by passing only the name of the array.  In function definition, formal parameter must be an array type; the size of the array does not need to be specified.  The function prototype must show that the argument is an array. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 33
  • 34. Write a function to sort an array. #include<stdio.h> int sort(int A[],int n) { int i,j,temp; for(i=0;i<n-1;i++) { for(j=0;j<n-i-1;j++) { if(A[j]>A[j+1]) { temp = A[j]; A[j] = A[j+1]; A[j+1]= temp; } } } void main() { int A[30],i,n; printf("Enter the limit:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the element:"); scanf("%d",&A[i]); } sort(A,n); printf("Sorted Arrayn"); for(i=0;i<n;i++) printf("%dt",A[i]); } Output Enter the limit:5 Enter the element:17 Enter the element:23 Enter the element:5 Enter the element:2 Enter the element:9 Sorted Array 2 5 9 17 23 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 34
  • 35. • Passing 2 D array as parameter • While passing 2D as parameter we need to mention the max size of element of each row that is column 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 35
  • 36. Write a program to pass a 2 D matrix as parameter and find its sum of all the elements #include<stdio.h> // function that takes a 2 D and its order int sum(int A[][30],int m,int n) { int i,j,sum =0; for(i=0;i<m;i++) { for(j=0;j<n;j++) { sum = sum + A[i][j]; } } printf("Sum=%d",sum); } } void main() { int A[30][30]; int i,n,m,j; printf("Enter the order of matrix:"); scanf("%d%d",&m,&n); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("Enter the element:"); scanf("%d",&A[i][j]); } } sum(A,m,n); } Output Enter the order of matrix:2 2 Enter the element:1 Enter the element:2 Enter the element:3 Enter the element:4 Sum=10 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 36
  • 37. STRUCTURE • A structure is a user defined data type in C. • A structure creates a data type that can be used to group items of possibly different types into a single type. • ‘struct’ keyword is used to create a structure. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 37
  • 38. • Consider we want to create the structure of a person with following variable name, age and address. • Then such a structure can be created as struct Person { char name[30]; int age; char addr[50]; }p; 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 38
  • 39. The general format of a structure definition is as follows struct structure_name { data_type member1; data_type member2; --------------------- --------------------- }S; Here S is called structure variable.6/30/2020 NIMMY RAJU,AP,VKCET,TVM 39
  • 40. • A structure variable can either be declared with structure declaration or as a separate declaration like basic types. struct structure_name { data_type member1; data_type member2; --------------------- --------------------- }: void main() { struct structure_name S; } struct structure_name { data_type member1; data_type member2; --------------------- --------------------- }S; 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 40
  • 41. Declare a structure namely Student to store the details (roll number, name, mark_for_C) of two students and display the data. #include<stdio.h> struct Student { char name[30]; int rollnum; int mark; }; void main() { struct Student s1,s2; printf("Enter the Student name:"); scanf("%s",s1.name); printf("Enter the Student rollnum:"); scanf("%d",&s1.rollnum); printf("Enter the Student Mark for C:"); scanf("%d",&s1.mark); printf("Enter the Student name:"); scanf("%s",s2.name); printf("Enter the Student rollnum:"); scanf("%d",&s2.rollnum); printf("Enter the Student Mark for C:"); scanf("%d",&s2.mark); printf("NametRoll NumbertMark for Cn"); printf("%st%dt%dn",s1.name,s1.rollnum,s1.m ark); printf("%st%dt%dn",s2.name,s2.rollnum,s2.m ark); } Output Enter the Student name:Sangeeth Enter the Student rollnum: Enter the Student Mark for C:35 Enter the Student name:Pratheesh Enter the Student rollnum:24 Enter the Student Mark for C:40 Name Roll Number Mark for C Sangeeth 27 35 Pratheesh 24 40 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 41
  • 42. Array of Structures  DeclareastructurenamelyStudenttostorethedetails(roll number, name, mark_for_C) of a student. Then, write a program in C to find the average mark obtained by the students in a class for the subject Programming in C (using the field mark_for_C). Use array of structures to store the required data. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 42
  • 43. Declare a structure namely Student to store the details (roll number, name, mark_for_C) of two students and display the data. #include<stdio.h> struct Student { char name[30]; int rollnum; int mark; }; void main() { struct Student s1,s2; printf("Enter the Student name:"); scanf("%s",s1.name); printf("Enter the Student rollnum:"); scanf("%d",&s1.rollnum); printf("Enter the Student Mark for C:"); scanf("%d",&s1.mark); printf("Enter the Student name:"); scanf("%s",s2.name); printf("Enter the Student rollnum:"); scanf("%d",&s2.rollnum); printf("Enter the Student Mark for C:"); scanf("%d",&s2.mark); printf("NametRoll NumbertMark for Cn"); printf("%st%dt%dn",s1.name,s1.rollnum,s1.m ark); printf("%st%dt%dn",s2.name,s2.rollnum,s2.m ark); } Output Enter the Student name:Sangeeth Enter the Student rollnum: Enter the Student Mark for C:35 Enter the Student name:Pratheesh Enter the Student rollnum:24 Enter the Student Mark for C:40 Name Roll Number Mark for C Sangeeth 27 35 Pratheesh 24 40 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 43
  • 44. #include<stdio.h> struct Student { char name[30]; int rollno; int mark_for_C; }; void main() { struct Student s[30]; int i,n,sum=0; float avg; printf("Enter the no of Student:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the Student name:"); scanf("%s",s[i].name); printf("Enter the Student rollno:"); scanf("%d",&s[i].rollno); printf("Enter the Mark for C:"); scanf("%d",&s[i].mark_for_C); } printf("NametRoll NumbertMark for Cn"); for(i=0;i<n;i++) { printf("%st%dt%dn",s[i].name, s[i].rollnum,s[i].mark_for_C); sum = sum + s[i].mark_for_C; } avg = (float)sum /n; printf("Average Mark=%fn",avg); } 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 44
  • 45. Output Enter the no of Student:3 Enter the Student name:Sangeeth Enter the Student rollnum:27 Enter the Student Mark for C:35 Enter the Student name:Pratheesh Enter the Student rollnum:24 Enter the Student Mark for C:40 Enter the Student name:Poornima Enter the Student rollnum:26 Enter the Student Mark for C:45 Name Roll Number Mark for C Sangeeth 27 35 Pratheesh 24 40 Poornima 26 45 Average Mark=40.00 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 45
  • 46. UNION  A union is a user-defined type similar to struct in C programming.  We use the union keyword to define unions. Example of Employee Union creation and declartion union Employee { char name[30]; int age; double salary; }; union Employee e; 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 46
  • 47. Difference between structure and union Structure Union struct keyword is used to define a structure union keyword is used to define a union Members do not share memory in a structure. Members share the memory space in a union Any member can be retrieved at any time in a structure Only one member can be accessed at a time in a union. Several members of a structure can be initialized at once. Only the first member can be initialized. Sizeofthestructureisequalto thesumofsize of the each member. Size of the union is equal to the size of the largest member. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 47
  • 48. #include<stdio.h> struct Person { char pincode[6]; // Size = 6 bytes int age; // Size = 4 bytes double salary;// Size = 8 bytes }; union Employee { char pincode[6]; int age; double salary; }; void main() { struct Person p; union Employee e; printf("Size of Structure Person=%dn",sizeof(p)); printf("Size of Union Employee=%d",sizeof(e)); } Output Size of Structure Person=18 Size of Union Employee=86/30/2020 NIMMY RAJU,AP,VKCET,TVM 48
  • 49. Storage Classes, Scope and life time of variables 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 49
  • 50. • In C, not only do all the variables have a data type, they also have a storage class. • The following variable storage classes are most relevant to functions – Automatic Variables – External or Global Variables – Static Variables – Register Variables 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 50
  • 51. Automatic Variables • Automatic variables are declared inside a function in which they are to be utilized. • They are created when the function is called and destroyed automatically when the function is exited, hence the name automatic. • Automatic variables are therefore private or local to the function in which they are declared. • Because of this property, automatic variables are also referred to as local or internal variables. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 51
  • 52. Automatic Variables • A variable declared inside a function without storage class specification is by default an automatic variable. void main() { int num; } is same as void main() { auto int num; } 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 52
  • 53. • Example #include<stdio.h> void func1() { int max=10; printf("Max in func1()=%dn",max); } void func2() { int max=20; printf("Max in func2()=%dn",max); } void main() { int max=30; func1(); func2(); printf("Max in main()=%dn",max); } Output Max in func1()=10 Max in func2()=20 Max in main()=30 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 53
  • 54. External Variables • Variables that are both alive and active throughout the entire program are known as external variables. • They are called global variables. • Unlike local variables, global variables can be accessed by any function in the program. • External global variables are global variables visible to all file other than in which it is declared6/30/2020 NIMMY RAJU,AP,VKCET,TVM 54
  • 55. #include<stdio.h> int max; void func1() { printf("Max in func1()=%dn",max); } void func2() { printf("Max in func2()=%dn",max); } void main() { max=30; func1(); func2(); printf("Max in main()=%dn",max); } Max in func1()=30 Max in func1()=30 Max in main()=30 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 55
  • 56. Static Variables • As the name suggests, the value of static variables persists until the end of the program. • A variable can be declared static using the keyword static like static int x; • Static global variables are global variables visible only to the file in which it is declared. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 56
  • 57. Register Variables • We can tell the compiler that a variable should be kept in one of the machine’s registers instead of keeping in the memory. • Since a register access is fast than a memory access, keep in the frequently accessed variables in the register will lead to faster execution of programs. • This is done as follows register int count; 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 57
  • 58. • The scope of a variable is the part of the program within which the variable can be used. So, the scope describes the visibility of an identifier within the program. • The lifetime of a variable or function is the time duration for which memory is allocated to store it, and when that memory is released. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 58
  • 59. StorageClass Wheredeclared Visibility Lifetime extern Beforeallfunctionsinafile Entire file plus other files where variable is declared. Global Static Beforeallthefunctioninafile Onlyinthatfile Global Noneorauto Inside afunction Only in that function orblock Until end of function Register Inside afunctionorblock Onlyinthatfunction orblock Until end of function 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 59