SlideShare a Scribd company logo
Program Structure
In earlier chapters, we've used local variables, which have scope within
the single function in which the variable is defined. Such variable is not
recognized in other functions. However, the same name is used in other
functions; it is required to re-define the variable. In other words, it reserves
different memory for both the variables however same name is used.
But, in some situations, it may require to define a variable in such a way
so that the scope of variable remains more than one function or throughout the
program from the point of its definition.
So, permanence of a variable and its scope within the program is
characterized with Storage Class. It shows the portion of program over which the
variable is recognized.
Hence, we can say that a variable has two characteristics, one data type
and another storage class.
The four types of storage class are –
automatic, external, static and register which can be defined using
keywords auto, extern, static and register respectively. Some typical variables
can be declared as follows:
auto int a,b,c;
extern char name[10];
static int x;
Automatic variable
Automatic variables are always declared within a function and its scope retains
within the function only in which they are declared. So, same name can be used
as variables in different functions and different memories are allocated for them.
Hence such variables are also known as local variables. While declaring
automatic variable within a function, it is not required to use auto keyword i.e.
auto is optional.
#include<stdio.h>
#include<conio.h>
#include<math.h>
float quad(int a, int b,int c, int i)
{
float x=(-b+i*(sqrt(pow(b,2)-4*a*c)))/(2*a);
return x;
}
void main()
{
clrscr();
auto int a=4,b=5,c=1;
float x,y;
x=quad(a,b,c,1);
y=quad(a,b,c,-1);
printf("n %f",x);
printf("n %f",y);
getch();
}
External variables
The scope of external variable extends from the point of definition throughout the
remainder of program. Since the variable is recognized throughout the program
from the point of declaration, it retains its values. Hence, external variable can be
assigned a value within a function and can be used within another function. It
provides a convenient way for transferring information back and forth between
functions without using arguments. One more, it can return more than one data
items from a function without using RETURN statement.
/* To find real roots of quadratic equation */
#include<stdio.h>
#include<conio.h>
#include<math.h>
int a=4,b=5,c=1;
float quad(int i)
{
float x=(-b+i*(sqrt(pow(b,2)-4*a*c)))/(2*a);
return x;
}
void main()
{
clrscr();
float x,y;
x=quad(1);
y=quad(-1);
printf("n %f",x);
printf("n %f",y);
getch();
}
/* To find real root of quadratic equation */
#include<stdio.h>
#include<conio.h>
#include<math.h>
extern int a=4,b=5,c=1;
float x,y;
void quad()
{
x=(-b+(sqrt(pow(b,2)-4*a*c)))/(2*a);
y=(-b-(sqrt(pow(b,2)-4*a*c)))/(2*a);
}
void main()
{
clrscr();
quad();
printf("n %f",x);
printf("n %f",y);
getch();
}
Static Variable
Static variable is defined within individual functions and therefore has the
same scope as automatic variable. So, it is similar to automatic variable i.e. local
to the current function in which it is defined. But, Static variable retains its
previous values throughout the life of program however the function (in which
static variables are defined) is re-called after exit.
Static variable is defined in the same way as automatic variable with
keyword static before data type of variables which shows static storage class.
#include<stdio.h>
#include<conio.h>
long int fact(int i)
{
static long int x=1;
return(x*=i);
}
void main()
{
long int y;
int a=6;
for(int i=1;i<=a;i++)
y=fact(i);
printf("n %ld",y);
}
It's unusual to define automatic or static variable having the same names as
external variables. If happened, local variables will take precedence over the
external variables. But it doesn't affect the values of external variables.
Here is one skeletal structure of a C program showing various storage class.
float a,b,c;
main()
{
static float a;
void dummy(void);
....
}
void dummy(void)
{
static int a;
int b;
........
}
Write programs using UDF(user defined function) different storage class.
1. to convert a character to uppercase using UDF.
2. to determine the greater number between two input integer numbers.
3. To check an input number is prime or composite
4. to find factorial of N.

More Related Content

What's hot (20)

PPT
storage class
student
 
PPT
Storage classes
Shanmughaneethi Velu
 
PPTX
Storage Class in C Progrmming
Kamal Acharya
 
PPTX
Global variables, sorting static variables,function and arrays,
Ahmad55ali
 
PPTX
11 lec 11 storage class
kapil078
 
PPT
Lecture 13 - Storage Classes
Md. Imran Hossain Showrov
 
PPTX
Storage classes
Puneet Rajput
 
DOC
Storage classess of C progamming
Appili Vamsi Krishna
 
PPTX
Storage class in C Language
Nitesh Kumar Pandey
 
PPTX
C language presentation
bainspreet
 
PPTX
Storage class
MANJULA_AP
 
PPT
Lecture 14 - Scope Rules
Md. Imran Hossain Showrov
 
PPTX
Method parameters in c#
Dr.Neeraj Kumar Pandey
 
PPT
Functions in c
SunithaVesalpu
 
PPT
Lecture 11 - Functions
Md. Imran Hossain Showrov
 
PPTX
Decision statements in c language
tanmaymodi4
 
PPTX
Type casting in c programming
Rumman Ansari
 
PDF
11 2. variable-scope rule,-storage_class
웅식 전
 
PPTX
Presentation on function
Abu Zaman
 
PDF
Storage classes arrays & functions in C Language
Jenish Bhavsar
 
storage class
student
 
Storage classes
Shanmughaneethi Velu
 
Storage Class in C Progrmming
Kamal Acharya
 
Global variables, sorting static variables,function and arrays,
Ahmad55ali
 
11 lec 11 storage class
kapil078
 
Lecture 13 - Storage Classes
Md. Imran Hossain Showrov
 
Storage classes
Puneet Rajput
 
Storage classess of C progamming
Appili Vamsi Krishna
 
Storage class in C Language
Nitesh Kumar Pandey
 
C language presentation
bainspreet
 
Storage class
MANJULA_AP
 
Lecture 14 - Scope Rules
Md. Imran Hossain Showrov
 
Method parameters in c#
Dr.Neeraj Kumar Pandey
 
Functions in c
SunithaVesalpu
 
Lecture 11 - Functions
Md. Imran Hossain Showrov
 
Decision statements in c language
tanmaymodi4
 
Type casting in c programming
Rumman Ansari
 
11 2. variable-scope rule,-storage_class
웅식 전
 
Presentation on function
Abu Zaman
 
Storage classes arrays & functions in C Language
Jenish Bhavsar
 

Similar to 5.program structure (20)

DOC
What is storage class
Isha Aggarwal
 
DOCX
Storage class
Kalaikumar Thangapandi
 
PPTX
Storage classes in c language
Tanmay Modi
 
PPTX
STORAGE CLASS.pptx
BU210535JeevanKishor
 
PDF
Storage Classes.pdf DJJKLF DKFF DSLKF. DSF; FD
AbcdR5
 
PPT
Storage classes
Leela Koneru
 
PPT
Storage classes
Leela Koneru
 
PPT
S torage class in C
kash95
 
PDF
Advanced C Programming Notes
Leslie Schulte
 
PDF
Latest C Interview Questions and Answers
DaisyWatson5
 
PPTX
Storage Class Specifiers
Reddhi Basu
 
PPTX
Storage classes
priyanka jain
 
PDF
C Programming Storage classes, Recursion
Sreedhar Chowdam
 
PDF
Storage class
Kathmandu University
 
PDF
Terms and Definitions.pdf
SheikhAbrarAhmad
 
PPTX
Storage_classes_and_Scope_rules.pptx
CheriviralaNikhil
 
PPTX
function in c
subam3
 
PDF
Arrays 2 Dimensional Unit 2 Part 1.pdf
Arpana Awasthi
 
What is storage class
Isha Aggarwal
 
Storage class
Kalaikumar Thangapandi
 
Storage classes in c language
Tanmay Modi
 
STORAGE CLASS.pptx
BU210535JeevanKishor
 
Storage Classes.pdf DJJKLF DKFF DSLKF. DSF; FD
AbcdR5
 
Storage classes
Leela Koneru
 
Storage classes
Leela Koneru
 
S torage class in C
kash95
 
Advanced C Programming Notes
Leslie Schulte
 
Latest C Interview Questions and Answers
DaisyWatson5
 
Storage Class Specifiers
Reddhi Basu
 
Storage classes
priyanka jain
 
C Programming Storage classes, Recursion
Sreedhar Chowdam
 
Storage class
Kathmandu University
 
Terms and Definitions.pdf
SheikhAbrarAhmad
 
Storage_classes_and_Scope_rules.pptx
CheriviralaNikhil
 
function in c
subam3
 
Arrays 2 Dimensional Unit 2 Part 1.pdf
Arpana Awasthi
 
Ad

More from Shankar Gangaju (20)

PDF
Tutorial no. 8
Shankar Gangaju
 
PDF
Tutorial no. 7
Shankar Gangaju
 
PDF
Tutorial no. 6
Shankar Gangaju
 
PDF
Tutorial no. 3(1)
Shankar Gangaju
 
PDF
Tutorial no. 5
Shankar Gangaju
 
PDF
Tutorial no. 4
Shankar Gangaju
 
PDF
Tutorial no. 2
Shankar Gangaju
 
PDF
Tutorial no. 1.doc
Shankar Gangaju
 
DOC
What is a computer
Shankar Gangaju
 
DOC
Pointer
Shankar Gangaju
 
DOC
Array
Shankar Gangaju
 
DOC
9.structure & union
Shankar Gangaju
 
DOC
6.array
Shankar Gangaju
 
DOC
4. function
Shankar Gangaju
 
DOC
3. control statement
Shankar Gangaju
 
DOC
2. operator
Shankar Gangaju
 
DOC
1. introduction to computer
Shankar Gangaju
 
PDF
Ads lab
Shankar Gangaju
 
PDF
Electromagnetic formula
Shankar Gangaju
 
DOCX
Electromagnetic formula
Shankar Gangaju
 
Tutorial no. 8
Shankar Gangaju
 
Tutorial no. 7
Shankar Gangaju
 
Tutorial no. 6
Shankar Gangaju
 
Tutorial no. 3(1)
Shankar Gangaju
 
Tutorial no. 5
Shankar Gangaju
 
Tutorial no. 4
Shankar Gangaju
 
Tutorial no. 2
Shankar Gangaju
 
Tutorial no. 1.doc
Shankar Gangaju
 
What is a computer
Shankar Gangaju
 
9.structure & union
Shankar Gangaju
 
4. function
Shankar Gangaju
 
3. control statement
Shankar Gangaju
 
2. operator
Shankar Gangaju
 
1. introduction to computer
Shankar Gangaju
 
Electromagnetic formula
Shankar Gangaju
 
Electromagnetic formula
Shankar Gangaju
 
Ad

Recently uploaded (20)

PDF
My Thoughts On Q&A- A Novel By Vikas Swarup
Niharika
 
PDF
Tips for Writing the Research Title with Examples
Thelma Villaflores
 
PPTX
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PPTX
K-Circle-Weekly-Quiz12121212-May2025.pptx
Pankaj Rodey
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PDF
John Keats introduction and list of his important works
vatsalacpr
 
PPTX
YSPH VMOC Special Report - Measles Outbreak Southwest US 7-20-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
PDF
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PPTX
ENGLISH 8 WEEK 3 Q1 - Analyzing the linguistic, historical, andor biographica...
OliverOllet
 
PDF
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
PPTX
Rules and Regulations of Madhya Pradesh Library Part-I
SantoshKumarKori2
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
My Thoughts On Q&A- A Novel By Vikas Swarup
Niharika
 
Tips for Writing the Research Title with Examples
Thelma Villaflores
 
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
K-Circle-Weekly-Quiz12121212-May2025.pptx
Pankaj Rodey
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
John Keats introduction and list of his important works
vatsalacpr
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 7-20-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
ENGLISH 8 WEEK 3 Q1 - Analyzing the linguistic, historical, andor biographica...
OliverOllet
 
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
Basics and rules of probability with real-life uses
ravatkaran694
 
Rules and Regulations of Madhya Pradesh Library Part-I
SantoshKumarKori2
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 

5.program structure

  • 1. Program Structure In earlier chapters, we've used local variables, which have scope within the single function in which the variable is defined. Such variable is not recognized in other functions. However, the same name is used in other functions; it is required to re-define the variable. In other words, it reserves different memory for both the variables however same name is used. But, in some situations, it may require to define a variable in such a way so that the scope of variable remains more than one function or throughout the program from the point of its definition. So, permanence of a variable and its scope within the program is characterized with Storage Class. It shows the portion of program over which the variable is recognized. Hence, we can say that a variable has two characteristics, one data type and another storage class. The four types of storage class are – automatic, external, static and register which can be defined using keywords auto, extern, static and register respectively. Some typical variables can be declared as follows: auto int a,b,c; extern char name[10]; static int x; Automatic variable Automatic variables are always declared within a function and its scope retains within the function only in which they are declared. So, same name can be used as variables in different functions and different memories are allocated for them. Hence such variables are also known as local variables. While declaring automatic variable within a function, it is not required to use auto keyword i.e. auto is optional. #include<stdio.h> #include<conio.h> #include<math.h> float quad(int a, int b,int c, int i) { float x=(-b+i*(sqrt(pow(b,2)-4*a*c)))/(2*a); return x; } void main() { clrscr(); auto int a=4,b=5,c=1;
  • 2. float x,y; x=quad(a,b,c,1); y=quad(a,b,c,-1); printf("n %f",x); printf("n %f",y); getch(); } External variables The scope of external variable extends from the point of definition throughout the remainder of program. Since the variable is recognized throughout the program from the point of declaration, it retains its values. Hence, external variable can be assigned a value within a function and can be used within another function. It provides a convenient way for transferring information back and forth between functions without using arguments. One more, it can return more than one data items from a function without using RETURN statement. /* To find real roots of quadratic equation */ #include<stdio.h> #include<conio.h> #include<math.h> int a=4,b=5,c=1; float quad(int i) { float x=(-b+i*(sqrt(pow(b,2)-4*a*c)))/(2*a); return x; } void main() { clrscr(); float x,y; x=quad(1); y=quad(-1); printf("n %f",x); printf("n %f",y); getch(); }
  • 3. /* To find real root of quadratic equation */ #include<stdio.h> #include<conio.h> #include<math.h> extern int a=4,b=5,c=1; float x,y; void quad() { x=(-b+(sqrt(pow(b,2)-4*a*c)))/(2*a); y=(-b-(sqrt(pow(b,2)-4*a*c)))/(2*a); } void main() { clrscr(); quad(); printf("n %f",x); printf("n %f",y); getch(); } Static Variable Static variable is defined within individual functions and therefore has the same scope as automatic variable. So, it is similar to automatic variable i.e. local to the current function in which it is defined. But, Static variable retains its previous values throughout the life of program however the function (in which static variables are defined) is re-called after exit. Static variable is defined in the same way as automatic variable with keyword static before data type of variables which shows static storage class. #include<stdio.h> #include<conio.h> long int fact(int i) { static long int x=1; return(x*=i);
  • 4. } void main() { long int y; int a=6; for(int i=1;i<=a;i++) y=fact(i); printf("n %ld",y); } It's unusual to define automatic or static variable having the same names as external variables. If happened, local variables will take precedence over the external variables. But it doesn't affect the values of external variables. Here is one skeletal structure of a C program showing various storage class. float a,b,c; main() { static float a; void dummy(void); .... } void dummy(void) { static int a; int b; ........ } Write programs using UDF(user defined function) different storage class. 1. to convert a character to uppercase using UDF. 2. to determine the greater number between two input integer numbers. 3. To check an input number is prime or composite 4. to find factorial of N.