Module 3
Module 3
Definition
Modular programming is defined as a software design technique that focuses on
separating the program functionality into independent, interchangeable
methods/modules. Each of them contains everything needed to execute only one aspect
of functionality.
Modularity is all about making blocks, and each block is made with the help of other
blocks. Every block in itself is solid and testable and can be stacked together to create
an entire application. Therefore, thinking about the concept of modularity is also like
building the whole architecture of the application.
Module
A module is defined as a part of a software program that contains one or more routines. When
we merge one or more modules, it makes up a program. Whenever a product is built on an
enterprise level, it is a built-in module, and each module performs different operations and
business. Modules are implemented in the program through interfaces. The introduction of
modularity allowed programmers to reuse prewritten code with new applications. Modules
are created and merged with compilers, in which each module performs a business or routine
operation within the program.
For example - SAP(System, Applications, and Products) comprises large modules like
finance, payroll, supply chain, etc. In terms of softwares example of a module is Microsoft
Word which uses Microsoft paint to help users create drawings and paintings.
o Code is easier to read - Working on modular programming makes code easier to read
because functions perform different tasks as compared to monolithic codes.
Sometimes modular programming can be a bit messy if we pass arguments and
variables in different functions. The use of modules should be done in a sensible
manner so as to avoid any problem. Functions should be neat, clean, and descriptive.
o Code is easier to test - In software, some functions perform fewer tasks and also
functions that perform numerous tasks. If the software is easily split using modules, it
becomes easier to test. We can also focus on the riskier functions during testing and
need more test cases to make it bug-free.
o Reusability - There are times where a piece of code is implemented everywhere in
our program. Instead of copying and pasting it, again and again, modularity gives us
the advantage of reusability so that we can pull our code from anywhere using
interfaces or libraries. The concept of reusability also reduces the size of our program.
o Faster fixes - Suppose there is an error in the payment options in any application, and
the bug needs to be removed. Modularity can be a great help because we know that
there will be a separate function that will contain the code of payments, and only that
function will only be rectified. Thus using modules to find and fixing bugs becomes
much more smooth and maintainable.
o Low-risk update - In modular programming, a defined layer of APIs protects things
that use it from making changes inside the library. Unless there is a change in the API,
there is a low risk for someone's code-breaking. For example, if you didn't have
explicit APIs and someone changed a function they thought was only used within that
same library (but it was used elsewhere), they could accidentally break something.
o Easy collaboration - Different developers work on a single piece of code in the team.
There are chances of conflicts when there's a git merge. This conflict can be reduced
if the code is split between more functions, files, repos, etc. We can also provide
ownership to specific code modules, where a team member can break them down into
smaller tasks.
o There is a need for extra time and budget for a product in modular programming.
o It is a challenging task to combine all the modules.
o Careful documentation is required so that other program modules are not affected.
o Some modules may partly repeat the task performed by other modules. Hence,
Modular programs need more memory space and extra time for execution.
o Integrating various modules into a single program may not be a task because different
people working on the design of different modules may not have the same style.
o It reduces the program's efficiency because testing and debugging are time-
consuming, where each function contains a thousand lines of code.
Previous Years University questions:
Q.1) Explain function call, function definition and function prototype with anexample. (5)
(July 2021)
Q.2) What is the purpose of function declaration and function definition and functioncall?
With examples illustrate their syntax. (5) (January 2017)
Q.3) What are the advantages of functions? Explain how it is implemented in C Language. (4)
(May 2019)
Q.4) enumerate three advantages of using functions. (3) (January 2017)
Q.5) What are functions? Explains the different types of functions in detail with an example
program for each type. (10) (January 2017)
Functions
• A function is a named unit of a group of program statements .This unit can be invoked
from other parts of the program.
• To write a program using function it contain three parts.
1. Function declaration/ function prototype
2. Function definition
3. Function call/ Accessing a function
2. Function definition
• The general form of a function definition is as given below.
type function name( parameter list)
{
body of the function;
}
• Where type is the basic data type, function name is the name of the function, the parameter
list is a comma-separated list of variables of a function referred to as its arguments.
Note:
Actual Parameters:
The parameters that appear in a function call statement are actual parameters.
Formal parameters :
The parameters that appear in function definition are formal parameters.
Eg:
Previous Years University questions:
Q.1) What is the purpose of „return‟ statement? Can multiple „return‟ statements be
included in a function? Justify your answer. (3) (September 2020)
Q.2) give the purpose of return statement. (January 2017)
Return Statement
• A C function may or may not return a value from the function. If you don't have
to return any value from the function, use void for the return type.
• If you want to return any value from the function, you need to use any data type
such as int, long, char, etc. The return type depends on the value to be returned
from the function.
purpose of return statement
• A return statement ends the execution of a function, and returns control to the
calling function. Execution resumes in the calling function at the point
immediately following the call. A return statement can return a value to the
calling function.
Method:1
Method:2
Q.2) WAP to print the cube of a given number using function. (function should return a value)
Q.2 ) WAP to find the factorial of a given number. (Function does not return a value)
#include<stdio.h>
#include<conio.h>
void check(int num)
{
int f=1,i; //assume given number is prime
for(i=2;i<=num/2;i++)
{
if(num%i==0)
{
f=0; //given number is not a prime
break;
}
} Output
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf("\nEnter a Number :");
scanf("%d",&num);
printf("\nGiven Number is : %d",num);
rev(num);
getch();
return;
}
Previous Years University questions:
Q.1) Define a C function checkprime( ) that accepts an integer argument and returns 1 if
the argument is prime, a 0 otherwise. Write a C program that invokes this function to
generate prime numbers between the given ranges. (5) (September 2020)
Q.2) Write a function for checking whether a counting number is prime or not. Using
this function write a program for displaying the prime numbers in first N
counting numbers. (5) (July 2021)
#include<stdio.h>
#include<conio.h>
int checkprime(int num)
{ Output
int i,f;
f=1;
for(i=2;i<=num/2;i++)
{
if(num%i==0)
{
f=0;
break;
}
}
return f;
}
void main()
{
int i,f,l,u;
clrscr();
printf("\n Enter the lower limit");
scanf("%d",&l);
printf("\n Enter the upper limit");
scanf("%d",&u);
for(i=l;i<=u;i++)
{
f=checkprime(i);
if(f==1)
{
printf("\n%d is prime",i);
}
else
{
printf("\n%d is not prime",i);
}
}
getch();
return;
}
[Link]. Ajayakumar.M.V, Department of IT, Toc
Q.4) WAP to check whether given number is Armstrong or not.
#include<stdio.h>
#include<conio.h>
void check (int num)
{
int temp,r,sum=0;
temp=num;
while(num>0)
{
r=num%10;
sum=sum+(r*r*r);
num=num/10;
}
if(temp==sum)
{
printf("\n%d is Armstrong Number",temp);
}
else
{
printf("\n%d is not Armstrong Number",temp);
}
}
void main()
Output
{
int num;
clrscr(); Enter a number : 153
printf("\nEnter a number : ");
153 is Armstrong Number
scanf("%d",&num);
check(num);
getch();
return;
}
Q.2 ) WAP to find the sum of array elements are using function.
#include<stdio.h>
#include<conio.h>
void Arraysum(int A[10],int n)
{
int i,sum=0;
float avg;
for(i=0;i<n;i++)
{
sum=sum+A[i];
}
avg=sum/n;
printf("\n Sum =%d",sum);
printf("\n Average =%f",avg);
}
void main()
{
int A[10],n,i;
clrscr();
printf("\nEnter the value of n :");
scanf("%d",&n); Output
printf("\nEnter the %d numbers into the array :",n);
for(i=0;i<n;i++)
Enter the value of n : 5
{
scanf("%d",&A[i]); Enter the 5 numbers into the array :
10
}
20
printf("\n given array elements are :\n");
30
for(i=0;i<n;i++)
40
{
50
printf("\n%d",A[i]);
}
Given array elements are:
Arraysum(A,n);
10
getch();
20
return;
30
}
40
50
Sum = 150
Average=30.0
Q,3 ) WAP for Bubble sort using function.
#include<stdio.h>
#include<conio.h>
void Bsort(int A[10],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(A[j]>A[j+1])
{
temp=A[j];
A[j]=A[j+1];
A[j+1]=temp;
}
}
}
#include<stdio.h>
#include<conio.h>
#include<stdio.h>
#include<conio.h>
void Lsearch(int A[10],int n,int snum)
{
int i,flag=0;
for(i=0;i<n;i++)
{
if(A[i]==snum)
{
flag=1;
break;
}
}
if(flag==1)
{
printf("\n Searching number %d is found in the array at
position: %d",snum,i+1);
}
else
{
printf("\nSearching number %d is not found in the
array",snum);
}
}
void main()
{
int A[10],n,i,snum;
clrscr();
printf("\nEnter the value of n :");
scanf("%d",&n);
printf("\nEnter the %d numbers into the array :",n);
for(i=0;i<n;i++)
{
scanf("%d",&A[i]);
}
printf("\n given array elements are :\n");
for(i=0;i<n;i++)
{
printf("\n%d",A[i]);
}
printf("\nEnter the searching number:");
scanf("%d",&snum);
Lsearch(A,n,snum);
getch();
return;
}
Output
Q.1) WAP to find the length of the given string using function.
#include<stdio.h>
#include<conio.h>
void slength(char str[10])
{
int i;
for(i=0;str[i]!='\0';i++)
{
}
printf("\nLength of the given String is %d",i);
}
void main()
{ Output
char str[10];
clrscr(); Enter a string : Anil
printf("\nEnter a string :");
gets(str); Given string is : Anil
printf("\n Given string is : %s",str);
slength(str); Length of the given String is 4
getch();
return;
}
Q.2) WAP to find the reverse of the given string using function.
include<stdio.h>
#include<conio.h>
void slength(char str[10])
{
int i,j;
for(i=0;str[i]!='\0';i++)
{
}
printf("\nLength of the given String is %d",i);
#include<stdio.h>
#include<conio.h>
void pal(char str[10])
{
int i,j,l,f=1;
for(i=0;str[i]!='\0';i++) //find the length of the given string
{
}
l=i;
j=i-1;
for(i=0;i<l/2;i++)
{
if(str[i]!=str[j])
{
f=0;
break;
}
j--;
}
if(f==1)
{
printf("\nGiven string is palindrome");
}
else
{
printf("\n Given string is not palindrome");
}
}
void main()
{
char str[10];
clrscr(); Output
printf("\nEnter a String :");
gets(str); Enter a string : amma
printf("\nGiven string is : %s",str);
pal(str); Given string is : amma
getch(); Given string is palindrome
return;
}
Q.4) WAP to concatenate two strings using function
#include<stdio.h>
#include<conio.h>
void stringcon(char str1[10],char str2[10])
{
int i,j;
char str3[20];
for(i=0;str1[i]!='\0';i++)
{
str3[i]=str1[i];
}
for(j=0;str2[j]!='\0';j++)
{
str3[i+j]=str2[j];
}
str3[i+j]='\0';
printf("\n First string is : %s",str1);
printf("\n Second string is : %s",str2);
printf("\nConcatenated String is : %s",str3);
}
void main()
{
char str1[10],str2[10],str3[20]; Output
int i,j;
clrscr(); Enter the first string : anil
printf("\nEnter the first string :");
gets(str1); Enter the second string :kumar
printf("\nEnter the second string :");
gets(str2); First string is : anil
stringcon(str1,str2); Second string is : kumar
getch();
return; Concatenated String is: anilkumar
}
Previous Years University questions:
Q.5) Write a C program to find the total number of vowels in a string using a
function. (3) (July 2021)
#include<stdio.h>
#include<conio.h>
void cvowels(char str[20])
{
int i,scount=0,ccount=0,vcount=0;
for(i=0;str[i]!='\0';i++)
{
if(str[i]==' ')
{
scount++;
}
else if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||
str[i]=='A'||str[i]=='E'||str[i]=='I'||str[i]=='O'||str[i]=='U')
{
vcount++;
}
else
{
ccount++;
}
}
printf("\nNumber of Space=%d",scount);
printf("\nNumber of Vowels=%d",vcount);
printf("\nNumber of consonant=%d",ccount);
}
void main()
{
char str[20];
clrscr();
printf("\nEnter a String : ");
gets(str);
printf("\n Given String is :%s",str);
cvowels(str);
getch();
return;
}
Output
Two-dimensional array & function
Q.1) WAP to find the transpose of the given matrix using function
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,m,n,A[3][3];
clrscr(); Output
printf("\nEnter the order matrix : A\n");
scanf("%d%d",&m,&n);
Enter the order matrix A : m n
printf("\n Read Matrix A\n");
Read Matrix A
for(i=0;i<m;i++)
{ 1 2
for(j=0;j<n;j++)
{ 3 4
scanf("%d",&A[i][j]);
5 6
}
} Given matrix A is
printf("\n Given matrix A is \n");
for(i=0;i<m;i++) 1 2
{
3 4
for(j=0;j<n;j++)
{ 5 6
printf("\t%d",A[i][j]);
} Transpose of Matrix A is
printf("\n");
1 3 5
}
trans(A,m,n); 2 4 6
getch();
return;
}
Q.2) WAP for Matrix Addition using function
}
Output
Q.3) WAP for Matrix Multiplication using function.
}
else
{
printf("\n Matrix Addition is not possible");
}
break;
case 3:printf("\nEnter the order of matrix A :");
scanf("%d%d",&m,&n);
printf("\n Read Matrix A \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&A[i][j]);
}
}
transpose(A,m,n);
break;
default:printf("\nInvalid choice");
}
printf("\n Do you wants to continue(y/n) :");
scanf("%s",&c);
}while(c=='y'||c=='Y');
getch();
return;
}
Previous Years University questions:
Q.1) Write a C program to find the largest element of each row of an m x n matrix and
place it in the last column of the corresponding row. Use function. (6) (December 2019)
#include<stdio.h>
#include<conio.h>
void mrlargest(int A[3][3],int m,int n)
{
int i,j,l;
printf("\n Given matrix A and Largest element of each raw\n");
for(i=0;i<m;i++)
{
``
l=A[i][0];
for(j=0;j<n;j++)
{
printf("\t%d",A[i][j]);
if(A[i][j]>l)
Output
{
l=A[i][j];
}
}
printf("\t%d",l);
l=0;
printf("\n");
}
}
void main()
{
int A[3][3],m,n,i,j;
clrscr();
printf("\nEnter the order of matrix A");
scanf("%d%d",&m,&n);
printf("\nRead the matrix A");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
` scanf("%d",&A[i][j]);
}
}
mrlargest(A,m,n);
getch();
return;
}
Two-dimensional character array & function
Q.1) WAP to sort the ‘n’ names using function.
Output
Call by value and Call by reference
• A function can be invoked in two manners.
1. Call by value and
2. Call by reference
Note:
Actual Parameters:
The parameters that appear in a function call statement are actual parameters.
Formal parameters :
The parameters that appear in function definition are formal parameters.
Note:
• Two special operator * and & are used with pointers.
1. & : The & is a unary operator that returns memory address of its operand.
2. * (Asterisk) : The * is a unary operator that returns the value of the variable.
void main()
{
int a,b;
clrscr();
printf("\n Enter the values of a and b");
scanf("%d%d",&a,&b);
printf("\n Given values are , a=%d and b=%d",a,b);
swap(a,b);
printf("\n After the swap function, a=%d b=%d",a,b);
getch();
return;
}
Output
Q.2) WAP to swap two values, by call by reference method
#include<stdio.h>
#include<conio.h>
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
printf("\nin function definition After swapping , the values are a=%d b=%d",*x,*y);
}
void main()
{
int a,b;
clrscr();
printf("\n Enter the values of a and b");
scanf("%d%d",&a,&b);
printf("\n Given values are , a=%d and b=%d",a,b);
swap(&a,&b);
printf("\n After the swap function, a=%d b=%d",a,b);
getch();
return;
}
Output
Previous Years University questions:
Q.1) Define recursion with an example. Differentiate between iteration and recursion.
(4) (December 2019)
Q.2) Using an example, explain the concept of recursion. (5) (December 2019)
Q.3) What are the advantages of recursive function? (2) (May 2019)
Q.4) With suitable example explain what you understand by recursion. (5) (January 2017)
Recursion
• In C, a function can call itself, this is called recursion.
• A function is said to be recursive if a statement in the body of the function calls
itself.
#include<stdio.h>
#include<conio.h>
int fact(int n)
{
if(n==1)
return 1;
else
return n*fact(n-1);
}
Output
void main()
{ Enter a number : 5
int n,res;
clrscr(); Factorial of 5 is 120
printf("\nEnter a number:");
scanf("%d",&n);
res=fact(n);
printf("\nFactorial of %d is %d",n,res);
getch();
return;
}
Previous Years University questions:
Q.1) Write a C program to print the Fibonacci series using recursion.(3) (DECEMBER 2018)
Q.2) Write a recursive function for finding the kth fibonacci number. Fibonacciseries is 1,
1, 2, 3, 5, 8, 13, 21, ........... (5) (July 2021)
#include<stdio.h>
#include<conio.h>
int fib(int n)
{
if(n==0)
return 0;
else if(n==1)
return 1;
else
return fib(n-1)+fib(n-2);
}
void main()
{
int n,i,res;
clrscr();
printf("\nEnter number of terms:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
res=fib(i);
printf("\t%d",res);
}
#include<stdio.h>
#include<conio.h>
int gcd(int a,int b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
void main()
{
int a,b,res;
clrscr();
printf("\nEnter the two numbers");
scanf("%d%d",&a,&b);
res=gcd(a,b);
printf("\tgcd( %d, %d)is%d",a,b,res);
getch();
return;
}
Structure
• One of the user defined data types
• Structure is a collection of variables of different data types that are referenced under
one name.
• Its keyword is struct
• The syntax of a structure definition takes the following form.
Accessing Structure elements
• Once a structure variable has been defined ,its members can be accessed through the
use of the . (dot) operator.
• The syntax for accessing structure element is
Structure variable . element name;
Union
• One of the user defined data types.
• Union have the same syntax as structure.
• Union is a collection of variables of different data types that are referenced under one
name.
• Its keyword is union.
• The syntax of a union definition takes the following form.
Difference between Structure & Union
[Link]. Ajayakumar.M.V, Department of IT, Toc H Institute of Science & Technology Page 45
[Link]. Ajayakumar.M.V, Department of IT, Toc H Institute of Science & Technology Page 46
Previous Years University questions:
Q.1) How does an array differ from a structure? (2) (DECEMBER 2018)
#include<stdio.h>
#include<conio.h>
struct student
{
int rno;
char name[10];
int marks[3];
int total;
double per;
}stud;
void read()
{ int i;
printf("\nEnter the roll number");
scanf("%d",&[Link]);
printf("\nEnter the name");
scanf("%s",[Link]);
printf("\nEnter the marks of 3 subjects");
[Link]=0;
for(i=0;i<3;i++)
{
scanf("%d",&[Link][i]);
[Link]=[Link]+[Link][i];
}
[Link]=[Link]/3;
}
void display()
{
Output
printf("\nMarklist\n");
printf("\n=========\n");
printf("\nRoll no:%d",[Link]);
printf("\nName:%s",[Link]);
printf("\nPhysics:%d",[Link][0]);
printf("\nChemistry:%d",[Link][1]);
printf("\nMathematics:%d",[Link][2]);
printf("\nTotal:%d",[Link]);
printf("\nPercentage:%lf",[Link]);
}
void main()
{
clrscr();
read();
display();
getch();
return;
}
Previous Years University questions:
Q.1) Explain array for structure with example. (3) (July 2021)
Array of Structures in C
#include<stdio.h>
#include<conio.h>
struct student
{
int rno;
char name[10];
int per;
}stud[5];
void read(int n)
{
int i;
for(i=0;i<n;i++)
{
printf("\nEnter the roll number: ");
scanf("%d",&stud[i].rno);
printf("\nEnter the Name: ");
scanf("%s",stud[i].name);
printf("\n enter the percentage: ");
scanf("%d",&stud[i].per);
}
}
void display(int n)
{
int i;
for(i=0;i<n;i++)
{
printf("\n%d",stud[i].rno);
printf("\t%s ",stud[i].name);
printf("\t%d",stud[i].per);
}
}
void main()
{
int n;
clrscr();
printf("\nEnter the value of n:");
scanf("%d",&n);
read(n);
printf("\nRoll No\tName\tpercentage\n");
display(n);
getch();
return;
}
Q.3 ) Using structure, read and print data of one employee (Name, Employee Id and
Salary)
#include<stdio.h>
#include<conio.h>
struct employee
{
int eid;
char name[10];
double salary;
}emp; Output
void read()
{
printf("\nEnter the employee id : ");
scanf("%d",&[Link]);
printf("\nEnter the employee name: ");
scanf("%s",[Link]);
printf("\nEnter the employee salary :");
scanf("%ld",&[Link]);
}
void display()
{
printf("\nEmployee id: %d",[Link]);
printf("\nEmployee name:%s",[Link]);
printf("\nEmployee salary:%ld",[Link]);
}
void main()
{
clrscr();
read();
display();
getch();
return;
}
Q.4 ) Using structure, read and print data of n employees (Name, Employee Id and
Salary)
#include<stdio.h>
#include<conio.h>
struct employee
{
int eid;
char name[10];
double salary;
}emp[5];
void read(int n)
{
int i;
for(i=0;i<n;i++)
{
printf("\nEnter the employee id : ");
scanf("%d",&emp[i].eid);
printf("\nEnter the employee name: ");
scanf("%s",emp[i].name);
printf("\nEnter the employee salary :");
scanf("%ld",&emp[i].salary);
}
} Output
void display(int n)
{
int i;
printf("\nEmp ID\tEmp Name\tEmp Salary\n");
for(i=0;i<n;i++)
{
printf("\n%d",emp[i].eid);
printf("\t%s",emp[i].name);
printf("\t\t%ld",emp[i].salary);
}
}
void main()
{
int n;
clrscr();
printf("\nEnter the value of n :");
scanf("%d",&n);
read(n);
display(n);
getch();
return;
}
Lab Cycle program
Q. 5) Read two input each representing the distances between two points in the
Euclidean space, store these in structure variables and add the two distance values.
#include<stdio.h>
#include<conio.h>
struct distance
{
int feet; // structure members
int inches;
}d1,d2,d3; //structure variables
void read()
{
printf("\nEnter the first distance (in Feet and Inches) :");
scanf("%d%d",&[Link],&[Link]);
void display()
{
printf("\n %d feet %d inches",[Link],[Link]);
printf("\n %d feet %d inches",[Link],[Link]);
printf("\n %d feet %d inches",[Link],[Link]);
}
void cal()
{
[Link]=([Link]+[Link])%12;
[Link]=([Link]+[Link])+([Link]+[Link])/12;
}
#include<stdio.h>
#include<conio.h>
struct Time
{
int hours;
int minutes;
int seconds;
}t1,t2,t3;
void read()
{
printf("\nEnter the first time(in Hours, Minutes and Seconds):");
scanf("%d%d%d",&[Link],&[Link],&[Link]);
printf("\nEnter the second time(in Hours, Minutes and Seconds):");
scanf("%d%d%d",&[Link],&[Link],&[Link]);
}
void display()
{
printf("\nTime 1 : %d Hours %d Minutes %d Seconds",[Link],[Link],[Link]);
printf("\nTime 2 : %d Hours %d Minutes %d Seconds",[Link],[Link],[Link]);
printf("\nTime 3 : %d Hours %d Minutes %d Seconds",[Link],[Link],[Link]);
}
void cal()
{
[Link]=([Link]+[Link])%60;
[Link]=(([Link]+[Link])+([Link]+[Link])/60)%60;
[Link]=([Link]+[Link])+(([Link]+[Link])+([Link]+[Link])/60)/60;
}
Output
void main()
{
clrscr();
read();
cal();
display();
getch();
return;
Q.1 Declare a union containing 5 string variables (Name, House Name, City Name,
State and Pin code) each with a length of C_SIZE (user defined constant). Then, read
and display the address of a person using a variable of the union.
#include<stdio.h>
#include<conio.h>
#define size 10
union address
{
char name[size];
char hname[size];
char cname[size];
char state[size];
char pin[size];
}s1;
void read()
{
printf("\nEnter the Name : ");
scanf("%s",[Link]);
printf("\nName:%s",[Link]);
printf("\nEnter the House Name : ");
scanf("%s",[Link]);
printf("\nHouse Name:%s",[Link]);
printf("\nEnter the City Name : ");
scanf("%s",[Link]);
printf("\nCity Name:%s",[Link]); Output
printf("\nEnter the State : ");
scanf("%s",[Link]);
printf("\nState:%s",[Link]);
printf("\nEnter the pin : ");
scanf("%s",[Link]);
printf("\npin :%s",[Link]);
}
void main()
{
int n;
clrscr();
read();
getch();
return;
}
Previous Years University questions:
Q.1) Explain register storage class with an example.( 3) (DECEMBER 2018)
Q.2) With suitable examples explain the various storage classes in C (8) (DECEMBER 2018)
Q.3) Compare different storage classes based on scope and lifetime of a variable. (3) (July 2021)
Q.4) Compare the features of automaic and static variables in C. (3) (July 2021)
Q.5) What are different storage classes in C? Give examples for each.(7) (January 2017)
Q.6) Explain the storage classes in C with appropriate example. (10) (January 2017)
Q.7) What is the use of a static variable? Explain with example. (5) (May 2019)
Q.8) with proper examples explain the storage classes in C (6) (January 2017)
Q.9) Describe the various storage classes in C. (4) (December 2019)
Q.10)Explain static storage class with an example. (3) (DECEMBER 2019)
Storage Classes in C
Storage classes in C are used to determine the lifetime, visibility, memory location, and
initial value of a variable. There are four types of storage classes in C
o Automatic
o External
o Static
o Register
• In storage class specifier, the syntax for declaration statement for a variable.
Syntax :
<storage class specifier> <data type> <variable name>;
Automatic storage class variable
• By default , all variables declared within the body of any functions are automatic.
• Its keyword is auto.
• They are created when the function is called and destroyed automatically when the
function is exited , hence the name automatic.
• Automatic variables are local to the function in which they are declared.
• Automatic variables are also referred to as local or internal variables.
• A variable declared inside a function without storage class specification is, by
default , an automatic variable.
• One important feature of automatic variables is that their value cannot be changed
accidentally by what happens in some other function in the program.
2. External storage class variables
• Variables that are active throughout the entire program are known as external
variable.
• They are also known as global variables.
• Unlike local variables, global variables can be accessed by any function in the
program.
• External variables are declared outside all function.
• Variables stored in registers of the CPU are accessed in much lesser time than those
stored in the primary memory.
• To allow the fastest access time for variables, the register storage class specifier is
used.
• The keyword for this storage class is register.
• The value of static variables exists (persists) until the end of the program.
• A variable can be declared static using the keyword static.
• A static variable may be either an internal type or an external type, depending on
the place of declaration.
• Internal static variables are those which are declared inside a function.
• The scope of internal static variables extend upto the end of the function in which
they are defined.
• Therefore , internal static variables can be used to retain values between function
calls.
Previous Years University questions:
Q.1) What are global variables? Give examples. (3) (September 2020)
Q.2) Differentiate between local variable and global variable.(3) (July 2021)
Q.3) Point out the difference between external variable definition and external variable
declaration. (3) (July 2021)
The scope of variables can be defined with their declaration, and variables are declared
mainly in two ways:
In the above example, we have declared x and y two variables inside the main
function. Hence these are local variables.
End of Module 3