Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 403

C Language

Basic Terminology
Computer:
It is an electronic device, It has memory and it
performs arithmetic and logical operations.
Input:
The data entering into computer is known as input.
Output:
The resultant information obtained by the computer
is known as output.
Program:
A sequence of instructions that can be executed by
the computer to solve the given problem is known as
program.
Software:

A set of programs to operate and controls the
operation of the computer is known as software.
these are 2 types.
1. ystem software.
2. Application software.
System Software:
It is used to manages system resources.
Eg ! "perating ystem.
Operating system:
It is an interface between user and the computer. In
other words operating system is a complex set of
programs which manages the resources of a
computer. #esources include input, output,
processor,memory,etc. o it is called as #esource
$anager.
Eg: %indows &',%indows(p,%indows),*nix,
+inux ,etc.
Application Software:
It is *sed to develop the applications.
It is again of 2 types.
3. +anguages
4. ,ackages.
Language:
It consists of set of executable instructions. *sing
these instructions we can communicate with the
computer and get the required results.
Eg: -, -..,/ava, etc.
Package:
It is designed by any other languages with limited
resources.
Eg: $ 0"ffice,Account ,ackage, etc.
Hardware:
All the physical components or units which are
connecting to the computer circuit is known as
1ardware.
ASCII character Set
ASCII - American Standard Code for Information
Interchange
There are 223 distinct A-II characters are used
by the micro computers. These values range from 4 to
222. These can be grouped as follows.
Character Type No of Characters
----------------------------------------------------------------
-
-apital +etters 5A to 67 23
mall +etters 5 a to 87 23
9igits 5 4 to &7 :4
pecial -haracters ;2
-ontrol -haracters ;<
=raphic -haracters :2'
000000000000000000000
Total 2!
000000000000000000000
"ut of 223, the first :2' are called as A-II
character set and the next :2' are called as extended
A-II character set. >ach and every character has
unique appearance.
Eg:
A to 6 32 to &4
a to 8 &) to :22
4 to & <' to 2)
>sc 2)
?ackspace '
>nter :;
pace?ar ;2
Tab &
etc.
Classification of programming languages:-
,rogramming languages are classifies into 2 types
5. +ow level languages
6. 1igh level languages
Low level languages:
It is also known as Assembly
language and was designed in the
beginning. It has some simple
instructions. These instructions are not
binary codes, but the computer can
understand only the machine
language, which is in binary format.
Hence a converter or translator is used
to translate the low level language
instructions into machine language.
This translator is called as assembler.
High level languages:
These are more English like languages
and hence the programmers found
them very easy to learn. To convert
high level language instructions to
machine language compilers and
interpreters are used.
Translators:
These are used to convert low or high
level language instructions to machine
language with the help of ASII
character set. There are ! types of
translators for languages.
7. Assembler :
It is used to convert low level
language instructions into machine
language.
8. Compiler:
It is used to convert high level
instructions into machine language.
It checks for the errors in the entire
program and converts the program
into machine language.
3)Interpreter:
It is also used to convert high level
language instructions into machine
language, "ut It checks for errors by
statement wise and converts into
machine language.
Debugging :
The process of correcting errors in
the program is
called as debugging.
Various Steps involved in program
development:
There are # steps involved in program
development.
1) Problem defnition:
$e%ning a problem is nothing but
understanding the problem. It involves
! speci%cations regarding a problem
solution.
9. . Input speci%cation
10. . &utput speci%cation
11. . 'rocessing
2)Analysis and design:
"efore going to make %nal solution
for the problem, the problem must be
analy(ed. &utline solution is prepared
for the simple problems. In case of
comple) problems, the main problem
is divided into sub problems called as
modules. Each module can be
handled and solved independently.
*hen the task is too large, it is always
better to divide the problem into
number of modulus and seek solution
for individual module.
3.Algorithm:
A step by step procedure to solve the
given problem is called as algorithm.
An algorithm can be described in a
natural language such as English.
4.lo! chart:
A symbolic or graphical
representation of given algorithm is
called as +ow chart.
".Coding and implementation:
oding is a process of converting
the algorithm or +ow chart into
computer program. In this process
each and every step of an algorithm
will be converted into instructions of
selected computer programming
language.
"efore selecting a programming
language we must fallow the following
! considerations.
a, -ature of program.
b,'rogramming language available
on
omputer system.
c) .imitations of the computer.
#.$ebugging and testing:
"efore loading the program into
computer, we must locate and correct
all the errors. The process of
correcting errors in a program is called
as debugging.
There are ! types of errors that
generally occur in a program.
a,Synta) errors
b,/untime errors
c,.ogical errors
It is very important to test the
program written to achieve a speci%c
task. Testing involves running the
program with known data of which the
results are known. As the results are
known, the results produced by the
computer can be veri%ed.
%.$ocumentation:
It is the most important aspect of
programming. It is a continuous
process to keep the copy of all the
phases involved in a problem
de%nition,000, debugging and
testing are parts of documentation.
This phase involves to producing a
written document for the user.
Algorithms:
"#To find addition$ su%traction$ multiplication$
di&ision and modulus of gi&en 2 num%ers'
Steps:
"' start
2. #ead Two @umbers A and ?
;. addAA.?
12. subAA0?
13. mulAAB?
14. divAAC?
15. modAAD?
16. print add, sub, mul, div and mod
17. stop.
2#To find ma(imum &alue of gi&en 2 &alues'
Steps:
18. start
19. #ead A and ?
20. maxAA
21. if 5max E ?7 then maxA?
22. print max
23. stop
)#To find ma(imum &alue of gi&en ) &alues
Steps:
24. start
25. #ead A, ? and -
26. maxAA
27. if5max E ?7 then maxA?
28. if5max E -7 then maxA-
29. print max
30. stop
*#To chec+ ,hether the gi&en num%er is e&en or
odd'
Steps:
31. start
32. #ead n
33. if5nD2A47 then print F=iven number is evenF
else print F=iven number is oddF
34. stop
#To display n natural num%ers'
Steps:
35. start
36. #ead n
37. iA:
38. print i
39. iAi.:
40. if5iEAn7 then goto step <
41. stop
!#To calculate and display the sum of n natural
num%ers'
Steps:
42. start
43. #ead n
44. sumA4,iA:
45. sumAsum.i
46. iAi.:
47. if5iEAn7 goto step <
48. print sum
49. stop
-#To find factorial of a gi&en num%er'
Steps:
50. start
51. #ead n
52. factA:
53. factAfactBn
54. nAn0:
55. if5nGA:7 then goto step <
56. print fact
57. stop
.#To find re&erse num%er of a gi&en num%er'
Steps:
58. start
59. #ead n
60. revA4
61. revA5revB:47.5nD:47
62. nAnC:4
63. if5nG47 then goto step <
64. print rev
65. stop
/#
Steps:
66. start
67. #ead n
68. revA4,mAn
69. revA5revB:47.5mD:47
70. mAmC:4
71. if5mG47 then goto step <
72. if 5nArev7 then print F=iven @umber is
,alindromeF
else print F=iven @umber is not ,alindromeF
73. stop
10)
Steps
1 start
! "ead n
3 sum#0
$ sum#sum%&n'10)
( n#n)10
* i+ &n,0) then goto srtp$
- print sum
. stop



/ntroduction to 0 :
is programming language. It is designed
by $ennis /itchie in 12#3 at AT 4T5American
Telephones and Telegraphs , "E.. labs in 6SA.
It is the most popular general purpose
programming language. *e can use the 77
lnaguage to implement any type of
applications.8ainly we are using language to
implement system softwares. These are
compilers ,editors, drivers ,databases and
operating systems.
Histor1 o+ 0 Language:
In 129:7s &"&. was being used for
commercial applications and ;&/T/A- is used
for scienti%c and engineering applications. At
this stage people started to develop a language
which is suitable for all possible applications.
Therefore an international committee was setup
to develop such a language < A.=&. 9: < was
released. It was not popular , because it seemed
too general. To reduce these generality a new
language '.5combined programming language,
was developed at ambridge 6niversity. It has
very less features. Then some other features
were added to this language and a new
language called "'.5"asic combined
programming language, developed by <8artin
/ichards< at ambridge 6niversity. Then < " <
language was developed by >?en Thompson@ at
AT4T "E.. labs.

$ennis /itchie inherited the features of "
and "'., added his own features and
developed language in 12#3.
2eatures o+ 304 Language:
1. is a structured programming
language with fundamental +ow
control construction.
3 . is simple and versatile
language.
!. 'rograms written in are eAcient
and fast.
B. has only !3 keywords.
C. is highly portable programming
language. The programs written
for one computer can be run on
another with or without any
modi%cations
9. has rich set of operators.
#. permits all data conversions
and mi)ed mode operations
D. $ynamic memory
allocation5$8A, is possible in .
2. E)tensive varieties of data
types such as arrays, pointers,
structures and unions are available
in .
1:. improves by itself. It has
several prede%ne
functions.
11. easily manipulates bits, bytes
and addresses.
13. /ecursive function calls for
algorithmic approach is possible in
.
1!. 8ainly we are using language
to implement
system softwares. These are
compilers ,editors,
drivers ,databases and operating
systems.
1B. compiler combines the
capability of an assembly level
language with the features of high
level language. So it is called as
middle level language.
/mportant points:
74. . was basically designed for 6ni)
operating system, 2!E of
instructions which are written in .
75. . is case sensitive programming
language. statements
are entered in lower case letters
only.
76. . is function oriented
programming language. Any
program contains one or more
functions . 8inimum &ne function
is compulsory by the name called
main. *ithout main we canFt
e)ecute a program.
77. . Every statement must be
terminated by semicolon 5 G ,,
e)cept preHprocessor statements
and function de%nition.
78. . A function is represented by
function name with a pair of
parenthesis 5 ,.
Bloc+ diagram of e(ecution of C program:

"nce the program is completed, the program is
feed into the computer using the compiler to produce
equivalent machine language code. In - program
compilation there are 2 mechanisms.
79. -ompiler
80. +inker.
Te Compiler receives the source file as input
and converts that file into obHect file. Then the
Linker receives the obHect file as its input and linking
with - libraries. After linking it produces an
executable file for the given code. After creation of
executable file, then start the program execution and
loads the information of the program into primary
memory through +"A9>#. After loading the
information the processor processing the information
and gives output.
Basic structure of C program

I 9ocument section J
,reprocessor section
5or7
+ink section
I =lobal declaration section J
main57
K
I +ocal declaration section J
tatements
L
Iub program sectionJ
5include user defined functions7
0ocument section:
It consists a set of comment lines giving the name
of the program, author name and some other details
about the entire program.
1reprocessor Section 2or# Lin+ section:
It provides instructions to the compiler to link the
functions from the system library.
3lo%al declaration Section:
The Mariables that are used in more than one
function are called as global variables and these are
declared in global declaration section.
4ain function section:
>very - program must have one function. i.e.
main function. This section contains 2 parts.
81. +ocal declaration section.
82. tatements .
The +ocal declaration section declares all the
variables used in statements. The statements part
consists a sequence of executable statements.These 2
parts must appear between opening and closing curly
braces. The program execution begins at the opening
brace and ends at closing brace.
Su% programming section:
It contains all the user defined functions.
This section may be placed before or after main
function.
Comments:
*nexecutable lines in a program are called as
comments. These lines are skipped by the compiler.
CB000000000000000000000
000000000000000000000
00000000000000000000BC $ulti line comment.
CC0000000000000000000 single line comment5-..
comment7.
1reprocessor statements:
The preprocessor is a program that process the
source code before it passes through the compiler.
5include:
It is preprocessor file inclusion directive and is
used to include header files. It provides instructions
to the compiler to link the functions from the system
library.
Synta(:
Ninclude F fileOname F
5or7
Ninclude E fileOname G
%hen the file name is included within the
double quotation marks, the search for the file is
made first in the current directory and then the
standard directories5T-7.
%hen the file name is included within angular
braces, the file is search only in standard
diretories5T-7.
stdio.h !0 standard input and output header file.
conio.h !0 console input and output header file.
console units! keyboard and monitor.
These 2 headers are commonly included in all -
programs.
If you ,ant to ,or+ ,ith Tur%o c6c77 $first
install Tur%o c6c77
soft,are and then follo, the follo,ing steps'

Steps in&ol&ed in C programming:
:7 1ow to open a - editor!
83. tart $enu I #un I type -!PT-P?inPT-.exe
then press >nter key
84. At the time of installation create hortcut to - on
9esktop, then it will create an icon5T-..;.47 on
desktop. 9ouble click on that icon.
27 After entering into - editor, check the path as!
goto "ptions menu I 9irectories I
85. - !PT-PInclude
86. - !PT-P+ib
87. 00000000000000000
88. 00000000000000000
;7 Type - program as, goto Qile menu I @ew
<7 ave program as, goto Qile menu I ave
27 -ompile ,rogram as, goto -ompile menu I
-ompile

37 #un the ,rogram as, goto #un menu I #un
)7 ee the output as, goto %indow menu I *ser
screen
'7 >xit from - editor as, goto Qile menu I Ruit
Shortcut 8eys:
"pen ! Q;
ave ! Q2
-lose file ! Alt . Q;
Qull creen ! Q2
-ompile ! Alt . Q&
#un ! -trl . Q&
"utput ! Alt . Q2
-hange to another file! Q3
1elp ! -trl . Q:
Tracing ! Q)
Ruit ! Alt . (
printf :
It is a function and is used to print data on the
standard output device5monitor7.
Synta(:
format string

int printf5Scontrol stringT I,arg:,arg2,
UUUUUU.argnJ 7V
Eg: printf5S%elcome to - ,rogrammingT7V
1rogram :
CB Qirst ,rogram BC
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
printf5S%elcome to - ,rograsmmingT7V
L
clrscr :
It clears text mode window.
syntax!
void clrscr57V
getc:
It is a function and it gets a character from
standard input device, but it doesnot echo5print7 to
the screen.
Synta( : int getch57V
1rogram :
CB econd ,rogram BC
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
clrscr57V
printf5S%elcome to ?9,T7V
getch57V
L
Escape se9uence characters :
- uses some backslash characters in output
functions for formating the output.These character
are called as >scape sequence characters.In these
characters it consists of two characters ,?ut it is
treated as a single character.
Pn 0 new line
Pt 0 hori8ontal tab5default ' spaces7
Pv 0 vertical tab5default : line7
Pb 0 back space
Pa 0 alert5beep sound7 5A-II value of Pa is )7
Pr 0 -arriage return
Pf 0 form feed
P4 0 null
PF 0 double quotes
1rogram :
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
printf5S%elcomePnT7V
printf5S=ood $orningT7V
L
Output:
%elcome
=ood $orning
1rogram : esc:%'c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
printf5S%elcomePbT7V
printf5S=ood $orningT7V
L
Output:
%elcom=ood $orning
1rogram : esc:r'c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
printf5S=ood $orningPrT7V
printf5S%elcomeT7V
L
Output:
%elcomerning
C To+ens
The smallest individual elements or units in a
program are called as Tokens. - has following
tokens.
89. Identifiers
90. Weywords
91. -onstants
92. "perators
93. pecial characters
Identifiers :
Identifiers refer to the name of the variables,
functions, arrays,etc. created by the user or
programmer, using the combination of following
characters.
94. Alphabets ! A to 6 5or7 a to 8
95. 9igits ! 4 to &
96. *nderscore ! O
Note :
97. . The first character of an identifier must be an
alphabet or underscore ,we canot use digit.
98. . 9efault identifier length is ;2 characters.
8ey,ords
Weywords are the words whose meaning has been
already explained by the compiler. That means at the
time of designing a language, some words are
reserved to do specific tasks. uch words are called
as keywords 5or7 reserved words. All - compilers
support ;2 keywords.
They are!
99. auto
100. break
101. case
102. char
103. const
104. continue
105. default
106. do
107. double
108. else
109. enum
110. extern
111. float
112. far
113. for
114. goto
115. if
116. int
117. long
118. register
119. return
120. short
121. signed
122. si8eof
123. static
124. struct
125. switch
126. typedef
127. union
128. unsigned
129. void
130. while
constants:
constants define fixed values, that donot change
during the execution of program. - supports the
following constants.
131. Integer constants
132. -haracter constants
133. #eal or floating constants
134. tring constants
;perators:
"perator is a symbol which performs particular
operation. - supports a rich set of operators. -
operators can be classified into @o of categories.
They include arithmetic operators, logical operators,
bitwise operators, etc.
Special characters:
All characters other than alphabets and digits are
treated as special characters.
Eg: B , D , X , K ,etc.
0ata types in C
The kind of data that variables may hold in a
programming language are called as data types. -
data types can be classifies into ; categories.
135. ,rimary data types
136. 9erived data types
137. *ser defined data types
1rimary data types:
All - compiler supports < fundamental data
types. @amely int, char, float and double.
int:
It is positive, negative and whole values but not a
decimal number.
Eg: :4, 24, 0;4, 4 etc.
car:
A single character can be treated as character data
type and is defined between single quotation marks.
Eg: YrZ, Y1Z, Y2Z, YBZ, etc.
!ote: tring is also a character data type. A group of
characters defined between double quotation marks is
a tring.
Eg: S1111T, Sabc&'2T, etc.
float: The numbers which are stored in the form of
floating point representation is called as float data
type.
Eg: :4.22, <)'.:2;<, 023.;)'2:, etc.
dou"le: The numbers which are stored in the form of
double
precision floating point representation is called as
double data type.
Eg: :42;.23)'&, :2;<2.23)'&, 4.<23;2:, 023).<223
etc.
0eri&ed data types:
These data types are created from the basic integers,
characters and floating data types.
Eg:
arrays, pointers, structures etc.
<ser defined data types:
The user defined data types enable a programmer
to invent his own data types and define what values it
can take on. Thus these data types can help a
programmer in reducing programming errors.
- supports 2 types of user defined data types.
138. typedef 5type definition7
139. enum 5enumerated data type7
Type modifiers : 2signed$ unsigned$ short$ long#
A type modifiers alter the meaning of the base data
type to yield a new type.
: >ach of these type modifiers can be applied to
the base type int.
2 Type modifiers signed and unsigned can also be
applied to the
base type car#
; In addition, long can be applied to dou"le#
Su% classification of 1rimary data types$ format
specifiers $ memory si=e and their accessi%ility
range :
0ata type >ormat Spec 4em si=e
Access%ility range
---------------------------------------------------------------------------------
-------------------
unsigned char Dc : ?yte 4 to
222

char Dc : ?yte 0:2'
to :2)
int Dd 2 ?ytes 0;2)3' to
;2)3)
unsigned int Du 2 ?ytes 4 to
322;2
long 5or7 Dld < ?ytes
02:<)<';3<' to 2:<)<';3<)
long int
unsigned long 5or7 Dlu < ?ytes 4 to
<2&<&3)2&2
unsigned long int
float Df < ?ytes ;.<B5:4
power 0;'7

to
;.<B5:4 power ;'7
double Dlf ' ?ytes :.)B5:4
power 0;4'7
to
:.)B5:4 power ;4'7
long double D+f :4 ?ytes ;.<B5:4
power 0<&;27
to
:.:B5:4
power <&;27
char I J 5string7 Ds 000000000
000000000000000
Do "ctal ?ase
Dx 1exa decimal base
Dp $emory address
?aria%le:
A quantity which may vary during the execution of
a program is called as variable.
0eclaration of a ?aria%le:
datatype identifier V
5or7
datatype identifier0:,identifier 02,UU,identifier0n V
Eg:
int nV
char chV
float ftV
double dbV
int a,b,c,dV
char ch:,ch2,ch;V
Initiali=ation of &aria%le :
At the time of declaring a variable, we can store
some value into that variable is known as
initiali8ation.
Synta(:

data type identifier A valueV
Eg:
int nA:44V
char chAZ1ZV
float ftA:4.23&V
double dbA:<:).:<:'V
int aA:4,bA24,cA;4V
int xA:44 , y , 8A244V
1rogram :
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int nA:44V
clrscr57V
printf5SDdT,n7V
printf5SPnvalue of n A DdT,n7V
getch57V
L
;utput
:44
value of nA:44
!ote:
In - language, all declarations will be done before
the first executable statement.
1rogram:
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int nA:'V
char cAZ#ZV
float fA:).:<'V
double dA:):<.:<:'V
clrscr57V
printf5SnADdT,n7V
printf5SPncADcT,c7V
printf5SPnfADfT,f7V
printf5SPndADlfT,d7V
getch57V
L
;utput
@A:'
-A#
QA:).:<'444
9A:):<.:<:'44
Note:
Qloating values displays 3 digits after the decimal
point, by default.
If we want to display specified number of digits after
decimal point for floating values, use the following
technique.
E(ample:
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
float fA:2.<<33V
double dA:2;<.23)'&V
clrscr57V
printf5SQADfT,f7V
printf5S9ADlf,d7V
printf5SQAD.2fT,f7V
printf5S9AD.2lfT,d7V
getch57V
L
;utput
QA:2.<<33
9A:2;<.23)'&
QA:2.<2
9A:2;<.2)
Constants:
-onstants in - refer to fixed valued that donot
change during the execution of a program.
const:
It is a keyword and is used to declare constants.
Synta(!
const datatype identifierAvalueV
"r
const datatype identifier0:Avalue,UUU.,identifier0
nAvalueV
Eg: const int aA:44V
const int aA:4,bA24,cA;4V
E(ample:
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
const int aA:44V
clrscr57V
printf5SaADdT,a7V
CB aA244V
5if we give like this, we will get an error can$t
modify a constant %alue7BC
printf5SPnaADdT,a7V
getch57V
L
;utput
aA:44
aA:44
Sym%olic constants:
5define :
It is a preprocessor statement and is used to define
symbolic constants.
Synta( :
Ndefine identifier value
Eg : Ndefine pi ;.:<
Ndefine g &.'
E(ample:
NincludeEstdio.hG
NincludeEconio.hG
Ndefine pi ;.:<
Ndefine g &.'
Ndefine val :44
void main57
K
clrscr57V
printf5Spi A D.2fT,pi7V
printf5Sg A D.2:T,g7V
printf5Sval A DdT,val7V
getch57V
L
;utput :
,IA;.:<
=A&.'
MA+A:44
scanf :
It is a function and is used to read data from the
standard input device5Wey?oard7.
Synta(:
int scanf5Sformat5s7T,address0:,address02,
UU..address0n7V
eg :
int nV
address of nA[n
scanf5SDdT,[n7V
E(ample :
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int nV
clrscr57V
printf5S>nter any value!T7V
scanf5SDdT,[n7V
printf5S=iven value ! DdT,n7V
getch57V
L
E(ample :
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int nV
char cV
float fV
double dV
clrscr57V
printf5S>nter any character value!T7V
scanf5SDcT,[c7V
printf5S>nter any Int value !T7V
scanf5SDdT,[n7V
printf5S>nter any float value!T7V
scanf5SDfT,[f7V
printf5S>nter any double value!T7V
scanf5SDlfT,[d7V
printf5S=iven Int !D dPnT,n7V
printf5S=iven char !DcPnT,c7V
printf5S=iven float !DfPnT,f7V
printf5S=iven double !DlfT,d7V
getch57V
L
S+ipping pro%lem :
E(ample :
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int nV
char cV
clrscr57V
printf5Senter any number!T7V
scanf5SDdT,[n7V
printf5Senter any character!T7V
scanf5SDcT,[c7V
printf5SnADdPnT,n7V
printf5ScADcPnT,c7V
getch57V
L
;utput
enter any number!:44
enter any character!
nA:44
cA
In the above program, it cannot read a
character into the character variable, because
previous integer reading statement creates a new line
character in the input stream. That character will
assign to the character variable.
To avoid this problem we use fflus or flusall
functions#
fflush : It flushes the specified stream.
Synta(: fflush5streamOname7V
Eg: fflush5stdin7V
flushall : It flushes all open streams.

Synta(: flushall57V
E(ample :
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int nV
char cV
clrscr57V
printf5Senter any number!T7V
scanf5SDdT,[n7V
printf5Senter any character!T7V
fflush5stdin7V 5or7 flushall57V
scanf5SDcT,[c7V
printf5S@ADdPnT,n7V
printf5S-ADcPnT,c7V
getch57V
L
@n character :
ometimes, instead of fflush57 or flushall57 we can
use Pn to avoid skipping problem.
E(ample :
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int nV
char cV
clrscr57V
printf5Senter any number!T7V
scanf5SDdT,[n7V
printf5Senter any character!T7V
scanf5SPnDcT,[c7V
printf5S@ADdPnT,n7V
printf5S-ADcPnT,c7V
getch57V
L
E(ample :
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int nV
char cV
clrscr57V
printf5Senter any number and a character!T7V
scanf5SDdPnDcT,[n,[c7V
printf5S@ADdPnT,n7V
printf5S-ADcPnT,c7V
getch57V
L

String :
A group of characters defined between double
quotation marks is a string. It is a constant string. In
- language, a string variable is nothing but an array
of characters and terminated by a null character 5P47.
0eclaration :

char identifierIsi8eJV
Eg ! char stI24JV

Initiali=ation :
At the time of declaring a string variable, we can
store a constant string into that variable is called as
initiali8ation.
optional

syn: char identifierIsi8eJATstringTV


Eg : char st:I:4JAT%>+-"$>TV
char st2I JATA?-9TV

The compiler assigns a constant string to the string
variable. It automatically supplied a null character at
the end of string. Therefore the si8e should be equal
to the maximum number of characters in the string
plus5.7 :.
Note: In - language, string initiali8ation is possible,
but string assigning is not possible.
E(ample :
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
char stI:4JAT=ood $orningTV
char sIJAT%elcomeTV
printf5SDsT,st7V
printf5SDsT,s7V
getch57V
L
1rogram : To accept a string from key board and to
display the string.
E(ample :
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
char stI24JV
clrscr57TV
printf5S>nter a string !T7V
scanf5SDsT,st7
printf5S=iven string is !DsT,st7V
getch57V
L
;utput :
140. >nter a string! welcome
=iven string!welcome

141. >nter a string! welcome to ?9, +Td
=iven string!welcome
scanf57 statement cannot read spaces into a string
variable
E(ample :
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
char stI24JV
clrscr57TV
printf5S>nter a string !T7V
scanf5SDI\PnJsT,st7V
printf5S=iven string is !DsT,st7V
getch57V
L
gets :
It is a function, and is used to read a string5including
spaces7 from the standard input device.
Synta( : gets5stringOvaribale7V
Eg: gets5st7V
E(ample :
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
char stI24JV
clrscr57TV
printf5S>nter a string !T7V
gets5st7V
printf5S=iven string is !DsT,st7V
getch57V
L
1rogram :
To enter any caracter and display its ASCII %alue
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
char cV
clrscr57V
printf5Senter any character !T7V
scanf5SDcT,[c7V
printf5SA-II value of given character !DdT,c7V
getch57V
L
1rogram :
To enter any ASCII %alue and display its caracter
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int nV
clrscr57V
printf5Senter any A-II value from 4 to 222 !T7V
scanf5SDdT,[n7V
printf5Scharacter of give A-II value! DcT,n7V
getch57V
L
1rgram :
To enter any date in date format and to display te
gi%en date#
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int d,m,yV
clrscr57V
printf5F>nter any date in the format dd0mm0
yyyy !F7V
scanf5FDd0Dd0DdF,[d,[m,[y7V
printf5F=iven date is ! D.2d0D.2d0DdF,d,m,y7V
getch57V
L
;perators in C
;perator: It is a symbol and it performs particular
operation.
;perand : It is an entity, on which an operator acts.
Binary operator: It requires 2 operands.
<nary operator : It requires only a single operand.
- operators can be classified into number of
categories. They are
142. Arithmetic operators
143. #elational operators
144. +ogical operators
145. Assignment operators
146. Increment and 9ecrement operators
147. ?it0wise operators
148. pecial operators
a7 Ternary 5or7 -onditional operator
b7 -omma operator
c7 si8eof operator
Arithmetic operators:
These are the basic operators in - language. These
are used for arithmetic calculations.
. Addition
0 ubtraction
B $ultiplication
C 9ivision
D $odulus5#emainder7
1rogram : aritm#c
To enter any & num"ers and test all aritmetic
operations#
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int a,bV
clrscr57V
printf5F>nter two numbers !F7V
scanf5FDdDdF,[a,[b7V
printf5FPnAddition of Dd and Dd is DdF,a,b,a.b7V
printf5FPnubtraction of Dd and Dd is DdF,a,b,a0b7V
printf5FPn$ultiplication of Dd and Dd is
DdF,a,b,aBb7V
printf5FPn9ivision of Dd and Dd is DdF,a,b,aCb7V
printf5FPn$odulus of Dd and Dd is DdF,a,b,aDb7V
getch57V
L
Aelational operators :
These are used to test the relation between 2 values
or 2 expressions. All - relational operators are binary
operators
and hence it requires 2 operands.
E less than
G greater than
EA less than or equal to
GA greater than or equal to
A A equal to
]A not equal to
Logical operators :
These are used to combine the result of 2 or more
expressions.
[[ +ogical A@9
JJ +ogical "#
K +ogical @"T
>xp: >xp2 >xp: [[ >xp2 >xp: ^^ >xp2
00000000000000000000000000000000000000000000000000000000000000000000000
True True True True
True Qalse Qalse True
Qalse True Qalse True
Qalse Qalse Qalse Qalse
149. If >xpATrue I ]>xpAQalse
150. If >xpAQalse I ]>xpATrue
Assignment ;perators :
These are used to assign a constant value or values on
an expression to an identifier.
They are of 2 types.
151. Simple assignment : A
Eg : nA:4


152. Shorthand or compound assignment

.A
0 A
BA
CA
DA
etc.
Eg :
int nA:4, if we want to add 2 to n, then we
will give nAn.2 or n.A2, which is compound
assignment.
Increment and 0ecrement ;perators :
These operators are used to control the loops in an
effective method.
153. Increment operator :
The symbol .. is used for incrementing by :.
It is of 2 types.
:7 .. identifier V prefix increment
27 identifier.. V postfix increment

Eg :
154. int a A :4V
..aV 5or7 a..V
a A ::
155. int a A :4,bV
b A ..aV
a A ::
b A ::
156. int a A :4,bV
b A a ..V
a A ::
b A :4

157. int x A :4
printf5SDd Pt DdT,x,x..7V
oCp! :: :4
0ecrement operator :
The symbol 00 is used for decrementing by :.
It is of 2 types.
:7 00identifierV prefix decrement
27 identifier00 V postfix decrement
Eg :
:7 int a A :4
00aV 5or7 a00V
a A &

27 int a A :4,bV
b A 00aV
a A &
b A &
;7 int a A :4,bV
b A a00V
a A &
b A :4
<7
int x A :4
printf5SDd PtD dT,x,x007V
output : & :4
Bit-,ise ;perators :
These operators are used for manipulation of data at
bit level.
These are classified into 2 types. @amely,
158. ?it0wise logical operators
159. ?it0wise shift operators.
Bit-,ise logical operators :
These are used for bit0wise logical decision making.
[ ?it0wise logical A@9
^ ?it0wise logical "#
\ ?it0wise logical ("#
?: ?2 ?: [ ?2 ?: ^ ?2 ?: \
?2
000000000000000000000000000000000000000000000000000000000000000000000000000000000
000000
: : : : 4
: 4 4 : :
4 : 4 : :
4 4 4 4 4
Eg : int aA2,bA3V
a A 2 0G : 4 :
b A 3 0G : : 4
: 4 :
: : 4
0000000000000000000000
a[b ! : 4 4 A <
0000000000000000000000
: 4 :
: : 4
0000000000000000000000
a^b ! : : : A )
0000000000000000000000
: 4 :
: : 4
0000000000000000000000
a\b ! 4 : : A ;
0000000000000000000000

E(ample :
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int a,bV
printf5S>nter values int a and b !T7V
scanf5SDdDdT,[a,[b7V
printf5SPn a [ b A DdT,a[b7V
printf5SPn a ^ b A DdT,a^b7V
printf5SPn a \ b A DdT,a\b7V
getch57V
L

Bit-,ise shift operartor :
The shift operations take binary patterns and shift
the bits to the left or right.
EE left shift
GG right shift
Eg :
int aA<,b,cV
aA< I aA: 4 4 5binary form7
bAaEE2V means a is to be left shifted by 2 bits and
store it in b.
: 4 4
1ence it became,
: 4 4
4 4
then the value of b !0

: 4 4 4 4 A 2 power < A :3
aA< I aA: 4 4 5binary form7
cAaGG:V means a is to be right shifted by : bit and
store it in c.
: 4 4
1ence it became,
:
4
then the value of c !0

4 : 4 A 2 power : A 2
E(ample :
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int aA<,b,cV
clrscr57V
bAaEE2V
cAaGG:V
printf5SPn aADdT,a7V
printf5SPn bADdT,b7V
printf5SPn cADdT,c7V

getch57V
L
Special operators :
"# Ternary 2or# Conditional operator :
A ternary operator pair F _ F and F ! F is available
in - language. It is used to construct a conditional
expression.
The general form of ternary operator is !
>xp: _ >xp2 ! >xp; V
In this operator first >xp: is evaluated, if it is true
then >xp2 is evaluated and its value becomes the
value of the >xpression, otherwise >xp; is evaluated
and its value becomes the value of the >xpression.
Eg :
int aA:4,bA24,cV
c A aEb _ a ! bV
output will be c A:4
1rogram : ma'()ter#c
To find te ma'imum and minimum %alues of gi%en
& num"ers#
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int a,b,max,minV
clrscr57V
printf5F>nter 2 numbers ! F7V
scanf5FDdDdF,[a,[b7V
maxAaGb _ a ! bV
minAaEb _ a ! bV
printf5F$aximim value ! DdF,max7V
pri`ntf5FPn$inimum value ! DdF,min7V
getch57V
L
1rogram : e%od)ter#c
To ceck weter te gi%en num"er is e%en or odd#
NincludeEstdio.hG
NincludeEconio.hG
void main 57
K
int nV
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
nD2AA4 _ printf5F=iven number is evenF7 !
printf5F=iven number is oddF7V
getch57V
L
1rogram : ma'&)ter#c
To find te ma'imum and minimum %alues of gi%en
* num"ers#
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int a,b,c,max,minV
clrscr57V
printf5F>nter ; numbers ! F7V
scanf5FDdDdDdF,[a,[b,[c7V
CB
maxAaGb _ a ! bV
maxAmax G c _ max!cV
minAaEb _ a ! bV
minAmin E c _ min!cV
BC
maxAaGb [[ aGc _ a ! 5bGc _ b ! c7V
minAaEb [[ aEc _ a ! 5bEc _ b ! c7V
printf5F$aximim value ! DdF,max7V
printf5FPn$inimum value ! DdF,min7V
getch57V
L
2# comma operator :

It can be used to link the related expressions together.
The general form of comma operator is !
varA5var0:Avalue,var02Avalue,..,var0nAvalue ,
expression7V
Eg :
int a,b,cV
cA5aA:4,bA24,a.b7V
1ere first assigns the value of :4 to a, then assigns 24
to b and finally assigns ;45a.b7 to c.
E(ample : comma#c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int a,b,cV
clrscr57V
cA5aA:4,bA24,2Ba.b7V
printf5FaADdF,a7V
printf5FPnbADdF,b7V
printf5FPncADdF,c7V
getch57V
L
)# si=eof operator :
It is used to get the memory si8e of specified
variable or expression or data type.
Synta( :
si8eof 5variable C expression C datatype7V
Eg ":
:. si8eof5int7V 2
2. si8eof5float7V <
eg2!
int a,bV
si8eof5a7V 2
si8eof5b7V 2
si8eof5a.b7V 2
E(ample : " si+eof)(#c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int nV
char cV
float fV
double dV
clrscr57V
printf5Fsi8e of int ! Dd bytesF,si8eof5n77V
printf5FPnsi8e of char ! Dd bytesF,si8eof5c77V
printf5FPnsi8e of float ! Dd bytesF,si8eof5f77V
printf5FPnsi8e of double ! Dd bytesF,si8eof5d77V
getch57V
L
E(ample : 2 si+eof)&#c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int a,bV
clrscr57V
printf5Fi8e of a ! Dd bytesF,si8eof5a77V
printf5FPni8e of b ! Dd bytesF,si8eof5b77V
printf5FPni8e of a.b ! Dd bytesF,si8eof5a.b77V
getch57V
L
1recedence of ;perators :
.@o -ategory "perator %hat it
is 5or does7
0000000000000000000000000000000000000000000000000000000000000000
000000000000000000
: 1ighest 5 7 Qunction
call

IJ Array
subscript
2 *nary ] +ogical
@ot
. *nary plus
0 *nary
minus
.. ,re or post
increment
00 ,re or post
decrement
[ address

si8eof returns si8e
of operand in

bytes
; $ultiplicative B $ultiply
C 9ivide
D
#emainder5modulus7
< Additive . ?inary
plus5addition7
0 ?inary
minus5subtraction7
2 shift EE shift left

GG shift right
3 #elational E +ess than
EA +ess than or
equal to
G =reater than
GA =reater than
or equal to

) >quality A A >qual to
]A @ot equal to
' [ ?it wise
A@9
& \ ?it wise
("#
:4 ^ ?it wise "#
:: [[ +ogical
A@9
:2 ^^ +ogical "#
:; -onditional _! 5a_x!y
means Sif a is true,
then
x else yT7
:< Assignment A imple
assignment

B A Assign
product
C A Assign
quotient

DA Assign
remainder
.A Assign sum
0 A Assign
difference
:2 -omma , >valuate
Type casting or Type con&ersion :
The process of converting one data type to another is
called as type casting or type conversion.
In - language type conversion is an arithmetic
expression will be done automatically. This is called
implicit conversion. If we want to store a value of
one type into a variable of another type, we must
caste the value to be stored by preceding it with the
type name in parenthesis.This is called explicit
conversion or type casting.
Eg :

int a,bV
float c,dV
aA2,bA2
cAaCbV 00000I cA2.44 Automatic or implicit
conversion
dA5float7aCb 00000I dA2.24 Type casting or
explicit conversion
E(ample : typecast#c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int a,bV
float c,dV
clrscr57V
printf5F>nter 2 numbers ! F7V
scanf5FDdDdF,[a,[b7V
cAaCbV
dA5float7aCbV
printf5FcAD.2fF,c7V
printf5FPndAD.2fF,d7V
getch57V
L
1rogram : tri)area#c
To enter "ase and eigt of a triangle and to
calculate its area#
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
float b,h,areaV
clrscr57V
printf5F>nter base of triangle ! F7V
scanf5FDfF,[b7V
printf5F>nter height of triangle ! F7V
scanf5FDfF,[h7V
areaA5float7:C2BbBhV CB or areaA4.2BbBhV BC
printf5FArea of triangle ! D.2f square
unitsF,area7V
getch57V
L
1rogram : circl)ar#c
To enter radius of a circle and to calculate its area#
NincludeEstdio.hG
NincludeEconio.hG
Ndefine pi ;.:<
void main57
K
float r,areaV
clrscr57V
printf5F>nter radius of circle ! F7V
scanf5FDfF,[r7V
areaApiBrBrV
printf5FArea of circle ! D.2f square
unitsF,area7V
getch57V
L
1rogram : simp)int#c
To enter principle amount, time period, and rate of
interest and to calculate and display simple interest
and total amount#
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int y,m,tV
float p,r,si,tamtV
clrscr57V
printf5F>nter principal amount ! F7V
scanf5FDfF,[p7V
printf5F>nter rate of interest ! F7V
scanf5FDfF,[r7V
printf5F>nter number of years and months ! F7V
scanf5FDdDdF,[y,[m7V
tAm.5yB:27V
siA5pBtBr7C:44V
tamtAp.siV
printf5Fimple Interest !D.2fF,si7V
printf5FPnToatal Amount !D.2fF,tamt7V
getch57V
L
1rogram : swap(#c
swapping of gi%en %aria"les using temporary
%aria"le
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int a,b,tV
clrscr57V
printf5F>nter 2 numbers ! F7V
scanf5FDdDdF,[a,[b7V
printf5FPn?efore swapping F7V
printf5FPnaADdF,a7V
printf5FPnbADdPnPnF,b7V
tAaV
aAbV
bAtV
printf5FAfter swapping F7V
printf5FPnaADdF,a7V
printf5FPnbADdF,b7V
getch57V
L
1rogram : swap&#c
swapping of gi%en %aria"le wit out using temporay
%aria"le
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int a,bV
clrscr57V
printf5F>nter 2 numbers ! F7V
scanf5FDdDdF,[a,[b7V
printf5FPn?efore swapping F7V
printf5FPnaADdF,a7V
printf5FPnbADdPnPnF,b7V
aAa.bV
bAa0bV
aAa0bV
printf5FAfter swapping F7V
printf5FPnaADdF,a7V
printf5FPnbADdF,b7V
getch57V
L
Compound statement 2or# %loc+ :
A group of statements enclosed within curly braces is
called as compound statement or block.
Accessi%ility of &aria%les inside and outside of
compound statement :
"' A variable which is declared above 5outside7 the
compound statement, it is accessible both inside and
outside the compound statement.
2. A variable which is declared inside the compound
statement it is not accessible outside the compound
statement.
;. It is possible to declare a variable with the same
name both inside and outside the compound
statement.
E(ample : compd(#c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int aA:44V
clrscr57V
K
int bA244V
printf5FInside -ompound tatementPnF7V
printf5FaADdPnF,a7V
printf5FbADdPnF,b7V
L
printf5FPn"utside -ompound tatementPnF7V
printf5FaADdPnF,a7V
CC printf5FDdF,b7V 00G not accessible outside
compound statement CC
getch57V
L
E(ample : compd&#c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int aA:44V
clrscr57V
printf5F"utside -ompound tatementPnF7V
printf5FaADdPnPnF,a7V
K
int aA244V
printf5FInside -ompound tatementPnF7V
printf5FaADdPnPnF,a7V
L
printf5F"utside -ompound tatementPnF7V
printf5FaADdPnF,a7V
getch57V
L
Control statements 2or# Control structures :
- is a structured programming language. "ne of the
reasons for this is having various program control
statements. - process the decision making
capabilities and supports the statements known as
control statements.
- supports ; types of control statements. They are !
:. -onditional control statements.
2. *nconditional control statements.
;. +oop control statements.
Conditional control statements :
- supports 2 types of conditional control statements.
:7 simple if statement
27 if0else statement
;7 @ested if statement
<7 else0if ladder statement
27 switch statement
simple if statement :
It is a conditional controlled statement and is used
to control the flow of execution.
Synta( :
if5 expr 7
K
statementsV
L

In this statement, first the expression will be
evaluated.If it is true then the statement block will be
executed and the control transfer to the next
statement. "therwise if the expression is false, then
the control directly goes to the next statement.
flo,chart
Note :
In any control statement, the statement block
contains only a single statement, curly braces are not
necessary.
E(ample : sim)if(#c
To find ma'imum of & num"er using simple if
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int a,b,maxV
clrscr57V
printf5F>nter any two numbers ! F7V
scanf5FDdDdF,[a,[b7V
maxAaV
if5maxEb7
K
maxAbV
L
printf5F$aximum Malue ! DdF,max7V
getch57V
L
E(ample : sim)if&#c
To find ma'imum of * num"ers using simple if
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int a,b,c,maxV
clrscr57V
printf5F>nter ; numbers ! F7V
scanf5FDdDdDdF,[a,[b,[c7V
maxAaV
if5maxEb7
maxAbV

if5maxEc7
maxAcV
printf5F$aximum Malue ! DdF,max7V
getch57V
L
E(ample : age#c
To enter current date and date of "irt# Calculate
and display present age #
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int cd,cm,cy,bd,bm,by,d,m,yV
clrscr57V
printf5F>nter current date 5dd0mm0yyyy7 ! F7V
scanf5FDd0Dd0DdF,[cd,[cm,[cy7V
printf5F>nter birth date 5dd0mm0yyyy7 ! F7V
scanf5FDd0Dd0DdF,[bd,[bm,[by7V
dAcd0bdV
mAcm0bmV
yAcy0byV
if5dE47
K
dAd.;4V
m00V
L
if5mE47
K
mAm.:2V
y00V
L
printf5F,resent age is ! Dd years Dd months Dd
daysF,y,m,d7V
getch57V
L
if-else statement :
It is an extension of simple if statement.
Synta( :
if5expr7
K
tatements0:V
L
else
K
tatements02V
L


In this statement, first the expression will be
evaluated, and if it is true then the if block statements
are executed and the else block statements are
ignored. "therwise if the expression is false, then
else block statements are executed and if block
statements are ignored.
flow chart
E(ample : else)if#c
To find ma'imum of & num"ers using else if
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int a,b,maxV
clrscr57V
printf5F>nter 2 numbers ! F7V
scanf5FDdDdF,[a,[b7V
if5aGb7
maxAaV
else
maxAbV
printf5F$aximum value ! DdF,max7V
getch57V
L
E(ample : else)if&#c
To ceck weter te gi%en num"er is e%en or odd
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int nV
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
if5nD2AA47
printf5F=iven number is >venF7V
else
printf5F=iven number is "ddF7V
getch57V
L
Nested if statements :
*sing a if statement within another if is called as
nested if. If a series of decisions are involved, we use
nested if statement.
>orm : "
if5expression0:7
K
if5expression027
K
BBBB'
if5expression0n7
K
statementsV
L
'BBBBB''
L
L

>orm : 2
if5expression0:7
K
if5expression027
K
tatement0:V
L
else
K
tatement02V
L
L
else
K
if5expression0;7
K
tatement0;V
L
else
K
tatement0<V
L
L
E(ample : nest)if#c
To find ma'imum of * num"ers using nested if
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int a,b,c,maxV
printf5F>nter any three numbers ! F7V
scanf5FDdDdDdF,[a,[b,[c7V
if5aGb7
K
if5aGc7
maxAaV
else
maxAcV
L
else
K
if5bGc7
maxAbV
else
maxAcV
L
printf5F$aximum value ! DdF,max7V
getch57V
L
else-if ladder :
This statement is also used for a series of decisions
are involved.
Synta( :
if5expr0:7
K
tatements0:V
L
else if5expr027
K
tatement02V
L
'BBBBB'
BBBBB''
else if5expr0n7
K
tatement0nV
L
else
K
else block statementsV
L
In this statement, the expressions are evaluated
from top to bottom, if the condition is true then the
statements associated that block is executed and the
control transfers to the next statement. "therwise
when all expressions are false then the final else
block statements will be executed.
E(ample : elif)lad#c
To find ma'imum of * num"ers using else if ladder
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int a,b,c,maxV
printf5F>nter any three numbers ! F7V
scanf5FDdDdDdF,[a,[b,[c7V
if5aGb [[ aGc7
maxAaV
else if5bGc7
maxAbV
else
maxAcV
printf5F$aximum value ! DdF,max7V
getch57V
L
E(ample : ck)car#c
To ceck weter te gi%en num"er is alpa"et or
num"er or special caracter
-include.stdio#/
-include.conio#/
%oid main01
2
car c3
clrscr013
printf04Enter any caracter :413
scanf045c4,6c13
78
if0 0c/9:; 66 c.9<=1 >> 0c/9<? 66
c.9(&&1 1
printf04@i%en caracter is Alpa"et413
else if0c/9AB 66 c.9;?1
printf04@i%en caracter is Cigit413
else
printf04@i%en caracter is a Special caracter413
87
if0 0c/9DAD 66 c.9DED1 >> 0c/9DaD 66 c.9D+D1
1
printf04@i%en caracter is Alpa"et413
else if0c/9D=D 66 c.9D<D1
printf04@i%en caracter is Cigit413
else
printf04@i%en caracter is a Special caracter413
getc013
F
E(ample : %ow)cons#c
To ceck weter te gi%en caracter is %owel or
consonant
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
char chV
clrscr57V
printf5F>nter any character ! F7V
scanf5FDcF,[ch7V
if55chGAaaa [[ chEAa8a7 ^^ 5chGAaAa [[ chEAa6a77
K
if5chAAaaa ^^ chAAaAa ^^ chAAaea ^^ chAAa>a ^^ chAAaia^^
chAAaIa ^^ chAAaoa ^^
chAAa"a ^^
chAAaua ^^ chAAa*a7
printf5F=iven character is a vowelF7V
else
printf5F=iven character is a consonantF7V
L
else
printf5F=iven character is not an AlphabetF7V
getch57V
L
E(ample : st)det#c
To enter student num"er, name, marks in
C,CPP,Ga%a and to calculate and display total
marks, a%erage, result and di%ision
-include.stdio#/
-include.conio#/
%oid main01
2
int sno,c,cpp,uni',tot3
car snameH&=I3
float a%g3
clrscr013
printf04Enter Student !um"er :413
scanf045d4,6sno13
printf04Enter Student !ame :413
fflus0stdin13
gets0sname13
printf04Enter Jarks in C,Cpp and Kni' :413
scanf045d5d5d4,6c,6cpp,6uni'13
tot9cLcppLuni'3
a%g90float1tot7*3
clrscr013
printf04MnStudent !um"er :5d4,sno13
printf04MnStudent !ame :5s4,sname13
printf04MnJarks in C :5d4,c13
printf04MnJarks in Cpp :5d4,cpp13
printf04MnJarks in Kni' :5d4,uni'13
printf04MnTotal Jarks :5d4,tot13
printf04MnA%erage :5#&f4,a%g13
if0c/9;= 66 cpp/9;= 66 uni'/9;=1
2
printf04MnNesult :Pass413
if0a%g/9:=1
printf04Mn@rade :A413
else
printf04Mn@rade :O413
F
else
printf04MnNesult :Pail413
getc013
F
e(it : Cprocess'hD
It terminates the program.
Synta(: void exit5int status7

tatus !
Typically value of 8ero , indicates a normal exit, and
a non 8ero indicates some error.
E(ample : c"ill#c
To enter consumer num"er, name, present mont
reading, last mont reading# calculate and display
total units and "ill amount "y te following ta"le#
<nits rate6unit
E F E "'*2min#
"-"EE 2'.E
"E"-2EE )'E
2E"-)EE *'-
)E" - EE !'EE
DEE !'2
NincludeEstdio.hG
NincludeEconio.hG
NincludeEprocess.hG
void main57
K
int cno,pmr,lmr,tuV
char cnameI24JV
float bamtV
clrscr57V
printf5F>nter consumer @umber !F7V
scanf5FDdF,[cno7V
printf5F>nter consumer @ame !F7V
fflush5stdin7V
gets5cname7V
printf5F>nter ,resent $onth #eading !F7V
scanf5FDdF,[pmr7V
printf5F>nter +ast $onth #eading !F7V
scanf5FDdF,[lmr7V
if5lmrGpmr7
K
printf5FInvalid #eadingF7V
getch57V
exit547V
L
tuApmr0lmrV
if5tuEA247
bamtA24B:.<2V
else if5tuEA:447
bamtA524B:.<27.5tu0247B2.'4V
else if5tuEA2447
bamtA524B:.<27.524B2.'47.5tu0:447B;.42V
else if5tuEA;447
bamtA524B:.<27.524B2.'47.5:44B;.427.5tu0
2447B<.)2V
else if5tuEA2447
bamtA524B:.<27.524B2.'47.5:44B;.427.5:44B<.)
27.5tu0;447B3.44V
else
bamtA524B:.<27.524B2.'47.5:44B;.427.5:44B<.)
27.5244B3.447.5tu02447B3.22V
clrscr57V
printf5FPn-onsuner @umber !DdF,cno7V
printf5FPnconsumer @ame !DsF,cname7V
printf5FPn,resent $onth #eading!DdF,pmr7V
printf5FPn+ast $onth #eading !DdF,lmr7V
printf5FPnTotal *nits !DdF,tu7V
printf5FPn?ill Amount !D.2fF,bamt7V
getch57V
L
S,itch statement :
It is a multiway condition statement used in -
language. It is mainly used in situations where there
is need to pick one alternative among many
alternatives.
S1nta5 :
switch5variable L e)pression,
M
case valueH1 N
statements O 1G
breakG
case valueH3 N
statements O 3G
breakG

00000000..
00000000.

case valueHn N
statements O nG
breakG
default N
default statementsG
P
The switch statement tests the value of
given variable or e)pression against a list
of case values. *hen a match is found,
the statement associated to that case is
e)ecuted and the control transfer to the
ne)t statement, otherwise the default
statements will be e)ecuted.

%rea+ :
It is an unconditional control statement and used to
terminate a switch statement or a loop statement.
Synta( : breakV
Note :
160. In switch statement the variable or expression is
an integral value5integer or character7, we cannot
use floating and string values.
161. In switch statement, the default case is optional.

1rogram : switc)(#c
To test all aritmetic operations using switc
statement
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int a,b,optV
clrscr57V
printf5F>nter two numbers ! F7V
scanf5FDdDdF,[a,[b7V
printf5F:0AdditionPn20ubtractionPn;0
$ultiplicationPn<09ivision
Pn20$odulusPn>nter
your option ! F7V
scanf5FDdF,[opt7V
switch5opt7
K
case :!
printf5FAddition of Dd and Dd is
DdF,a,b,a.b7V
breakV
case 2!
printf5Fubtraction of Dd and Dd is
DdF,a,b,a0b7V
breakV
case ;!
printf5F$ultiplication of Dd and Dd is
DdF,a,b,aBb7V
breakV
case <!
printf5F9ivision of Dd and Dd is D.2fF,a,b,
5float7aCb7V
breakV
case 2!
printf5F$odulus of Dd and Dd is DdF,a,b,a
Db7V
breakV
default!
printf5FInvalid "ptionF7V
L
getch57V
L
1rogram : switc)&#c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int a,bV
char optV
clrscr57V
printf5F>nter 2 numbers ! F7V
scanf5FDdDdF,[a,[b7V
printf5FA0AdditionPn0ubtractionPn$0
$ultiplicationPn909ivision
Pn#0$odulusPn>nter your option ! F7V
fflush5stdin7V
scanf5FDcF,[opt7V
switch5opt7
K
case aAa!
case aaa!

printf5FAddition of Dd and Dd is DdF,a,b,a.b7V
breakV
case aa!
case asa!
printf5 Fubtraction of Dd and Dd is
DdF,a,b,a0b7V
breakV
case a$a!
case ama!
printf5F$ultiplication of Dd and Dd is
DdF,a,b,aBb7V
breakV
case a9a!
case ada!
printf5F9ivision of Dd and Dd is DdF,a,b,aCb7V
breakV
case a#a!
case ara!
printf5F$odulus of Dd and Dd is DdF,a,b,a
Db7V
breakV
default!
printf5FInvalid "ptionF7V
L
getch57V
L
1rogram : ele)"ill#c
Qrite a menu dri%en program0mdp1 to compute te
electricity "ill for te following purpose
Commercial 1urpose
<nits Aate6<nit
4 0 24 ;.&25min7

G24 3.24
E(tras :
#s.4.43 per unit power tax
ser&ice chrge:
#s.:4 0 single phase
#s.24 0 three phase
0omestic 1urpose

<nits Aate6<nit
4 0 24 :.<25min7
2: 0 :44 2.'4
:4: 0 244 ;.42
24: 0 ;44 <.)2
G;44 2.24
E(tras :
#s.4.43 per unit power tax

ser&ice charge:
#s.24 0 single phase
#s.24 0 three phase
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int cno,pmr,lmr,tu,opt,phV
char cnameI24JV
float pt,sc,bamt,tamtV
clrscr57V
printf5FPtPt$>@*PnPnF7V
printf5F000000000000000000000F7V
printf5FPn:09omestic ,ruposePnF7V
printf5FPn20-ommerical ,urposePnF7V
printf5FPn>nter your option5: or 27 ! F7V
scanf5FDdF,[opt7V
if5opt]A: [[ opt]A27
K
printf5FPnPtPtInvalid "ptionF7V
getch57V
exit547V
L
printf5FPn>nter ,hase Type5: or ;7F7V
scanf5FDdF,[ph7V
if5ph]A: [[ ph]A;7
K
printf5FPnPtPtInvalid ,haseF7V
getch57V
exit547V
L
clrscr57V
printf5FPn>nter -onsumer @umber ! F7V
scanf5FDdF,[cno7V
printf5FPn>nter -onsumer @ame ! F7V
fflush5stdin7V
gets5cname7V
printf5FPn>nter ,resent $onth #eading ! F7V
scanf5FDdF,[pmr7V
printf5FPn>nter +ast $onth #eading ! F7V
scanf5FDdF,[lmr7V
if5lmrGpmr7
K
printf5FPnPtPtInvalid #eadingF7V
getch57V
exit547V
L
tuApmr0lmrV
switch5opt7
K
case :!
if5tuEA247
bamtA24B:.<2V
else if5tuEA:447
bamtA524B:.<27.5tu0247B2.'4V
else if5tuEA2447
bamtA524B:.<27.524B2.'47.5tu0:447B;.42V
else if5tuEA;447
bamtA524B:.<27.524B2.'47.5:44B;.427.
5tu02447B<.)2V
else
bamtA524B:.<27.524B2.'47.5:44B;.427.5:44B<.)27.
5tu0;447B2.24V
ptA4.43BtuV

if5phAA:7
scA:4.44V
else
scA24.44V
breakV

case 2!
if5tuEA247
bamtA24B;.&2V
else
bamtA524B;.&27.5tu0247B3.24V
ptA4.43BtuV
if5phAA:7
scA24.44V
else
scA24.44V
breakV
L
tamtAbamt.pt.scV
clrscr57V
printf5FPtPtPt -onsumer 9etailsPn F7V
printf5F0000000000000000000000000000000000000000000000000PnF7V
printf5FPn-onsumer @umber ! DdF,cno7V
printf5FPn-onumer @ame ! DsF,cname7V
printf5FPn,resent $onth #eading ! DdF,pmr7V
printf5FPn+ast $onth #eading ! DdF,lmr7V
printf5FPnTotal *nits ! DdF,tu7V
printf5FPn,ower Tax ! D.2fF,pt7V
printf5FPnervice -harge ! D.2fF,sc7V
printf5FPn?ill Amount ! D.2fF,bamt7V
printf5FPnTotal Amount ! D.2fF,tamt7V
getch57V
L
<ncondition control statements :
- supports ; types of unconditional control
statements. They are
162. break
163. continue
164. goto
%rea+ :

It is an unconditional control statement and is
used to terminate a switch statement or a loop
statement.
Synta( :
breakV
continue :
It passed the control
Synta( :
continueV
-auses the control to pass to the end of the
innermost enclosing while, do or for statement, at
which point the loop continuation condition is
revaluated.
goto :
It is uncondition control statement which is used
to alter the execution of the program sequence by
transfer of control to some other part of the program.
Synta( :
goto labelV
%here label is valid - identifier used to the label
of the destination such that the control could
transferred.
Synta( of la%el :
identi%er N
1rogram : nat:goto#c
To display natural num"ers from ( to gi%en num"er
using goto statement
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int n,iA:V
clrscr57V
printf5F>nter a number !F7V
scanf5FDdF,[n7V
printf5F@atural @umbers from : to Dd !F,n7V
lb!
printf5FPtDdF,i7V
i..V
if5iEAn7
goto lbV
getch57V
L
1rogram : goto"#c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int nV
clrscr57V
printf5F>nter value of n !F7V
scanf5FDdF,[n7V
if5nAA:7
goto lb:V
if5nAA27
goto lb2V
printf5FPn%elcomeF7V
lb:!
printf5FPn+abel :F7V
lb2!
printf5FPn+abel 2F7V
getch57V
L
1rogram : goto2#c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int nV
clrscr57V
printf5F>nter value of n ! F7V
scanf5FDdF,[n7V
if5nAA:7
goto lb:V
if5nAA27
goto lb2V
printf5FPn%elcomeF7V
goto lbV
lb:!
printf5FPn+abel :F7V
goto lbV
lb2!
printf5FPn+abel 2F7V
lb!
getch57V
L
goto(y :
It moves the cursor to the given position in the
current text window.
Synta( : void gotoxy5int x,int y7V
x 0 cols
y 0 rows
default -olumns 5'47 and #ows5227
Note : If the coordinates are invalid, the call to
gotoxy is ignored.
1rogram : goto(y#c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
clrscr57V
gotoxy5;2,:27V
printf5F%>+-"$>F7V
getch57V
L
1rogram : st:formt#c
To calculate and display student result "y te
following format
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
R
OCPS SOPTQANE LIJITEC
SITAUAQACA
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
R
SIC : S!AJE :
JANVS I! C :
JANVS I! CPP :
JANVS I! TASA :
TOTAL JANVS : ASENA@E :
NESKLT : CISISIO! :
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
R
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int sid,c,cpp,Hava,totV
char snameI24JV
float avgV
clrscr57V
gotoxy5:2,;7V
printf5F000000000000000000000000000000000000000000000000F7V
gotoxy52',27V
printf5F?9, "QT%A#> +I$IT>9F7V
gotoxy5;;,)7V
printf5FMI/AbA%A9AF7V
gotoxy5:2,&7V
printf5F000000000000000000000000000000000000000000000000F7V
gotoxy5:2,::7V
printf5FI9 ! F7V
gotoxy5<4,::7V
printf5F@A$> ! F7V
gotoxy5:2,:<7V
printf5F$A#W I@ - ! F7V
gotoxy5:2,:37V
printf5F$A#W I@ -,, ! F7V
gotoxy5:2,:'7V
printf5F$A#W I@ /AMA ! F7V
gotoxy5:2,2:7V
printf5FT"TA+ $A#W ! F7V
gotoxy5<4,2:7V
printf5FAM>#A=> ! F7V
gotoxy5:2,2;7V
printf5F#>*+T ! F7V
gotoxy5<4,2;7V
printf5F9IMII"@ ! F7V
gotoxy5:2,227V
printf5F000000000000000000000000000000000000000000000000F7V
gotoxy52:,::7V
scanf5FDdF,[sid7V
gotoxy52:,::7V
fflush5stdin7V
gets5sname7V
lb:!
gotoxy5;:,:<7V
scanf5FDdF,[c7V
if5cG:447
K
gotoxy5;4,;47V
printf5FInvalid $arks of -F7V
getch57V
gotoxy5;4,;47V
printf5F F7V
gotoxy5;:,:<7V
printf5F F7V
goto lb:V
L
lb2!
gotoxy5;:,:37V
scanf5FDdF,[cpp7V
if5cppG:447
K
gotoxy5;4,;47V
printf5FInvalid $arks of -,,F7V
getch57V
gotoxy5;4,;47V
printf5F F7V
gotoxy5;:,:37V
printf5F F7V
goto lb2V
L
lb;!
gotoxy5;:,:'7V
scanf5FDdF,[Hava7V
if5HavaG:447
K
gotoxy5;4,;47V
printf5FInvalid $arks of /avaF7V
getch57V
gotoxy5;4,;47V
printf5F F7V
gotoxy5;:,:'7V
printf5F F7V
goto lb;V
L
totAc.cpp.HavaV
avgA5float7totC;V
gotoxy5;:,2:7V
printf5FDdF,tot7V
gotoxy52:,2:7V
printf5FD.2fF,avg7V
if5cGA24 [[ cppGA24 [[ HavaGA247
K
gotoxy5;:,2;7V
printf5F,AF7V
gotoxy52:,2;7V
if5avgGA347
printf5FQI#TF7V
else
printf5F>-"@9F7V
L
else
K
gotoxy5;:,2;7V
printf5FQAI+F7V
gotoxy52:,2;7V
printf5F@" 9IMII"@F7V
L
getch57V
L
Loop control statements :
loop!
The process of repeatedly executing a block of
statement up to specified number of times is called as
loop.
- supports ; types of looping statements. They are !
:7 while loop
27 do0while loop
;7 for loop
Ghile loop 2Entry control loop statement# :
It is a conditional controlled loop statement in -
language.
Synta( :
while5test condition7
K
tatementsV
L
In this loop first the test condition will be
evaluated. If it is true, then the statement block will
be executed. After the execution of statements, the
test condition will be evaluated once again, if it is
true then the statement block will be executed once
again. This process of repeated execution continued
until the test condition becomes false.
1rogram : nnos:,#c
To print natural num"ers from ( to gi%en num"er
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int n,iA:V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
printf5F@atural number from : to DdPnPnF,n7V
while5iEAn7
K
printf5FPtDdF,i7V
i..V
L
getch57V
L
1rogram : e&:od:,#c
To display e%en and odd num"ers for ( to gi%en
num"er
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int n,iV
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
printf5FPnPn>ven numbers from : to DdPnPnF,n7V
iA:V
while5iEAn7
K
if5iD2AA47
printf5FPtDdF,i7V
i..V
L
printf5FPnPnPn"dd numbers from : to DdPnPnF,n7V
iA:V
while5iEAn7
K
if5iD2AA:7 CC if5iD2]A47
printf5FPtDdF,i7V
i..V
L
getch57V
L
1rogram : nsum:,#c
To print sum of natural num"ers for ( to gi%en
num"er
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int n,iA:,sumA4V
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
while5iEAn7
K
sumAsum.iV
i..V
L
printf5Fum of Dd natural numbers is DdF,n,sum7V
getch57V
L
Note :
%e can also find the sum of n digits by the following
formula !
um A n B 5n.:7C2
1rogram : facts:,#c
To display factors of gi%en num"er
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int n,iA:V
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
printf5FPnPnQactors of given number ! PnPnF7V
while5iEAn7
K
if5nDiAA47
printf5FPtDdF,i7V
i..V
L
getch57V
L
1rogram : facto:,#c
To find factorial of gi%en num"er
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int nV
unsigned long factA:V
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
while5nGA:7
K
factAfactBnV
n00V
L
printf5FPnPnQactorial of Dd is Dlu F,n,fact7V
getch57V
L
1rogram : ndig:,#c
To find num"er of digits in te gi%en num"er
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int countA4V
unsigned long nV
clrscr57V
printf5F>nter a number ! F7V
scanf5FDluF,[n7V
while5nG47
K
nAnC:4V
count..V
L
printf5FPnPn@umber of digits of given number ! Dd
F,count7V
getch57V
L
1rogram : sumdig:,#c
To find sum of digits in te gi%en num"er
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int sumA4V
unsigned long nV
clrscr57V
printf5F>nter a number ! F7V
scanf5FDluF,[n7V
while5nG47
K
sumAsum.5nD:47V
nAnC:4V
L
printf5FPnPnum of digits of given number ! Dd
F,sum7V
getch57V
L
1rogram : re&:,#c
To find re%erse num"er of a gi%en num"er using
wile loop
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int nV
unsigned long revA4V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
while5nG47
K
revA5revB:47.5nD:47V
nAnC:4V
L
printf5F#everse number of given number !
DluF,rev7V
getch57V
L
1rogram : sum:dig"#c
To find sum of digits of gi%en num"er 0final sum1
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
unsigned long nV
int sumA4V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
while5nG47
K
sumAsum.5nD:47V
nAnC:4V
L
printf5Fum of digits ! DdF,sum7V
getch57V
L
1rogram : sum:dig2#c
To find sum of digits of gi%en num"er
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
unsigned long nV
int sumA4V
printf5F>nter a number ! F7V
scanf5FDluF,[n7V
while5nG47
K
sumAsum.5nD:47V
nAnC:4V
if5nAA4 [[ sumG&7
K
printf5FPnDdPnF,sum7V
nAsumV
sumA4V
L
L
printf5Fum of digits ! DdF,sum7V
getch57V
L
Note :
If any number exactly divides with &, then sum of
digits A &, otherwise its remainder value will be sum
of its digits. The above program depends on this
technique.
1rogram : re&":,#c
To find re%erse num"er of a gi%en num"er
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int n,countA4V
unsigned long revA4V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
while5nG47
K
revA5revB:47.5nD:47V
nAnC:4V
count..V
L
printf5F#everse number of given number !
DBluF,count,rev7V
getch57V
L
If nA:444, then we will get the reverse number as :,
i.e. 8eros are not displayed, if we use the above
format, the compiler will format it and gives the
output as 444:, this is because the number of digits in
the given number, i.e. count A < and the value of this
count is assigned to B, and hence the output have <
digits. i.e. 444: in case of nA:444
1rogram : pal:,#c
To ceck weter te gi%en num"er is palindrome
or not
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int n,m,revA4V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
mAnV
while5mG47
K
revA5revB:47.5mD:47V
mAmC:4V
L
if5nAArev7
printf5F=iven number is a ,alindromeF7V
else
printf5F=iven number is not ,alindromeF7V
getch57V
L
1rogram : prime:,#c
To ceck weter te gi%en num"er is prime or not
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int n,iA:,countA4V
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
while5iEAn7
K
if5nDiAA47
count..V
i..V
L
if5countAA27
printf5F=iven number is ,rimeF7V
else
printf5F=iven numbr is not ,rimeF7V
getch57V
L
1rogram : fi%o:,#c
To generate fi"onacci series upto n terms
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int m,n,aA:,bA4,cA4V
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
printf5FQibonacci series !F7V
while5nGA:7
K
printf5FPtDdF,c7V
cAa.bV
aAbV
bAcV
n00V
L
getch57V
L
1rogram : e&:od":,#c
To ceck weter te gi%en num"er is e%en or odd
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int n,iA2,kA:V
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
while5kEA27
K
if5iAA27
printf5F>ven numbers from : to DdF,n7V
if5iAA:7
printf5F"dd numbers from : to DdF,n7V
printf5FDdPtF,i7V
iAi.2V
if5iGn7
K
iA:V
k..V
L
L
L
do-,hile loop : 2e(it control loop statement#
It is an alternative form of while loop. The only
difference between while and do0while is the
minimum number of execution of while is 8ero and
the minimum number of execution of do0while is
one.
Synta( :
do
K
tatementsV
L
while5test condition, G
In this loop first the statement block will be
executed, after the execution of statements, the test
condition will be evaluated. If it is true, then the
statement block will be executed once again, this
process of repeated execution continuous until the
test condition becomes false.

1rogram : nat:d,#c
Programs to display natural num"er from ( to gi%en
num"er is using doRwile loop
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int n,iA:V
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
printf5FPn@atural numbers from : to Dd !F,n7V
do
K
printf5FPtDdF,i7V
i..V
L while5iEAn7V
getch57V
L
for loop :
It is the most commonly used loop statement in
- language. It is consisting of ; expressions.
Synta( :
for5e)p1 G e)p3 G e)p!,
M
StatementsG
P
In this loop first expression is used to initiali8e the
index, second expression is used to test whether the
loop is to be continued or not 5test condition7 and the
third expression is used to change the index for
further iteration.
1rogram : nat:for#c
Programs to display natural num"er from ( to gi%en
num"er is using for loop
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int n,iV
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
printf5FPn@atural numbers from : to DdPnPnF,n7V
for5iA:ViEAnVi..7
K
printf5FPtDdF,i7V
L
getch57V
L
1rogram : re&:for#c
To find re%erse num"er of a gi%en num"er using for
loop
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int nV
unsigned long revV
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
for5revA4VnG4VnAnC:47
K
revA5revB:47.5nD:47V
L
printf5F#everse number of given number !
DluF,rev7V
getch57V
L
1rogram : perfect#c
To ceck weter te gi%en num"er is perfect
num"er or not
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int n,i,sumA4V
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
for5iA:ViEAnC2Vi..7
K
if5nDiAA47
sumAsum.iV
L
if5nAAsum7
printf5F=iven number is ,erfect numberF7V
else
printf5F=iven number is not ,erfect numberF7V
getch57V
L
1erfect num%er :
If a given number A sum of its divisibles except that
number, is called as perfect number.
Eg : 3, 2', <&3 etc
1rogram : armst#c
To ceck weter te gi%en num"er is armstrong
num"er of not
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int n,sumA4,m,rV
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
mAnV
while5mG47
K
rAmD:4V
sumAsum.5rBrBr7V
mAmC:4V
L
if5nAAsum7
printf5F=iven number is Armstrong numberF7V
else
printf5F=iven number is not Armstrong numberF7V
getch57V
L
1rogram : ta%le#c
To display multiplication ta"le of gi%en num"er
from ( to &=
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int n,iV
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
for5iA:ViEA24Vi..7
K
printf5FPnDd B Dd A DdF,n,i,nBi7V
L
getch57V
L
1rogram : continue#c
To display natural num"ers from ( to (== e'cept
A=,;= and :=
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int iA4V
clrscr57V
while5iE:447
K
i..V
if5iAA<4 ^^ iAA24 ^^ iAA347
continueV
printf5FDdPtF,i7V
L
getch57V
L
1rogram : gcd#c
To find @CC of gi%en & num"ers
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int a,b,g,iV
clrscr57V
printf5F>nter 2 numbers ! F7V
scanf5FDdDdF,[a,[b7V
for5iA:ViEAa [[ iEAbVi..7
K
if5aDiAA4 [[ bDiAA47
gAiV
L
printf5F=-9 of given numbers ! DdF,g7V
getch57V
L
Eg :
=-9 of :4 and 24 A :4
:4D:4 A 4 and 24D:4 A 4 also 5:4,247D2 A 4 and
5:4,247D2 A 4, but the greatest of 2, 2 and :4 is :4.
1ence =-9 A :4.
1rogram : lcm#c
Qrite a program To find LCJ of gi%en & num"ers
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int a,b,lcmV
clrscr57V
printf5F>nter 2 numbers ! F7V
scanf5FDdDdF,[a,[b7V
if5aGb7
lcmAaV
else
lcmAbV
while5:7
K
if5lcmDaAA4 [[ lcmDbAA47
breakV
lcm..V
L
printf5F+-9 of given numbers ! DdF,l7V
getch57V
L
Eg :
+-$ of :4 and :2 is ;4
;4D:4 A 4 and ;4D:4 A 4
2 nd $ethod
000000000000000000
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int a,b,lcmV
clrscr57V
printf5F>nter any two numbers !F7V
scanf5FDdDdF,[a,[b7V
if5aGb7
lcmAaV
else
lcmAbV
while5 lcmDa]A4 ^^ lcmDb]A47
K
lcm..V
L
printf5F+cm of Dd and Dd is DdF,a,b,lcm7V
getch57V
L
Nested loops :
*sing a loop statement, within another loop is called
as nested loop.
1rogram : prime:nl#c
To display prime num"ers from ( to gi%en num"er
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int n,i,H,countV
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
printf5F,rime @umbers for : to DdPnF,n7V
for5iA:ViEAnVi..7
K
countA4V
for5HA:VHEAiVH..7
K
if5iDHAA47
count..V
L
if5countAA27
printf5FPtDdF,i7V
L
getch57V
L
1rogram : prm2:nl#c
To display prime num"ers "etween gi%en & num"ers
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int n:,n2,i,H,countV
clrscr57V
printf5F>nter starting number ! F7V
scanf5FDdF,[n:7V
printf5F>nter ending number ! F7V
scanf5FDdF,[n27V
for5iAn:ViEAn2Vi..7
K
countA4V
for5HA:VHEAiVH..7
K
if5iDHAA47
count..V
L
if5countAA27
printf5FPtDdF,i7V
L
getch57V
L
1rogram : pal:nl#c
To display Palindrome num"ers from ( to n
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int n,rev,m,iV
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
for5iA:ViEAnVi..7
K
revA4V
mAiV
while5mG47
K
revA5revB:47.5mD:47V
mAmC:4V
L
if5revAAi7
printf5FPtDdF,i7V
L
getch57V
L
1rogram : perf:nl#c
To display perfect num"ers from ( to gi%en num"er
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int n,i,H,sumA4V
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
printf5F,erfect @umbers between : and Dd ! F,n7V
for5iA:ViEAnVi..7
K
sumA4V
for5HA:VHEAiC2VH..7
K
if5iDHAA47
sumAsum.HV
L
if5sumAAi7
printf5FPtDdF,i7V
L
getch57V
L
1rogram : armst:nl#c
To display Armstrong num"ers from ( to gi%en
num"er
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int n,m,r,arm,iV
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
printf5FArmstrong numbers from : to Dd ! PnF,n7V
for5iA:ViEAnVi..7
K
armA4V
mAiV
while5mG47
K
rAmD:4V
armAarm.5rBrBr7V
mAmC:4V
L
if5armAAi7
printf5FPtDdF,i7V
L
getch57V
L
1rogram : prm:fact#c
To calculate and display Prime Pactors of gi%en
num"er
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int n,iV
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
printf5F,rime Qactor of Dd ! PnF,n7V
while5nG:7
K
iA2V
while5:7
K
if5nDiAA47
K
printf5FPtDdF,i7V
nAnCiV
breakV
L
else
i..V
L
L
getch57V
L
>ormatting the output :
Integer :
:. int nA:44
printf5SDdT,n7V

;utput : :44
2. int nA:44
printf5SD2dT,n7V

;utput : :44
2 spaces
;. int nA:44
printf5SD02dT,n7V

;utput : :44
2 spaces
<. int nA2
printf5SD.2dT,n7V
;utput : 42
2. int nA2
printf5SD.;dT,n7V
;utput : 442
Character :
:. char cAZ#Z
printf5SDcT,c7V
;utput : #
2. char cAZ#Z
printf5SD<cT,c7V

;utput : #
; spaces
;. char cAZ#Z
printf5SD0<cT,c7V

;utput : Y#Z
; spaces
>loat :
:. float fA:<.:):'
printf5SDfT,f7V

;utput : :<.:):'44
2. float fA:<.:):'
printf5SD:2fT,f7V

;utput : :<.:):'44
; spaces
;. float fA:<.:):'
printf5SD0:2fT,f7V

;utput : :<.:):'44
; spaces
<. float fA:<.:):'
printf5SD.2fT,f7V

;utput : :<.:)

2. float fA:<.:):'
printf5SD).2fT,f7V
;utput : :<.:)

2 spaces
In ).2, ) indicates total number of digits including
decimal point, 2 represents number of digits after
decimal point.
String :
:. char stI:4JATwelcomeT
printf5SDsT,s7V

;utput : welcome
2. char stI:4JATwelcomeT
printf5SD&sT,s7V
;utput : welcome
2 spaces
;. char stI:4JATwelcomeT
printf5SD0&sT,s7V
;utput : welcome
2 spaces
<. char stI:4JATwelcomeT
printf5SD.;sT,s7V
;utput : wel
;ther important formats :
:. int nA:44, xA2V
printf5SDxdT,x,n7V QI printf5SD2dT,n7V
;utput : :44
2 spaces
2. char stI:4J A SwelcomeTV
int xA;V
printf5SD.BsT,x,st7V QI printf5SD.;sT,st7V
;utput : wel
1rogram : patrn"#c
To generate the gi&en pattern
,
,e
,el
,elc
,elco
,elcom
,elcome
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int xV
char stI:4JAFwelcomeFV
clrscr57V
for5xA:VxEA)Vx..7
K
printf5FPnD.BsF,x,st7V
L
getch57V
L
te(tmode2# :
It changes screen mode 5in text mode7.
Synta( : void textmode5int newmode7V
Constant ?alue Te(t mode
+AT$"9> 0: ,revious text mode
?%<4 4 ?lack and %hite
<4 columns
-<4 : -olor
<4 columns
?%'4 2 ?lack and %hite
'4 columns
-'4 ; -olor
'4 columns
$"@" ) $onochrome
'4 columns
-<;24 3< >=A 5>nhanced
<; line
graphic adapter7
M=A 5Mideo
24 line
graphic adapter7
1rogram : t(tmode"#c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
clrscr57V
textmode5:7V CC or textmode5-<47V
gotoxy5:),:27V
printf5F#A/IF7V
getch57V
L
1rogram : t(tmode2#c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int iV
clrscr57V
textmode53<7V CC or textmode5-<;247V
for5iA:ViEA<2Vi..7
K
printf5FPn#A/IF7V
L
getch57V
L

te(tcolor2# :
It selects a new character color in text mode.
Synta( : void textcolor5int newcolor7V
tee(t%ac+ground2# :
It selects a new text background color
Synta( : void textbackground5int new color7V
color 2te(tmode# :
Constant ?alue
Bac+ground >oreground
?+A-W 4 bes
bes
?+*> : b
b

=#>>@ 2 b
b
-bA@ ; b
b
#>9 < b
b
$A=>@TA 2 b
b
?#"%@ 3 b
b
+I=1T =#>b ) b
b
9A#W =#>b ' @
b

+I=1T ?+*> & @
b
+I=1T =#>>@ :4 @
b

+I=1T -bA@ :: @
b
+I=1T #>9 :2 @
b
+I=1T $A=>@TA :; @
b
b>++"% :< @
b
%1IT> :2 @
b
cprintf2# :
It is same as printf57. If we want to display colors in
text mode, we use cprintf57.
1rogram : t(tmode)#c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
clrscr57V
textmode5:7V
textcolor5#>9.?+I@W7V
textbackground5%1IT>7V
gotoxy5:),:27V
cprintf5F#A/IF7V
getch57V
L
1rogram : patrn2#c
To generate the follo,ing pattern
H H H H H
H H H H H
H H H H H
H H H H H
H H H H H

NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int i,H,nV
clrscr57V
printf5F>nter value of n ! F7V
scanf5FDdF,[n7V
for5iA:ViEAnVi..7
K
for5HA:VHEAnVH..7
K
printf5FD2cF,aBa7V
L
printf5FPnF7V
L
getch57V
L
1rogram : patrn)#c
To generate the follo,ing pattern
H
H H
H H H
H H H H
H H H H H
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int i,H,nV
clrscr57V
printf5F>nter value of n ! F7V
scanf5FDdF,[n7V
for5iA:ViEAnVi..7
K
for5HA:VHEAiVH..7
K
printf5FD2cF,aBa7V
L
printf5FPnF7V
L
getch57V
L
1rogram : patrn*#c
To generate the follo,ing pattern
"
2 2
) ) )
* * * *


NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int i,H,nV
clrscr57V
printf5F>nter value of n ! F7V
scanf5FDdF,[n7V
for5iA:ViEAnVi..7
K
for5HA:VHEAiVH..7
K
printf5FD;dF,i7V
L
printf5FPnF7V
L
getch57V
L
1rogram : patrn#c
To generate the follo,ing pattern
"
" 2
" 2 )
" 2 ) *
" 2 ) *
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int i,H,nV
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
for5iA:ViEAnVi..7
K
for5HA:VHEAiVH..7
K
printf5FD;dF,H7V
L
printf5FPnF7V
L
getch57V
L
1rogram : patrn!#c
To generate the follo,ing pattern
H
H H
H H H
H H H H
H H H H H
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int i,H,k,n,sV
clrscr57V
printf5F>nter value of n ! F7V
scanf5FDdF,[n7V
sAnB2V
for5iA:ViEAnVi..7
K
CB
for5kA:VkEAsVk..7
K
printf5F F7V
L

or BC
printf5FDBcF,s,;27V
for5HA:VHEAiVH..7
K
printf5FD<cF,aBa7V
L
printf5FPnF7V
sAs02V
L
getch57V
L
1rogram : patrn-#c
To generate the follo,ing pattern
"
2 2
) ) )
* * * *

NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int i,H,k,n,sV
clrscr57V
printf5F>nter value of n ! F7V
scanf5FDdF,[n7V
sAnB2V
for5iA:ViEAnVi..7
K
for5kA:VkEAsVk..7
K
printf5F F7V
L
for5HA:VHEAiVH..7
K
printf5FD<dF,i7V
L
printf5FPnPnF7V
sAs02V
L
getch57V
L
1rogram : patrn.#c
To generate the follo,ing pattern
H
H H
H H H
H H H H
H H H H H
H H H H
H H H
H H
H

NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int i,H,kA:,n,sV
clrscr57V
printf5F>nter value of n ! F7V
scanf5FDdF,[n7V
sAnB2V
for5iA:ViEAnB2Vi..7
K
printf5FDBcF,s,;27V
for5HA:VHEAkVH..7
K
printf5FD<cF,aBa7V
L
if5iEn7
K
sAs02V
k..V
L
else
K
sAs.2V
k00V
L
printf5FPnF7V
L
getch57V
L
1rogram : patrn/#c
To generate the follo,ing pattern
" 2 ) *
"E / . - !
"" "2 ") "* "
2E "/ ". "- "!
2" 22 2) 2* 2
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int i,H,n,kA:V
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
for5iA:ViEAnVi..7
K
for5HA:VHEAnVH..7
K
printf5FD<dF,k7V
if5HEn7
K
if5iD2AA47
k00V
else
k..V
L
L
printf5FPnF7V
kAk.nV
L
getch57V
L
1rogram : patrn"E#c
To generate the follo,ing pattern
" 2 ) *
" 2 ) *
* " 2 )
* ) " 2
* ) 2 "
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int i,H,k,n,mV
clrscr57V
printf5F>nter value of n ! F7V
scanf5FDdF,[n7V
mAnV
for5iA:ViEAnVi..7
K
for5HAnVHGmVH007
K
printf5FD;dF,H7V
L
for5kA:VkEAmVk..7
K
printf5FD;dF,k7V
L
printf5FPnF7V
m00V
L
getch57V
L
1rogram : patrn""#c
To generate the follo,ing pattern
A B C 0 E > 3 > E 0 C B A
A B C 0 E > > E 0 C B A
A B C 0 E E 0 C B A
A B C 0 0 C B A
A B C C B A
A B B A
A A
BC
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int i,H,p,xA),yA)V
clrscr57V
for5iA:ViEA)Vi..7
K
pA32V
for5HA:VHEA:;VH..7
K
if5iG: [[ 5HGAx [[ HEAy77
printf5FD2cF,;27V
else
printf5FD2cF,p7V
if5HE)7
p..V
else
p00V
L
printf5FPnF7V
if5iG:7
K
x00V
y..V
L
L
getch57V
L
1rogram : patrn"2#c
To generate the follo,ing pattern
A B C 0 E > 3 > E 0 C B A
A B C 0 E > > E 0 C B A
A B C 0 E E 0 C B A
A B C 0 0 C B A
A B C C B A
A B B A
A A
A B B A
A B C C B A
A B C 0 0 C B A
A B C 0 E E 0 C B A
A B C 0 E > > E 0 C B A
A B C 0 E > 3 > E 0 C B A
BC
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int i,H,p,xA),yA)V
clrscr57V
for5iA:ViEA)Vi..7
K
pA32V
for5HA:VHEA:;VH..7
K
if5iG: [[ 5HGAx [[ HEAy77
printf5FD2cF,;27V
else
printf5FD2cF,p7V
if5HE)7
p..V
else
p00V
L
printf5FPnF7V
if5iG:7
K
if5iE)7
K
x00V
y..V
L
else
K
x..V
y00V
L
L
L
getch57V
L
delay01 : Edos.hG
It suspends execution for specified interval 5milli
seconds7
Synta( : void delay5unsigned milliseconds7V
k"it01 :
It checks for currently available key strokes.
Synta( : int kbhit57
)setcursortype01 :
It selects cursor appearance.
Synta( : void Osetcursortype5int curOt7V
cur)t : set the cursor type to one of the following
165. O@"-*#"# ! turn of the
cursor
166. O"+I9-*#"# ! solid block
cursor
167. O@"#$A+-*#"# ! normal
underscore cursor
1rogram : raIiram#c
NincludeEstdio.hG
NincludeEconio.hG
NincludeEdos.hG
void main57
K
int cA:,c:A;3,rA:V
textmode5:7V
Osetcursortype5O@"-*#"#7V
while5]kbhit577
K
textbackground5%1IT>7V
clrscr57V
textcolor5?+*>7V
gotoxy5c,:27V
cprintf5FraHiT7V
textcolor5#>97V
gotoxy5c:,:27V
cprintf5FramF7V
textcolor5$A=>@TA7V
gotoxy5:',r7V
cprintf5F%>9F7V
c..V
c:00V
r..V
if5cG;37
K
cA:V
c:A;3V
L
if5rG227
rA:V
delay5:247V
L
getch57V
L
>unctions in C
Punction :
Qunction is a self contained block of statements and
it performs a particular task . It can be used at several
times in a program, but defined only once.
They are of 2 types.
168. +ibrary functions 5or7 pre0define
functions
169. *ser defined functions.
Li"rary functions:
The functions which are In0built with the compiler
are known as +ibrary functions 5or7 ,re0defined
functions.
Eg:
170. printf
171. scanf
172. getch
173. clrscr, etc.
Kserdefined functions:
*ser can define functions to do a task relavant to
their program.
such functions are called as userdefined functions.
0 Any function5 either pre0defined 5or7 user defined7
has ; things.
174. Qunction declaration
175. Qunction definition
176. Qunction calling
In case of +ibrary functions5pre0defined7, the
function declaration is in header files, function
definition is in - libraries and function calling is in
source program. ?ut in case of user defined functions
all three things are in source program.
>unction declaration:
Synta(:
returntype functionOname 5 IargumentsOlistJ 7 V
>unction definition:
Synta(:

returntype functionOname 5 IargumentsOlistJ 7
K
statementsV
L
>unction calling :
Synta(:

functionOname 5 IparameterOlistJ 7V
Note:
The arguments which are given at the time of
function declaration or function definition are called
as arguments or formal arguments. The arguments
which are given at the time of function calling are
called as parameters or actual arguments.
Eg:
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
void func57V CC declaration
clrscr57V
func57V CC calling
getch57V
L
void func57 CC definition
K
printf5S%eclome to - QunctionsT7V
L
Aules for Creating and accessing functions:
177. A function can be called by any number of
times
178. A function may or maynot receive
arguments.
179. A function may or may not return a value
180. If a function doesnot return any value, the
function return data type will be specified as
void.
181. If A function returns a value ,only one value
it can be returned.
182. If a function returnrs a value, the returning
value must be returned with statement FreturnF.
183. If a function returns a value ,the execution
of the return statement should be last.
184. If we cannot specify any returntype, the
function returns an int by default.
185. A function returns a value, the returning
value should match with the function return data
type.
186. A function is defined after or before the
main function.
187. ?efore calling a function, the function
declaration or definition is must and should.
188. If a function definition is specified before
the function call, then the function declaration is
not necessary.
189. A function is executed when the function is
call by its name.
190. If a function returns a value,the return value
is replaced by function calling statement.
191. The function definition should not be
terminated with a semicolon5V7
return2+ey,ord#:
>xits immediately from the currently executing
function to the calling routine, optionally returning a
value.
Synta(:
return IEexpressionGJ V
E(ample : ret:ma(#c
To find ma'imum %alue of gi%en & num"ers
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int maxval5int,int7V
int a,b,mV
clrscr57V
printf5F>nter any two numbrs ! F7V
scanf5FDdDdF,[a,[b7V
mAmaxval5a,b7V
printf5F$aximum Malue ! DdF,m7V
getch57V
L
int maxval5int x,int y7
K
if5xGy7
return xV
else
return yV
L
>unction categories or >unction prototypes:
A function ,depending on whether arguments are
present or not
and whether a value is returning or not,may belongs
to one of the following categories.
192. Qunction with no arguments and return no
value.
193. Qunction with arguments and no return
value.
194. Qunction with arguments and return value.
195. Qunction with no arguments and return
value.
>unction ,ith no arguments and no return &alue:
In this type the function has no arguments, it
doesnot receive any data from the calling function.
imilarly it doesnot return any value, the calling
function doesnot receive any data from called
function. o there is no data communication between
calling function and called function.
E(ample : cat(#c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
void sum57V
clrscr57V
sum57V
getch57V
L
void sum57
K
int a,bV
printf5T>nter any two numbers!T7V
scanf5FDdDdF,[a,[b7V
printf5TsumA DdT,a.b7V
L

>unction ,ith arguments and no return &alue:
In this type the function has some arguments, it
receives data from the calling function. ?ut it doesnot
return any value, the calling function doesnot receive
any data from the called function. o there is one way
data communication between calling function and
called function.
E(ample : cat&#c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
void sum5int,int7V
int a,b,sV
clrscr57V
printf5T>nter any two numbers !T7V
scanf5FDdDdF,[a,[b7V
sum5a,b7V
getch57V
L
void sum5int x,int y7
K
printf5TsumA DdT,x.y7V
L
>unction ,ith arguments and return &alue:
In this type the function has some arguments, it
receives data from the calling function. imilarly it
returns a value, the calling function receives data
from the called function. o there is two way data
communication between calling function and called
function.
E(ample : cat*#c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int sum5int,int7V
int a,b,sV
clrscr57V
printf5T>nter any two numbers !T7V
scanf5FDdDdF,[a,[b7V
sAsum5a,b7V
printf5TsumA DdT,s7V
getch57V
L
int sum5int x,int y7
K
return x.yV
L

>unction ,ith no arguments and return &alue:
In this type the function has no arguments, it
doesnot receive any data from the calling function.
?ut it returns a value, the calling function receives
data from the called function. o there is one way
data communication between calling function and
called function.
E(ample : catA#c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int sum57V
int sV
clrscr57V
sAsum57V
printf5Fsum A DdF,s7V
getch57V
L
int sum57
K
int a,bV
printf5T>nter any two numbers!T7V
scanf5FDdDdF,[a,[b7V
return a.bV
L
1rogram : fn:ma(#c
To find ma'imum %alue of gi%en & num"ers
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int maxval5int,int7V
int a,b,c,mV
clrscr57V
printf5F>nter ; numbrs ! F7V
scanf5FDdDdDdF,[a,[b,[c7V
mAmaxval5a,b7V
mAmaxval5m,c7V
printf5F$aximum Malue ! DdF,m7V
getch57V
L
int maxval5int x,int y7
K
if5xGy7
return xV
else
return yV
L
1rogram : nnos:fn#c
To display natural num"ers from ( to n
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
void disp5int7V
int nV
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
disp5n7V
getch57V
L
void disp5int x7
K
int iV
for5iA:ViEAxVi..7
K
printf5FDdPtF,i7V
L
L
1rogram : fact:fn#c
To calculate factorial of gi%en num"er
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
unsigned long fact5int7V
int nV
unsigned long fV
clrscr57V
printf5F>ntr a number ! F7V
scanf5FDdF,[n7V
fAfact5n7V
printf5FQactorial of Dd ! DluF,n,f7V
getch57V
L
unsigned long fact5int x7
K
unsigned long fA:V
while5xGA:7
K
fAfBxV
x00V
L
return fV
L
1rogram : re&:fn#c
To display re%erse num"er of gi%en num"er
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
unsigned long rev5int7V
int nV
unsigned long rV
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
rArev5n7V
printf5F#everse number of Dd ! DluF,n,r7V
getch57V
L
unsigned long rev5int x7
K
unsigned long rA4V
while5xG47
K
rArB:4.5xD:47V
xAxC:4V
L
return rV
L
1rogram : fi%o:fn#c
To generate fi"onacci series
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
void fibo5int7V
int nV
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
printf5FQibonacci series PnF7V
fibo5n7V
getch57V
L
void fibo5int x7
K
int i,aA4,bA:,cV
printf5FDdPtDdF,a,b7V
for5iA:ViEAx02Vi..7
K
cAa.bV
printf5FPtDdF,c7V
aAbV
bAcV
L
L
1rogram : lcm:gcd#c
To find LCJ and @CC of gi%en num"ers
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int lcm5int,int7V
int gcd5int,int7V
int l,g,a,bV
clrscr57V
printf5F>nter 2 numbers ! F7V
scanf5FDdDdF,[a,[b7V
lAlcm5a,b7V
gAgcd5a,b7V
printf5F+-$ of Dd and Dd ! DdPnF,a,b,l7V
printf5F=-9 of Dd and Dd ! DdF,a,b,g7V
getch57V
L
int lcm5int x,int y7
K
int lmV
if5xGy7
lmAxV
else
lmAyV
while5:7
K
if5lmDxAA4 [[ lmDyAA47
breakV
lm..V
L
return lmV
L
int gcd5int x,int y7
K
int gd,iV
for5iA:ViEAx [[ iEAyVi..7
K
if5xDiAA4 [[ yDiAA47
gdAiV
L
return gdV
L
"
5 nA4,rA47
n values
" "
5nA:,rA47 5nA:,rA:7
" 2 "
5nA2,rA47 5nA2,rA:7
5nA2,rA27
" ) )
"
5nA;,rA47 5nA;,rA:7 5nA;,rA27
5nA;,rA;7
" * ! *
"
5nA<,rA47 5nA<,rA:7 5nA<,rA27
5nA<,rA;7 5nA<,rA<7
r values
Storage classes
?y the declaration statement the memory is
allocated temporarily for all the variables. The si8e of
memory varies with respect to the type of the
variable. The availability of the variables for access
depends on its declaration type.
The storage class specifiers are used to specify
the life and scope of the variables with in blocks,
functions and the entire program.
There are < types of storage classes supported by -
language
@amely
196. automatic variables
197. static variables
198. external or global variables
199. register variables
automatic &aria%les :
These variables are declared inside a function
block.
If there is no storage class specifier before the
declaration of any variable inside a function block, by
default it takes auto storage class.
Storage : main memory
0efault &alue : garbage value
Scope : +ocal to the block in which it is
defined
Life : Till the control remains within the
block in
which it is defined
8ey,ord : auto
1rogram : auto"#c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
auto int a,bV
clrscr57V
printf5FPna A DdF,a7V
printf5FPnb A DdF,b7V
getch57V
L
Note ! The declaration of auto variables as global
variables is not allowed
static &aria%les :
The memory of static variables remains unchanged
until the end of the program.
Storage : main memory
0efault &alue : 8ero
Scope : local to the block in which it is
defined
Life : The value of static variable persists
between different
function calls 5 it cannot reinitiali8es
between
different function
calls 7
8ey,ord : static
1rogram : static"#c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
static int a,bV
clrscr57V
printf5FPna A DdF,a7V
printf5FPnb A DdF,b7V
getch57V
L
1rogram : static2#c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
void disp57V
int iV
clrscr57V
for5iA:ViEA:4Vi..7
K
disp57V
L
getch57V
L
void disp57
K
static int nA:V
printf5FDdPtF,n7V
n..V
L
;utput
: 2 ; < 2 3 ) ' & :4
1ere n is static, it cannot reinitiali8es between
function calls
Also if we declare n as automatic variable, we will
get
: : : : : : : : : :
e(ternal or glo%al &aria%les :
The variables that are both alive and active
throughout the entire program are known as global
variables.
Storage : main memory
0efault &alue : 8ero
Scope : global
Life : As long as the program execution
doesnot come to end
8ey,ord : extern
1rogram : e(tern"#c
NincludeEstdio.hG
NincludeEconio.hG
int a,bV
void main57
K

clrscr57V
printf5FPna A DdF,a7V
printf5FPnb A DdF,b7V
getch57V
L
1rogram : e(tern2#c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
extern int aV
clrscr57V
printf5Fa A DdF,a7V
getch57V
L
int aA:44V
Note :
%ithout the keyword e(tern in main57 block, we will
get garbage values of a. This is because a becomes
local to main57.
Aegister &aria%les :
%e can use register variables for frequently used
variables to improve the faster execution of program.
These variables are also declared inside a function
block.
Storage : -,* register
0efault &alue : =arbage value
Scope : +ocal to the block in which it is
defined
Life : Till the control within the block in
which it is
defined
8ey,ord : register
Note :
It supports only integral data type 5int and char7
1rogram : register#c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
register int a,bV
clrscr57V
printf5FPna A DdF,a7V
printf5FPnb A DdF,b7V
getch57V
L
Aecursi&e 2or # Aecursion >unction :
-alling a function within the same function
definition is called as recursive function.
If we want to work with recursive function, we must
follow the following 2 aspects.
200. -alling by itself
201. Termination condition
1rogram : nnos:rfn#c
To display natural num"ers from ( to n using
recursi%e function
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
void disp5int7V
int nV
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
printf5F@atural numbers from : to Dd ! PnPnF,n7V
disp5n7V
getch57V
L
void disp5int x7
K
if5xG:7
disp5x0:7V
printf5FDdPtF,x7V
L
Note :
If we want to display number from :44 to :, write the
printf5SDdPtT,x7 statement as the first statement in
recursive method.
1rogram : fact:rfn#c
To find factorial of gi%en num"er using recursi%e
function
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
unsigned long fact5int7V
int nV
unsigned long fV
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
fAfact5n7V
printf5FQactorial of Dd is DluF,n,f7V
getch57V
L
unsigned long fact5int x7
K
if5xEA:7
return :V
else
return xBfact5x0:7V
L
1rogram : ncr:rfn#c
To find ncr %alue using recursi%e function
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
unsigned long fact5int7V
int n,r,ncrV
printf5F>nter n and r values ! F7V
scanf5FDdDdF,[n,[r7V
ncrAfact5n7C5fact5n0r7Bfact5r77V
printf5Fncr value ! DdF,ncr7V
getch57V
nsigned long fact5int x7
K
if5xEA:7
return :V
else
return xBfact5x0:7V
L
Calculating ncr &alues :
<c2 A 3 I 5 < B 2 7 C 5 : B 27
'c; A 23 I 5 ' B ) B 3 7 C 5 : B 2 B ; 7
1rogram : pascal#c
To display Pascal Triangle
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
unsigned long fact5int7V
int i,H,k,n,ncr,sV
printf5F>nter n value ! F7V
scanf5FDdF,[n7V
sAnB2V
for5iA4ViEAnVi..7
K
CB for5kA:VkEAsVk..7
K
printf5F F7V
L BC
printf5FDBcF,s,;27V
for5HA4VHEAiVH..7
K
ncrAfact5i7C5fact5i0H7Bfact5H77V
printf5FDd F,ncr7V
L
printf5FPnPnF7V
sAs02V
L
getch57V
L
unsigned long fact5int x7
K
if5xEA:7
return :V
else
return xBfact5x0:7V
L
1rogram : re&:rfn#c
To find re%erse num"er of gi%en num"er using
recursi%e function
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
unsigned long rev5int7V
unsigned long rV
int nV
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
rArev5n7V
printf5F#everse number ! DluF,r7V
getch57V
L
unsigned long rev5int x7
K
static unsigned long rA4V
rA5rB:47.5xD:47V
xAxC:4V
if5xG47
rev5x7V
return rV
L
Note :
If we declare r as automatic variable, we will get
only the last digit of the resultant number as output.
?ecause for each function call r is initiali8ed to 8ero
and only the last iteration value is stored in r, hence
we will get the last digit of the number as output. o
we should declare r as static.
1rogra
m : fi%o:rfn#c
To generate fi"onacci series using recursi%e
function
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
void fibo5int7V
int nV
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
fibo5n7V
getch57V
L
void fibo5int x7
K
static int aA:,bA4,cA4V
if5xGA:7
K
printf5FPtDdF,c7V
cAa.bV
aAbV
bAcV
fibo5x0:7V
L
L
1rogram : mn:mn#c
To display natural num"ers using recursi%e
function0main witin anoter main1
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
static int iA:,nV
if5iAA:7
K
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
L
printf5FDdPtF,i7V
i..V
if5iEAn7
main57V
getch57V
exit547V
L
Note :
1ere we are using main57 within main57, and main57
returns void data type, we have to press the enter key
n times5if nA2, 2 times7 to come out from the
program, to avoid this we will use exit547. -heck this
without giving exit547.
4ath'h >unctions :
sWrt :
-alculates the square root of a given number.
Synta( : double sqrt5double x7V
1rogram : s9rt#c
To find sWuare root of gi%en num"er
NincludeEstdio.hG
NincludeEconio.hG
NincludeEmath.hG
void main57
K
int nV
double sV
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
sAsqrt5n7V
printf5Fquare root of Dd A D.;lfF,n,s7V
getch57V
L
Pow : -alculates exponential value of given base
and power
Synta( :
double pow5double x,double y7V
1ere x is base and y is power.
1rogram : po,er#c
To find e'ponential %alue of gi%en "ase and power
NincludeEstdio.hG
NincludeEconio.hG
NincludeEmath.hG
void main57
K
int b,pV
double eV
printf5F>nter base and power values ! F7V
scanf5FDdDdF,[b,[p7V
eApow5b,p7V
printf5F>xponential value A D.;lfF,e7V
getch57V
L
floor : It rounds down the given value
Synta( : double floor5double x7V
ceil : It rounds up the given value.
Synta( : double ceil5double x7V
1rogram : flor:ceil#c
To find floor and ceil %alues of a gi%en num"er
NincludeEstdio.hG
NincludeEconio.hG
NincludeEmath.hG
void main57
K
double n,f,cV
clrscr57V
printf5F>nter a number ! F7V
scanf5FDlfF,[n7V
fAfloor5n7V
cAceil5n7V
printf5FQloor Malue A D.2lfF,f7V
printf5FPn-eil Malue A D.2lfF,c7V
getch57V
L
a"s : It is a macro that gets the absolute value of an
integer
Synta( : int abs5int x7V
1rogram : a%s#c
To display a"solute %alue of gi%en num"er
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int n,aV
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
aAabs5n7V
printf5F=iven value ! DdF,n7V
printf5FPnAbsolute value ! DdF,a7V
getch57V
L
1rogram : triangle#c
To find area of gi%en triangle wit * sides
NincludeEstdio.hG
NincludeEconio.hG
NincludeEmath.hG
void main57
K
float a,b,c,s,areaV
printf5F>nter sides of a triangle ! F7V
scanf5FDfDfDfF,[a,[b,[c7V
if5a.bEAc ^^ a.cEAb ^^ b.cEAa7
K
printf5Funable to form a triangleF7V
getch57V
exit547V
L
sA5a.b.c7C2V
areaAsqrt5sB5s0a7B5s0b7B5s0c77V
printf5FArea of triangle A D.2f sq unitsF,area7V
getch57V
L
1rogram : compound#c
To enter principal amount, rate of interest and time,
ten calculate total amount wit compound interest
NincludeEstdio.hG
NincludeEconio.hG
NincludeEmath.hG
void main57
K
int y,m,nV
float p,r,i,totV
clrscr57V
printf5F>nter ,rincipal Amount ! F7V
scanf5FDfF,[p7V
printf5F>nter #ate of Interest ! F7V
scanf5FDfF,[r7V
printf5F>nter time5years and months7 ! F7V
scanf5FDdDdF,[y,[m7V
nAyB:2.mV
totApBpow5:.5rC:447,n7V
iAtot0pV
printf5FInterest ! D.2fF,i7V
printf5FPnTotal Amount ! D.2fF,tot7V
getch57V
L
1rogram : sim)ser#c
E%aluate te following simple series
( L (7& L (7* L (7A L #############
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int i,nV
float sumA4V
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
for5iA:ViEAnVi..7
K
sumAsum.5float7:CiV
L
printf5Fum A D.2fF,sum7V
getch57V
L
1rogram : e(p)ser#c
E%aluate te following e'ponential series
( L ' L 0' power & 17fact0&1 L 0' power *17fact0*1
L ############### L
0' power n17fact0n1
NincludeEstdio.hG
NincludeEconio.hG
NincludeEmath.hG
void main57
K
unsigned long fact5int7V
int i,x,nV
float sumA4V
clrscr57V
printf5F>nter a value of x and n ! F7V
scanf5FDdDdF,[x,[n7V
for5iA:ViEAnVi..7
K
sumAsum.pow5x,i7Cfact5i7V
L
printf5Fum A D.2fF,sum7V
getch57V
L
unsigned long fact5int x7
K
if5xEA:7
return :V
else
return xBfact5x0:7V
L
1rogram : sin)ser#c
E%aluate te following sin' series
' R 0' power *17fact0*1 L 0' power ;17fact0;1 R 0'
power ?17fact0?1 L ######
NincludeEstdio.hG
NincludeEconio.hG
NincludeEmath.hG
void main57
K
unsigned long fact5int7V
int i,n,x,pA:V
float sumA4V
clrscr57V
printf5F>nter value of x and n ! F7V
scanf5FDdDdF,[x,[n7V
for5iA:ViEAnVi..7
K
if5iD2AA47
sumAsum0pow5x,p7Cfact5p7V
else
sumAsum.pow5x,p7Cfact5p7V
pAp.2V
L
printf5Fum A D.2fF,sum7V
getch57V
L
unsigned long fact5int x7
K
if5xEA:7
return :V
else
return xBfact5x0:7V
L
1rogram : cos)ser#c
E%aluate te following cos' series
( R 0' power &17fact0&1 L 0' power A17fact0A1 R 0'
power :17fact0:1 L ######
NincludeEstdio.hG
NincludeEconio.hG
NincludeEmath.hG
void main57
K
unsigned long fact5int7V
int i,n,x,pA4V
float sumA4V
clrscr57V
printf5F>nter value of x and n ! F7V
scanf5FDdDdF,[x,[n7V
for5iA:ViEAnVi..7
K
if5iD2AA47
sumAsum0pow5x,p7Cfact5p7V
else
sumAsum.pow5x,p7Cfact5p7V
pAp.2V
L
printf5Fum A D.2fF,sum7V
getch57V
L
unsigned long fact5int x7
K
if5xEA:7
return :V
else
return xBfact5x0:7V
L
int a,b,c,d,e,fV

Arrays

Array is a collection of data items 5 variables7 of
same datatype, stored in a continuous memory
locations and it can be referred as
a common name. A particular value in the array is
indicated by using a number called index or subscript
in square braces after the array name.
Types of arrays :
C supports three types of arrays$they are
202. "neCsingle dimensional arrays
203. Two Cdouble dimensional arrays
204. $ulti dimensional arrays
;ne6single dimensional array :
A group of data items can be given one variable
name, using only one index, such a variable is called
as single dimensional array. In single dimensional
array the elements are represented one after the other
in edges of memory bytes.
0eclaration :
datatype arrOnameIsi8eJV
Eg : int aI2JV
Elements are : aI4J,aI:J,aI2J,aI;J,aI<JV
Note : In any array, the array index is from 4 to si8e
0 :

Initiali=ation :
optional
datatype arrOnameIsi8eJ A Kval0:,val02,UUUUU..val0
nLV
Eg : int aI2JAK:,2,;,<,2LV
5"r7
int aIJAK:,2,;,<,2,3,)LV
1rogram : arr"'c
To initiali+e (C array and to display elements
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int i,aI2JAK:,2,;,<,2LV
clrscr57V
for5iA4ViE2Vi..7
K
printf5FPn aIDdJ A DdF,i,aIiJ7V
L
getch57V
L
1rogram : arr2'c
To accept array elements and to display te
elements
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int i,n,aI24JV
clrscr57V
printf5F>nter number of elements ! F7V
scanf5FDdF,[n7V
if5nG247
K
printf5FInvalid si8eF7V
getch57V
exit547V
L
printf5FPn>nter >lements ! F7V
for5iA4ViEnVi..7
K
scanf5FDdF,[aIiJ7V
L
printf5FPn=iven >lements ! F7V
for5iA4ViEnVi..7
K
printf5FPn aIDdJ A DdF,i,aIiJ7V
L
getch57V
L
1rogram : arr)'c
To accept (C array and to display ma'imum and
minimum elements
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int i,n,aI24J,max,minV
clrscr57V
printf5F>nter number of >lements ! F7V
scanf5FDdF,[n7V
printf5FPn>nter >lements ! F7V
for5iA4ViEnVi..7
K
scanf5FDdF,[aIiJ7V
L
maxAminAaI4JV
for5iA:ViEnVi..7
K
if5aIiJGmax7
maxAaIiJV
if5aIiJEmin7
minAaIiJV
L
printf5FPn$aximum >lement ! DdF,max7V
printf5FPn$inimum >lement ! DdF,min7V
getch57V
L
1rogram : arr*'c
To accept (C array and to searc specified element
in te gi%en array
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int aI24J,n,i,se,ckA4V
clrscr57V
printf5F>nter number of >lements ! F7V
scanf5FDdF,[n7V
printf5FPn>nter >lements PnPnF7V
for5iA4ViEnVi..7
K
printf5F>nter element in aIDdJ ! F,i7V
scanf5FDdF,[aIiJ7V
L
printf5FPnPn>nter >lement to earch ! F7V
scanf5FDdF,[se7V
for5iA4ViEnVi..7
K
if5aIiJAAse7
K
ckA:V
printf5FPnDd is found at aIDdJF,se,i7V
L
L
if5ckAA47
printf5FDd is not foundF,se7V
getch57V
L
1rogram : arr'c
To accept (C array and to sort te gi%en elements in
ascending order7
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int aI24J,n,i,H,tV
clrscr57V
printf5F>nter number of >lements ! F7V
scanf5FDdF,[n7V
printf5FPn>nter >lements ! PnF7V
for5iA4ViEnVi..7
K
scanf5FDdF,[aIiJ7V
L
printf5FPnPn>lements ?efore orting ! F7V
for5iA4ViEnVi..7
K
printf5FPnaIDdJADdF,i,aIiJ7V
L
CC sorting CC
for5iA4ViEnVi..7
K
for5HAi.:VHEnVH..7
K
if5aIiJGaIHJ7
K
tAaIiJV
aIiJAaIHJV
aIHJAtV
L
L
L
printf5FPnPn>lements After orting ! F7V
for5iA4ViEnVi..7
K
printf5FPnaIDdJADdF,i,aIiJ7V
L
getch57V
L
1rogram : arr!'c
To accept and display (C array using functions
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
void accept5int IJ,int7V
void disp5int IJ,int7V
int aI24J,nV
clrscr57V
printf5F>nter number of elements ! F7V
scanf5FDdF,[n7V
printf5F>nter >lements ! F7V
accept5a,n7V
printf5F=iven >lements ! F7V
disp5a,n7V
getch57V
L
void accept5int xIJ,int s7
K
int iV
for5iA4ViEsVi..7
K
scanf5FDdF,[xIiJ7V
L
L
void disp5int xIJ,int s7
K
int iV
for5iA4ViEsVi..7
K
printf5FPtDdF,xIiJ7V
L
L
1rogram : arr-'c
To accept (C array and display elements in
ascending order using functions
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
void accept5intIJ,int7V
void disp5intIJ,int7V
void sort5intIJ,int7V
int aI24J,nV
printf5F>nter number of elements ! F7V
scanf5FDdF,[n7V
printf5F>nter elements ! F7V
accept5a,n7V
printf5FPn>lements ?efore orting !F7V
disp5a,n7V
sort5a,n7V
printf5FPn>lements After orting ! F7V
disp5a,n7V
getch57V
L
void accept5int aIJ,int n7
K
int iV
for5iA4ViEnVi..7
K
scanf5FDdF,[aIiJ7V
L
L
void disp5int aIJ,int n7
K
int iV
for5iA4ViEnVi..7
K
printf5FPtDdF,aIiJ7V
L
L
void sort5int aIJ,int n7
K
int i,H,tV
for5iA4ViEn0:Vi..7
K
for5HAi.:VHEnVH..7
K
if5aIiJGaIHJ7
K
tAaIiJV
aIiJAaIHJV
aIHJAtV
L
L
L
L
1rogram : arr.'c
To accept (C array and searc specified element
using functions
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
void accept5intIJ,int7V
void disp5intIJ,int7V
int search5intIJ,int,int7V
int aI24J,n,se,pV
printf5F>nter number of elements ! F7V
scanf5FDdF,[n7V
printf5F>nter elements ! F7V
accept5a,n7V
printf5FPn=iven >lements !F7V
disp5a,n7V
printf5FPn>nter element to search ! F7V
scanf5FDdF,[se7V
pAsearch5a,n,se7V
if5pAA0:7
printf5FDd is not foundF,se7V
else
printf5FDd is found at position DdF,se,p7V
getch57V
L
void accept5int aIJ,int n7
K
int iV
for5iA4ViEnVi..7
K
scanf5FDdF,[aIiJ7V
L
L
void disp5int aIJ,int n7
K
int iV
for5iA4ViEnVi..7
K
printf5FPtDdF,aIiJ7V
L
L
int search5int aIJ,int n,int se7
K
int iV
for5iA4ViEnVi..7
K
if5aIiJAAse7
return iV
L
return 0:V
L
T,o dimensional arrays :
A T,o dimensional array can store a table of
values which contains rows and columns. In this
array we use 2 index values. The first index is for
rows and second is for columns.
The general form of T,o dimensional array is !
datatype arrOnameIrow si8eJIcolumn si8eJV
Eg : int aI;JI;JV
Elements are :
aI4JI4J aI4JI:J aI4JI2J
aI:JI4J aI:JI:J aI:JI2J
aI2JI4J aI2JI:J aI2JI2J

Initiali=ation :
>orm : "
optional
datatype arrOnameIrow si8eJIcolumn si8eJ A
KvalO:,valO2,UU,valOnLV
Eg :
205. int aI2JI2J A K:,2,;,<LV
206. int aI2JI2J A K:,2,;,<,2LV not possible
207. int aIJI2J A K:,2,;,<,2LV is possible
1ere the rows are allocated according to the
given elements and it will assign 8eros for
remaining values.
: 2
; <
2 4
>orm : 2
optional
data type arrOnameIrow si8eJIcolumn si8eJA K KvalO:,valO2,
UU,valOnL,
KvalO:,valO2,
UU,valOnL,
.....................
UUUU.
KvalO:,valO2,
UU,valOnL LV
Eg :
<7 int aI;JI;JAKK:,2,;L,
K<,2,3L,
K),',&LLV
: 2 ;
< 2 3
) ' &
27 int aI;JI;JAKK:L,K<,3L,K',&LLV
: 4 4
< 3 4
' & 4
If the values are missing it will assign 8eros in
that element place.
1rogram : arr""'c
To initiali+e &C array and display elements
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
CCint aI;JI;JAK:,2,;,<,2,3,),',&LV 5or7
int aI;JI;JAKK:,2,;L,
K<,2,3L,
K),',&LLV
int i,HV
clrscr57V
for5iA4ViE;Vi..7
K
for5HA4VHE;VH..7
K
printf5FPnaIDdJIDdJADdF,i,H,aIiJIHJ7V
L
L
getch57V
L
1rogram : arr"2'c
To accept &C array and display elements
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int aI:4JI:4J,r,c,i,HV
clrscr57V
printf5F>nter number of rows and columns ! F7V
scanf5FDdDdF,[r,[c7V
if5rG:4 ^^ cG:47
K
printf5FInvalid i8eF7V
getch57V
exit547V
L
printf5FPn>nter >lements ! F7V
for5iA4ViErVi..7
K
for5HA4VHEcVH..7
K
scanf5FDdF,[aIiJIHJ7V
L
L
printf5FPn=iven >lements ! PnF7V
for5iA4ViErVi..7
K
for5HA4VHEcVH..7
K
printf5FD;dF,aIiJIHJ7V
L
printf5FPnF7V
L
getch57V
L
1rogram : arr")'c
To accept & dimensional array and display elements
using functions
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
void accept5int IJI:4J,int,int7V
void disp5int IJI:4J,int,int7V
int aI:4JI:4J,r,cV
clrscr57V
printf5F>nter number of rows and columns ! F7V
scanf5FDdDdF,[r,[c7V
printf5FPn>nter >lements ! F7V
accept5a,r,c7V
printf5FPn=iven >lements ! F7V
disp5a,r,c7V
getch57V
L
void accept5int aIJI:4J,int r,int c7
K
int i,HV
for5iA4ViErVi..7
K
for5HA4VHEcVH..7
K
scanf5FDdF,[aIiJIHJ7V
L
L
L
void disp5int aIJI:4J,int r,int c7
K
int i,HV
for5iA4ViErVi..7
K
for5HA4VHEcVH..7
K
printf5FD;dF,aIiJIHJ7V
L
printf5FPnF7V
L
L
1rogram : arr"*'c
Addition of two Jatrices using arrays and
functions
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
void accept5int IJI:4J,int,int7V
void disp5int IJI:4J,int,int7V
void addt5int IJI:4J,intIJI:4J,intIJI:4J,int,int7V
int aI:4JI:4J,bI:4JI:4J,addI:4JI:4J,r,cV
clrscr57V
printf5F>nter number of rows and columns ! F7V
scanf5FDdDdF,[r,[c7V
printf5FPn>nter Qirst $atrix >lements ! F7V
accept5a,r,c7V
printf5FPn>nter econd $atrix >lements ! F7V
accept5b,r,c7V
printf5FPn>lements of =iven Qirst $atrix ! PnF7V
disp5a,r,c7V
printf5FPn>lements of =iven econd $atrix ! PnF7V
disp5b,r,c7V
addt5a,b,add,r,c7V
printf5FPn Addition of given 2 $atrices !PnF7V
disp5add,r,c7V
getch57V
L
void accept5int aIJI:4J,int r,int c7
K
int i,HV
for5iA4ViErVi..7
K
for5HA4VHEcVH..7
K
scanf5FDdF,[aIiJIHJ7V
L
L
L
void disp5int aIJI:4J,int r,int c7
K
int i,HV
for5iA4ViErVi..7
K
for5HA4VHEcVH..7
K
printf5FD;dF,aIiJIHJ7V
L
printf5FPnF7V
L
L
void addt5int aIJI:4J,int bIJI:4J,int addIJI:4J,int r,int
c7
K
int i,HV
for5iA4ViErVi..7
K
for5HA4VHEcVH..7
K
addIiJIHJAaIiJIHJ.bIiJIHJV
L
L
L
pogram : arr"'c
Jultiplicaton of two Jatrices using arrays and
functions
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
void accept5int IJI:4J,int,int7V
void disp5int IJI:4J,int,int7V
void mul5int IJI:4J,intIJI:4J,intIJI:4J,int,int,int7V
int aI:4JI:4J,bI:4JI:4J,cI:4JI:4J,m,n,p,qV
clrscr57V
printf5F>nter number of rows and columns of Qirst
$atrix ! F7V
scanf5FDdDdF,[m,[n7V
printf5F>nter number of rows and columns of
econd $atrix ! F7V
scanf5FDdDdF,[p,[q7V
if5n]Ap7
K
printf5FPn$atrix $ultiplication is not ,ossibleF7V
getch57V
exit547V
L
printf5FPn>nter Qirst $atrix >lements ! F7V
accept5a,m,n7V
printf5FPn>nter econd $atrix >lements ! F7V
accept5b,p,q7V
printf5FPn>lements of =iven Qirst $atrix ! PnF7V
disp5a,m,n7V
printf5FPn>lements of =iven econd $atrix ! PnF7V
disp5b,p,q7V
mul5a,b,c,m,n,q7V
printf5FPn$ultiplication of given $atrices !PnF7V
disp5c,m,q7V
getch57V
L
void accept5int aIJI:4J,int r,int c7
K
int i,HV
for5iA4ViErVi..7
K
for5HA4VHEcVH..7
K
scanf5FDdF,[aIiJIHJ7V
L
L
L
void disp5int aIJI:4J,int r,int c7
K
int i,HV
for5iA4ViErVi..7
K
for5HA4VHEcVH..7
K
printf5FD;dF,aIiJIHJ7V
L
printf5FPnF7V
L
L
void mul5int aIJI:4J,int bIJI:4J,int cIJI:4J,int m,int
n,int q7
K
int i,H,kV
for5iA4ViEmVi..7
K
for5HA4VHEqVH..7
K
cIiJIHJA4V
for5kA4VkEnVk..7
K
cIiJIHJAcIiJIHJ.5aIiJIkJBbIkJIHJ7V
L
L
L
L
1rogram : arr"!'c
To ceck weter te gi%en matri' is identity matri'
or not
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
void accept5int IJI:4J,int,int7V
void disp5int IJI:4J,int,int7V
int checkmat5int IJI:4J,int,int7V
int aI:4JI:4J,r,c,kV
printf5F>nter number of rows and columns ! F7V
scanf5FDdDdF,[r,[c7V
printf5FPn>nter >lements ! F7V
accept5a,r,c7V
printf5FPn=iven >lements ! F7V
disp5a,r,c7V
kAcheckmat5a,r,c7V
if5kAA:7
printf5F=iven $atrix is Indentity $atrixF7V
else
printf5F=iven $atrix is not Indentity $atrixF7V
getch57V
L
void accept5int aIJI:4J,int r,int c7
K
int i,HV
for5iA4ViErVi..7
K
for5HA4VHEcVH..7
K
scanf5FDdF,[aIiJIHJ7V
L
L
L
void disp5int aIJI:4J,int r,int c7
K
int i,HV
for5iA4ViErVi..7
K
for5HA4VHEcVH..7
K
printf5FD;dF,aIiJIHJ7V
L
printf5FPnF7V
L
L
int checkmat5int aIJI:4J,int r,int c7
K
int i,HV
for5iA4ViErVi..7
K
for5HA4VHEcVH..7
K
if5iAAH7
K
if5aIiJIHJ]A:7
return 4V
L
else
K
if5aIiJIHJ]A47
return 4V
L
L
L
return :V
L
1rogram : arr"-'c
To find Transpose of te gi%en matri'
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
void accept5int IJI:4J,int,int7V
void disp5int IJI:4J,int,int7V
void trans5int IJI:4J,intIJI:4J,int,int7V
int aI:4JI:4J,atI:4JI:4J,r,cV
clrscr57V
printf5F>nter number of rows and columns ! F7V
scanf5FDdDdF,[r,[c7V
printf5FPn>nter >lements ! F7V
accept5a,r,c7V
printf5FPn=iven >lements ! PnF7V
disp5a,r,c7V
trans5a,at,r,c7V
printf5FTranspose $atrix ! PnF7V
disp5at,c,r7V
getch57V
L
void accept5int aIJI:4J,int r,int c7
K
int i,HV
for5iA4ViErVi..7
K
for5HA4VHEcVH..7
K
scanf5FDdF,[aIiJIHJ7V
L
L
L
void disp5int aIJI:4J,int r,int c7
K
int i,HV
for5iA4ViErVi..7
K
for5HA4VHEcVH..7
K
printf5FD;dF,aIiJIHJ7V
L
printf5FPnF7V
L
L
void trans5int aIJI:4J,int atIJI:4J,int r,int c7
K
int i,HV
CB for5iA4ViErVi..7
K
for5HA4VHEcVH..7
K
atIHJIiJAaIiJIHJV
L
L
BC
for5iA4ViEcVi..7
K
for5HA4VHErVH..7
K
atIiJIHJAaIHJIiJV
L
L
L
4ulti dimensional arrays :
- supports arrays of ; or more dimensions. In $ulti
dimensional array we use more than 2 index values
Synta( :
9atatype arrOname Is:JIs2JUUUUIsnJV
Eg ! int aI2JI2JI;J
Elements : aI4JI4JI4J aI4JI4JI:J aI4JI4JI2J
aI4JI:JI4J aI4JI:JI:J aI4JI:JI2J
aI:JI4JI4J aI:JI4JI:J aI:JI4JI2J
aI:JI:JI4J aI:JI:JI:J aI:JI:JI2J
1rogram : arr".'c
To display multi dimensional array
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int aI2JI2JI;J,i,H,kV
for5iA4ViE2Vi..7
K
for5HA4VHE2VH..7
K
for5kA4VkE;Vk..7
K
printf5F>nter Malue into aIDdJIDdJIDdJ !
F,i,H,k7V
scanf5FDdF,[aIiJIHJIkJ7V
L
L
L
for5iA4ViE2Vi..7
K
for5HA4VHE2VH..7
K
for5kA4VkE;Vk..7
K
printf5FPn=iven Malue in aIDdJIDdJIDdJ A
DdF,i,H,k,aIiJIHJIkJ7V
L
L
L
getch57V
L
Strings
A group of characters defined between double
quotation marks is a string. It is a constant string
,?ut in - language, a string variable is nothing but an
array of characters and terminated by a null character
5P47.
0eclaration :
char identifier Isi8eJ V

Eg : char stI24J V
Initiali=ation :
At the time of declaring a string variable we can
store a constant string into the variable is called as
initiali8ation of a string.
optional
Synta( : char identifierIsi8eJ A SstringTV
Eg : char stI:4J A S%>+-"$>TV
char strI J A SA?-9T V


Note :
208. The compiler assigns a constant string
to a string variable, it automatically supplies a
null character at the end of a string. Therefore
the si8e should be equal to number of characters
in the string plus :.
209. In - language, string initiali8ation is
possible, but string assigning is not possible.
1rogram : str"'c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
char s:I:4JAF%>+-"$>FV
char s2IJAFA?-9FV
clrscr57V
printf5Fstring: A DsF,s:7V
printf5FPnstring2 A DsF,s27V
getch57V

L

gets :
It gets a string from stdin.

Synta( : char B gets5char Bs7V
Eg :
:7 gets5st7V
27 gets5SwelcomeT7V I not possible
puts :
It outputs a string to stdout and appends a new line
character.
Synta( : char B puts5const char Bs7V
Eg :
:7 puts5st7V
27 puts5SwelcomeT7V I possible
1rogram : str2'c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
char stI24JV
clrscr57V
puts5F>nter a tring ! F7V
gets5st7V
puts5F=iven tring ! F7V
puts5st7V
getch57V
L
getch:
It gets a character from the console but doesnot echo
to the screen.
Synta( : int getch57V
getche :
It gets a character from the console and echoes to the
screen.
Synta( : int getche57V
1rogram : str)'c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
char chV
char cheV
clrscr57V
printf5F>nter first character ! F7V
chAgetch57V
printf5F>nter second character ! F7V
cheAgetche57V
printf5FQirst character ! DcFch7V
printf5Fecond character ! DcFche7V
getch57V
L
Note :
@o need to press enter key after entering a character
because this function itself supplies a new line
character at the end of the character.
getchar :
It is a macro that gets a character from stdin.
Synta( : int getchar57V
putchar :
It is a macro that outputs a character on stdout.
Synta( : int putchar5int c7V
1rogram : str*'c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
char chV
clrscr57V
printf5F>nter a character ! F7V
chAgetchar57V
printf5F=iven character ! F7V
putchar5ch7V
getch57V
L
String handling functions :
These are included in the header file .string#/
strlen :
It calculates length of given string.
Synta( :
si8eOt strlen5const char Bs7V
1rogram : str'c
NincludeEstdio.hG
NincludeEconio.hG
NincludeEstring.hG
void main57
K
int lenV
char sI24JV
clrscr57V
printf5F>nter a string ! F7V
gets5s7V
lenAstrlen5s7V
printf5F+ength of given string ! DdF,len7V
getch57V
L
strcpy :
It copies one string to another string.
Synta( : char B strcpy5char Bdest, const char Bsrc7V
1rogram : str!'c
NincludeEstdio.hG
NincludeEconio.hG
NincludeEstring.hG
void main57
K
int lenV
char s:I24J,s2I24JV
clrscr57V
printf5F>nter a string ! F7V
gets5s:7V
strcpy5s2,s:7V
printf5FPn=iven string ! DsF,s:7V
printf5FPn-opied tring ! DsF,s27V
getch57V
L
strre& !
It reverses all characters in the given string except
for the terminating null.
Synta( : char B strrev5char Bs7V
1rogram : str-'c
NincludeEstdio.hG
NincludeEconio.hG
NincludeEstring.hG
void main57
K
char sI'4JV
clrscr57V
printf5F>nter a string ! F7V
gets5s7V
printf5F=iven string ! DsF,s7V
strrev5s7V
printf5FPn#everse string ! DsF,s7V
getch57V
L
strcat !
It appends one string to another.
Synta( : char B strcat5char Bdest, const char Bsrc7V
Note :
strcat appends a copy of src to the end of dest. The
length of resulting string is !
strlen5dest7 . strlen5src7
1rogram : str.'c
NincludeEstdio.hG
NincludeEconio.hG
NincludeEstring.hG
void main57
K
char s:I'4J,s2I'4JV
clrscr57V
printf5F>nter first string ! F7V
gets5s:7V
printf5F>nter second string ! F7V
gets5s27V
strcat5s:,s27V
printf5FPn-oncatenation of 2 strings ! DsF,s:7V
getch57V
L
strupr !
It converts lower case letters in the given string to
upper case.
Synta( : char B strupr5char Bs7V
strl,r !
It converts upper case letters in the given string
to lower case.
Synta( ! char B strlwr5char Bs7V
1rogram : str/'c
NincludeEstdio.hG
NincludeEconio.hG
NincludeEstring.hG
void main57
K
char sI'4JV
clrscr57V
printf5F>nter a string ! F7V
gets5s7V
printf5F=iven string ! DsF,s7V
strlwr5s7V
printf5FPn=iven string in lower case! DsF,s7V
strupr5s7V
printf5FPn=iven string in upper case! DsF,s7V
getch57V
L
strcmp ! It compares 2 string with case sensitivity.
Synta( : int strcmp5const char B s:, const char B
s27V
stricmp ! It compares 2 string without case
sensitivity.
Synta( ! int stricmp5const char B s:, const char B
s27V
strcmpi ! It is a macro which compares 2 string
without case sensitivity.
Synta( ! int strcmpi5const char B s:, const char B
s27V
Aeturn &alue :
These routines returns an int value
E 4 I s: E s2
A 4 I s: A s2
G 4 I s: G s2
1rogram : str"E'c
NincludeEstdio.hG
NincludeEconio.hG
NincludeEstring.hG
void main57
K
char s:I'4J,s2I'4JV
int nV
clrscr57V
printf5F>nter first string ! F7V
gets5s:7V
printf5F>nter second string ! F7V
gets5s27V
CC nAstrcmp5s:,s27V
nAstricmp5s:,s27V
CC nAstrcmpi5s:,s27V
if5nAA47
printf5FPn2 strings are equalF7V
if5nE47
printf5FPnQirst string is less than econd stringF7V
if5nG47
printf5FPnQirst string is greater than econd
stringF7V
getch57V
L
1rogram : str""'c
To check weather the given string is ,lindrome or
@ot
NincludeEstdio.hG
NincludeEconio.hG
NincludeEstring.hG
void main57
K
char s:I'4J,s2I'4JV
int nV
clrscr57V
printf5F>nter a string ! F7V
gets5s:7V
strcpy5s2,s:7V
strrev5s27V
nAstrcmp5s:,s27V
if5nAA47
printf5FPn=iven string is a ,alindromeF7V
else
printf5FPn=iven string is not a ,alindromeF7V
getch57V
L
1rogram : str"2'c
To find caracter freWuency of gi%en String
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
static int aI223J,iV
char stI'4JV
clrscr57V
printf5F>nter a tring ! F7V
gets5st7V
for5iA4ViEstrlen5st7Vi..7
K
aIstIiJJAaIstIiJJ.:V
L
printf5FPn-haracter QrequencyF7V
printf5FPn0000000000000000000000PnF7V
for5iA4ViE223Vi..7
K
if5aIiJ]A47
printf5FPnDc DdF,i,aIiJ7V
L
getch57V
L
1rogram : str")'c
To enter any password and display te password
string
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
char stI24J,chV
int iA4V
clrscr57V
gotoxy524,:27V
printf5F>nter ,assword ! F7V
while5:7
K
chAgetch57V
if5chAA:;7
breakV
if5chAA'7
K
if5iG47
K
printf5FPbDcPbF,;27V
i00V
L
L
else
K
stIiJAchV
i..V
printf5FBF7V
L
L
stIiJAaP4aV
gotoxy524,;47V
printf5F=iven tring ! DsF,st7V
getch57V
L
1rogram : str"*'c
To enter any String and drop caracters from top to
"ottom
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
char stI<4JV
int i,H,cV
clrscr57V
printf5F>nter a tring ! F7V
gets5st7V
Osetcursortype5O@"-*#"#7V
while5:7
K
cA5'40strlen5st77C2V
clrscr57V
gotoxy5c,:7V
printf5FDsF,st7V
for5iA4ViEstrlen5st7Vi..7
K
if5stIiJ]A47
K
for5HA2VHEA22VH..7
K
if5kbhit577
exit547V
gotoxy5c,H7V
printf5FDcF,stIiJ7V
gotoxy5c,H0:7V
printf5FDcF,;27V
delay5247V
L
L
c..V
L
L
L
1rogram : str"'c
To enter any String and drop caracters from top to
"ottom and again "ottom to top
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
char stI<4JV
int c,i,HV
clrscr57V
printf5F>nter a string ! F7V
gets5st7V
Osetcursortype5O@"-*#"#7V
while5]kbhit577 CC while5:7
K
cA5'40strlen5st77C2V
clrscr57V
gotoxy5c,:7V
printf5FDsF,st7V
for5iA4ViEstrlen5st7Vi..7
K
if5stIiJ]A;27
K
for5HA2VHEA22VH..7
K
CC if5kbhit577
CC exit547V
gotoxy5c,H7V
printf5FDcF,stIiJ7V
gotoxy5c,H0:7V
printf5FDcF,;27V
delay5247V
L
L
c..V
L
for5iAstrlen5st70:ViGA4Vi007
K
c00V
if5stIiJ]A;27
K
for5HA2<VHGA:VH007
K
CC if5kbhit577
CC exit547V
gotoxy5c,H7V
printf5FDcF,stIiJ7V
gotoxy5c,H.:7V
printf5FDcF,;27V
delay5247V
L
L
L
L
L
1rogram : str"!'c
Scrolling te gi%en string on screen center
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
char stI<4J,chV
int c,iV
clrscr57V
printf5F>nter a string ! F7V
gets5st7V
strcat5st,F F7V
strupr5st7V
clrscr57V
textmode5:7V
Osetcursortype5O@"-*#"#7V
textbackground5%1IT>7V
textcolor5#>97V
cA5<40strlen5st77C2V
gotoxy5c,:27V
cprintf5FDsF,st7V
while5]kbhit577
K
chAstI4JV
for5iA4ViEstrlen5st7Vi..7
K
stIiJAstIi.:JV
L
stIi0:JAchV
stIiJAaP4aV
gotoxy5c,:27V
cprintf5FDsF,st7V
delay5:447V
L
getch57V
L
T,o dimensional character arrays 2String array# :
A list of strings can be treated as a table of strings
and a two dimensional character array can be used to
store the entire list.
Por e'ample :
char stI24JI24J may be used to store a list of 24
strings, each of length not more than 24 characters.
1rogram : strarr"'c
To enter group of strings and to display tem
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
char stI24JI24JV
int n,iV
clrscr57V
printf5F>nter number of strings ! F7V
scanf5FDdF,[n7V
printf5F>nter Dd strings ! F,n7V
fflush5stdin7V
for5iA4ViEnVi..7
K
gets5stIiJ7V
L
printf5FPn=iven strings ! F7V
for5iA4ViEnVi..7
K
puts5stIiJ7V
L
getch57V
L
1rogram : strarr2'c
To enter group of strings and to display tem in
alpa"etical order
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
char stI24JI24J,tI24JV
int n,i,H,kV
clrscr57V
printf5F>nter number of strings ! F7V
scanf5FDdF,[n7V
printf5F>nter Dd strings ! PnF,n7V
fflush5stdin7V
for5iA4ViEnVi..7
K
gets5stIiJ7V
L
printf5FPn=iven strings ! PnF7V
for5iA4ViEnVi..7
K
puts5stIiJ7V
L
CC soring CC
for5iA4ViEn0:Vi..7
K
for5HAi.:VHEnVH..7
K
kAstrcmp5stIiJ,stIHJ7V
if5kG47
K
strcpy5t,stIiJ7V
strcpy5stIiJ,stIHJ7V
strcpy5stIHJ,t7V
L
L
L
printf5FPn=iven strings in Alphabetical order ! PnF7V
for5iA4ViEnVi..7
K
puts5stIiJ7V
L
getch57V
L

1reprocessor statements or 1reprocessor
directi&es
The processor is a program that process the
source code before it passes through the compiler.
The commands used to control the processor are
known as preprocessor directives.
These directives are divided into ; categories.
:. Qile inclusion directives
2. $acro substitution directives
;. -ompiler control directives
>ile inclusion directi&es:
5include:
It is a pre processor file inclusion directive and
is used to include header files. It provides instructions
to the compiler to link the functions from the system
library.
Synta(:
Ninclude Ffile nameF
5or7
Ninclude E file nameG
%hen the file name is included within S S,
the search for the file is made first the current
directory and then the standard directories. "therwise
the file name is included within E G, the file is
sarched only in the standard directories.
4acro su%stitution directi&es:
$acro is a process where an identifier in a program
is replaced by a predefined string or value.
5define:
It is a preprocessor statement and is used to
define macros.
Synta(:
>orm " : 2simple macro#
Ndefine EidentifierG EpredefinedOstring or
valueG

eg!
Ndefine pf printf
Ndefine sf scanf
Ndefine ,I ;.:<
Ndefine g &.'
1rogram : sim:mac'c
NincludeEstdio.hG
NincludeEconio.hG
Ndefine pf printf
Ndefine sf scanf
Ndefine vm void main57
Ndefine cls clrscr
Ndefine gt getch
Ndefine val :44
vm57
K
int nV
cls57V
pf5F>nter a number ! F7V
sf5FDdF,[n7V
pf5F=iven @umber A DdF,n7V
pf5FPnval A DdF,val7V
gt57V
L
>orm 2 : 2comple( macro or macro ,ith
arguments#
yntax!
Ndefine identifier5arg0:,arg02,UUU.arg0n7
definition
Eg:
Ndefine square5x7 xBx
1rogram : com(:mac'c
NincludeEstdio.hG
NincludeEconio.hG
Ndefine square5x7 xBx
void main57
K
int n,sV
clrscr57V
printf5F>nter a number ! F7V
scanf5FDdF,[n7V
sAsquare5n7V
printf5Fquare of given number A DdF,s7V
getch57V
L
0ifference %et,een function and macro :
>unction 4acro
:7It is a self contained block of statements. :7It is a
preprocessor statement.

27It replaces its return value 27It replaces its
definition.
;7%e can use only specified data types. ;79ata types
are generic.

<7>xecution speed is less. <7>xecution
speed is more
1rogram : macr:def'c
NincludeEstdio.hG
NincludeEconio.hG
CCNdefine square5x7 xBx
CCNdefine square5x7 5xBx7
void main57
K
int nV
nA:44Csquare527V
printf5Fn A DdF,n7V
getch57V
L
In the above example, if we gives
nA:44Csquare527, we should get :445according to
macros7. ?ecause it is a precpcessor statement and it
replaces its definition. i.e. according to macro
definition,
:44Csquare527 means :44C2B2A24B2A:44.
According to function definition,
:44Csquare527 means :44C22A<
1ence the difference between macro and a function is
in macros, the method name is replaced with its
definition. i.e. square5a7 is replaced with 2B2.
whereas in functions, the method is replaced with its
value. i.e. square527 is replaced with the value 22.
If we want to get the result of <, we should give
n A :44C5square5277
or
in the macro definition we should give Ndefine
square5a7 5aBa7
then, res A :44C5227
A <
1rogram : macr:ma('c
NincludeEstdio.hG
NincludeEconio.hG
Ndefine maxval5a,b7 aGb _ a!b
void main57
K
int a,b,maxV
clrscr57V
printf5F>nter any two values ! F7V
scanf5FDdDdF,[a,[b7V
maxAmaxval5a,b7V
printf5F$aximum value A DdF,max7V
getch57V
L
Note :
The are predefined macros namely max and min,
hence do not use these names for defining macros.
Compiler control directi&es:
These directives are used to control the compiler.
The following compiler control directives are used in
-.
210. Nif
211. Nelse
212. Nelif
213. Nendif etc
1rogram : comp:dir'c
NincludeEstdio.hG
NincludeEconio.hG
Ndefine n :4
Nif nEA:4
Ndefine val :44
Nelse
Ndefine val 244
Nendif
void main57
K
clrscr57V
printf5FMal A DdF,val7V
getch57V
L
6ointers
- provides the important feature of data
manipulations with the address of the variables.
1ence the execution time is very much reduced. uch
concept is possible with the special data type called
pointers.
0efinition :
A pointer is a variable which stores the address of
another variable.
0eclaration :
,ointer declaration is similar to normal variable
but preceded
by B symbol.
syntax!
datatype BidentifierV
eg!
int BpV
char Bch:,Bch2V
int x,y,Bp,8V
Initiali=ation :
At the time of declaring a pointer, we can store
some address into that pointer called as initiali8ation
of pointer.
Synta( : datatype Bidentifier A addressV
E(ample :
int aV
int Bp A [aV
Assigning Address to a pointer:
datatype BidentifierV
identifierAAddressV
eg!
int BpV
int aV
pA[aV

&oid H :
It is a generic pointer, it refers the address of any
type of variable and also it will convert to any type of
pointer.
eg!
void BpV
N<LL :
@ull pointer value 5empty address7
Note :
Any type of pointer, it allocates only 2 bytes of
memory ,?ecause it stores the address of memory
location. In - language the memory address is in
unsigned integer. 52 bytes for unsigned int, in the
range of 4 to 322;27
1rogram : ptr"'c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int Bp:V
char Bp2V
float Bp;V
double Bp<V
clrscr57V
printf5Fsi8e of int pointer A Dd bytesF,si8eof5p:77V
printf5FPnsi8e of char pointer A Dd
bytesF,si8eof5p277V
printf5FPnsi8e of float pointer A Dd
bytesF,si8eof5p;77V
printf5FPnsi8e of double pointer A Dd
bytesF,si8eof5p<77V
getch57V
L
Accessing pointers :
int aA:44V int aA:44V
int BpA[aV 5or7 int BpV
p A [aV


a 0 :44 Bp 0 :44
[a 0 2444 p 0 2444
a A 244 BpA244

1rogram : ptr2'c

NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int aA:44V
int BpA[nV
clrscr57V
printf5Fvalue of a A DdF,a7V
printf5FPnaddress of a A DluF,[a7V
printf5FPnvalue of a using p A DdF,Bp7V
printf5FPnaddress of a using p A DluF,p7V
nA244V
printf5FPn Bp A DdF,Bp7V
BpA;44V
printf5FPn a A DdF,a7V
getch57V
L
1ointers and >unctions :
Calling mechanism of argument function or 1arameter passing
techni9ues
214. -all by value or ,ass by value
215. -all by reference or ,ass by reference.
Call %y &alue :
The process of passing the value to the function call
is known as call by value.
1rogram : fn"'c
Passing constant %alue
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
void fun5int7V
clrscr57V
fun5:447V
getch57V
L
void fun5int x7
K
printf5FDdF,x7V
L
1rogram : fn2'c
Passing a %aria"le
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
void fun5int7V
int nV
clrscr57V
printf5F>nter any value ! F7V
scanf5FDdF,[n7V
fun5n7V
getch57V
L
void fun5int x7
K
printf5F=iven value A DdF,x7V
L
1rogram : fn)'c
Passing an e'pression
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
void fun5int7V
int a,bV
clrscr57V
printf5F>nter any two numbers ! F7V
scanf5FDdDdF,[a,[b7V
fun5a.b7V
getch57V
L
void fun5int x7
K
printf5Fum A DdF,x7V
L
Call %y reference :
The process of calling a function using pointers to
pass the address of the variables is called as call by
reference.
1rogram : ptrfn"'c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
void setdata5int B7V
int nV
clrscr57V
setdata5[n7V
printf5FnADdF,n7V
getch57V
L
void setdata5int Bp7
K
BpA:44V
L
1rogram : ptrfn2'c
To accept a %alue and display it using call "y
reference
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
void accept5int B7V
int nV
clrscr57V
accept5[n7V
printf5F=iven value A DdF,n7V
getch57V
L
void accept5int Bp7
K
printf5F>nter a value ! F7V
scanf5FDdF,p7V
L
1rogram : ptrfn)'c
Swaping of & %alues using call "y reference
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
void swap5int B,int B7V
int a,bV
clrscr57V
printf5F>nter 2 values ! F7V
scanf5FDdDdF,[a,[b7V
printf5FPn=iven values before swaping ! F7V
printf5FPnaADdF,a7V
printf5FPnbADdF,b7V
printf5FPnPn=iven values after swaping ! F7V
swap5[a,[b7V
printf5FPnaADdF,a7V
printf5FPnbADdF,b7V
getch57V
L
void swap5int Bx,int By7
K
int tV
tABxV
BxAByV
ByAtV
L
1ointers and Arrays :
%hen an array is declared, the compiler allocates a
base address and sufficient amount of storage to
contain all the elements of array in
continuous memory locations. The base address is the
location of the first element 5index A 4 7 of the array.
The compiler also defines the array name as a
constant pointer to the first element.
1rogram : ptrarr"'c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int aI2JV
clrscr57V
printf5F?ase address ! DuF,[aI4J7V
printf5FPn?ase address ! DuF,a7V
getch57V
L
uppose we declare an array a as follows !0
int aI2J A K:,2,;,<,2LV
uppose the base address of aA:444 and each
integer requires 2 bytes of memory,the 2 elements
will be stored as follows !0
The name aaa is defined as a constant pointer
pointing to the first element aI4J and therefore the
value of a is :444, the location where aI4J is stored.
1ence aA [aI4J A :444
If we declare p as an integer pointer, then we can
make the pointer p to point to the array a by the
following assignment.
int BpV
p A a 5or7 p A [aI4JV
@ow we can access every element of a using p..
or p.4, p.:, U.. to move one element to another. The
relationship between p and a is shown below.

p.4 A [aI4J A :444
p.: A [aI:J A :442
p.2 A [aI2J A :44<
p.; A [aI;J A :443
p.< A [aI<J A :44'
%hen handling arrays instead of using array index
we can use pointers to access array elements. @ote
that B5p.k7 gives the value of aIkJ.
To get the values of array elements !
B5 p.47 A aI4J A :
B5p.:7 A aI:J A 2
B5p.27A aI2J A ;
B5p.;7A aI;J A <
B5p.<7 A aI<J A 2
Note :
The pointer accessing method is much faster than
array indexing.
1rogram : ptrarr2'c
To accept Single dimension array and display array
elements using pointer accessing metod
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int aI24J,n,i,BpV
pAaV CCpA[aI4JV
clrscr57V
printf5F>nter number of elements ! F7V
scanf5FDdF,[n7V
printf5F>nter array elements ! F7V
for5iA4ViEnVi..7
K
scanf5FDdF,5p.i77V
CCdirectly we can give 5a.i7 without declaring Bp,
but it is not efficient
L
printf5FPn=iven array elements ! F7V
for5iA4ViEnVi..7
K
printf5FDdPtF,B5p.i77V
CC here also we can give B5a.i7, instead of B5p.i7
L
getch57V
L
1ointers and Strings
A pointer can also point to a string .
1rogram : ptrstr"'c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
char BstV
clrscr57V
printf5F>nter a tring ! F7V
gets5st7V
printf5F=iven tring ! DsF,st7V
getch57V
L
%e need not specify the si8e of tring,
the compiler
automatically allocates
sufficient amount of memory
String copying using 1ointers :
1rogram : ptrstr2'c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
char Bst:,Bst2V
clrscr57V
printf5F>nter a tring ! F7V
gets5st:7V
st2Ast:V
printf5F=iven tring ! DsF,st:7V
printf5FPn-opied tring ! DsF,st27V
getch57V
L
0ynamic 4emory Allocation204A# :
- language requires the number of elements in an
array to be specified at compile time. ?ut we may not
be able to do so always. "ur initial Hudgement of si8e,
if it is wrong may cause failure of program or
wastage of memory space. At this situation we can
use 9$A.
0efinition :
The process of allocating memory at run time is
known as 0ynamic 4emory Allocation 204A#'
malloc : Ealloc.hG
It is used to allocate memory at runtime.
Synta( : void B malloc5si8eOt si8e7V
si+e)t : It is the type used for memory obHect si8es
and repeat counts, also it is another name for
unsigned integer.
void B malloc5si8eOt si8e7V
pointer A 5typecast7 maclloc5memory si8e7V
Eg :
int BpV
p A 5int B7 malloc5si8eof5int77 V CC : location
p A 5int B7 malloc52 B si8eof5int77 V CC 2 locations
It allocates specified number of locations in the
heap5main memory7 and initiali8ed to garbage values.
calloc : Calloc'hD
It is also used to allocate memory at runtime.
Synta( : void B calloc5si8eOt nitems , si8eOt
si8e7V
pointer A 5typecast7 calloc5number of locations, si8e of
each location7V
Eg :
int BpV
p A 5int B7 calloc5:, si8eof5int77 V CC : location
p A 5int B7 calloc52, si8eof5int77 V CC 2 locations
It allocates specified number of locations in the
heap5main memory7 and initiali8ed to 8eroes.
free : Calloc'hD

It frees the allocated blocks.
Synta( : void free5void B block7V
1rogram : dma"'c
NincludeEstdio.hG
NincludeEconio.hG
NincludeEalloc.hG
void main57
K
int Bp:,Bp2,iV
p:A5int B7malloc52Bsi8eof5int77V
p2A5int B7calloc52,si8eof5int77V
clrscr57V
printf5F$A++"- ! F7V
for5iA4ViE2Vi..7
K
printf5FDdPtF,B5p:.i77V
L
printf5FPn-A++"- ! F7V
for5iA4ViE2Vi..7
K
printf5FDdPtF,B5p2.i77V
L
free5p:7V
free5p27V
getch57V
L
1rogram : dma2'c
NincludeEstdio.hG
NincludeEconio.hG
NincludeEalloc.hG
void main57
K
int Bp,n,iV
clrscr57V
printf5F>nter number of elements ! F7V
scanf5FDdF,[n7V
pA5int B7malloc5nBsi8eof5int77V
CCpA5int B7calloc5n,si8eof5int77V
printf5F>nter elements ! F7V
for5iA4ViEnVi..7
K
scanf5FDdF,5p.i77V
L
printf5FPn=iven elements ! F7V
for5iA4ViEnVi..7
K
printf5FDdPtF,B5p.i77V
L
free5p7V
getch57V
L
realloc :
It is included in the header file Ealloc.hG
It reallocates main memory at run time.
Synta( : void B realloc5void B block, si8eOt si8e7V
realloc adHusts the si8e of allocated block to si8e,
copying the contents to a new location.
1rogram : realloc"'c
NincludeEstdio.hG
NincludeEconio.hG
NincludeEalloc.hG
NincludeEstring.hG
void main57
K
char BstrV
clrscr57V
strA5char B7malloc5:47V
strcpy5str,F1ellowF7V
printf5F=iven tring ! DsF,str7V
strA5char B7realloc5str,227V
strcat5str,F %elcome to ?9,F7V
printf5FPn@ew tring ! DsF,str7V
free5str7V
getch57V
L
1rogram : realloc2'c
NincludeEstdio.hG
NincludeEconio.hG
NincludeEalloc.hG
void main57
K
int Bp,idA4,i,sV
char chV
clrscr57V
sAsi8eof5int7V
pA5int B7malloc5s7V
while5:7
K
printf5F>nter element ! F7V
scanf5FDdF,5p.id77V
id..V
printf5F9o u want to add another element 5yCn7 ! F7V
fflush5stdin7V
scanf5FDcF,[ch7V
if5chAAaya^^chAAaba7
K
sAs.si8eof5int7V
pA5int B7realloc5p,s7V
L
else
breakV
L
printf5FPn=iven elements ! F7V
for5iA4ViEidVi..7
K
printf5FDdPtF,B5p.i77V
L
free5p7V
getch57V
L
Structures
%e have seen that arrays can be used to
represent a group of data items that belongs to same
data type. If we want to represent a collection of data
items of different data types using a single name, then
we cannot use an array. - supports a constructed data
type known as tructure, which is a method for
packing data of different data types.
A structure is a convenient tool for handling a
group of logically related data items. tructures help
to organi8e complex data in a more meaningful way.
It is a powerful concept that we may often need to
use in our program design.
0efinition :
A group of data items that belongs to different data
types is known as tructure.
struct : It is a keyword and is used to declare a
tructure.
0eclaration :
>orm : "
0eclaration of structure :
struct structRname
M
data itemH1G
data itemH3G
0000
0000
data itemHnG
PG

Eg :
struct emp
K
int enoV
char enameI24JV
float salV
LV
0eclaration of structure &aria%le :
struct structOname identifierV
5or7
struct structOname dentifier0:,identifier0
2,.......,identifier0nV
Eg:
struct emp eV
5or7
struct emp e:,e2,e;V
>orm : 2
0eclaration of structure and structure &aria%le in a
single statement :
struct structOname
K
data item0:V
data item02V
UUUU
UUUU
data item0nV
LvarOlistV
Eg :
struct emp
K
int enoV
char enameI24JV
float salV
LeV
5or7
struct emp
K
int enoV
char enameI24JV
float salV
Le:,e2,e;V
' 2Access operator# :
It is used to access the data items of a structure
with the help of structure variable.
Synta( :
structOvariable . dataitemV
Eg:
e.enoV
e.enameV
e.salV
Initiali=ation :
>orm : "
struct structOname
K
data item0:V
data item02V
UUUU
UUUU
data item0nV
LV
struct structOname identifierAKvalue0:,value02,..,value0nLV
Eg :
struct emp
K
int enoV
char enameI24JV
float salV
LV
struct emp e A K:44,T1111T,24444.44LV
>orm : 2
struct structOname
K
data item0:V
data item02V
UUUU
UUUU
data item0nV
Lidentifier A Kvalue0:,value02,UUUUvalue0
nLV
Eg :
struct emp
K
int enoV
char enameI24JV
float salV
Le A K:44,T1111T,24444.44LV
1rogram : struct"'c
To initiali+e a structure and display its data
NincludeEstdio.hG
NincludeEconio.hG
struct emp
K
int enoV
char enameI24JV
float salV
LV
CCL eAK:,F1111F,&4444.44LV
void main57
K
struct emp eAK:,F1111F,&4444LV
clrscr57V
printf5FPn>mp number ! DdF,e.eno7V
printf5FPn>mp name ! DsF,e.ename7V
printf5FPnalary ! D.2fF,e.sal7V
getch57V
L
1rogram : struct2'c
To accept a structure and display te data
NincludeEstdio.hG
NincludeEconio.hG
struct emp
K
int enoV
char enameI24JV
float salV
LV
void main57
K
struct emp eV
clrscr57V
printf5F>nter emp number ! F7V
scanf5FDdF,[e.eno7V
printf5F>nter emp name ! F7V
fflush5stdin7V
gets5e.ename7V
printf5F>nter salary ! F7V
scanf5FDfF,[e.sal7V
printf5FPnPn=iven >mploy data F7V
printf5FPn0000000000000000000F7V
printf5FPn>mp number ! DdF,e.eno7V
printf5FPn>mp name ! DsF,e.ename7V
printf5FPnalary ! D.2fF,e.sal7V
getch57V
L
1rogram : struct)'c
To accept student details and to calculate and
display result
NincludeEstdio.hG
NincludeEconio.hG
NincludeEstring.hG
struct student
K
int sno,c,cpp,Hava,totV
float avgV
char snameI24J,resI:4J,divI:2JV
LsV
void main57
K
clrscr57V
printf5F>nter tudent number ! F7V
scanf5FDdF,[s.sno7V
printf5F>nter tudent name ! F7V
fflush5stdin7V
gets5s.sname7V
printf5F>nter marks in - ! F7V
scanf5FDdF,[s.c7V
printf5F>nter marks in -,, ! F7V
scanf5FDdF,[s.cpp7V
printf5F>nter marks in /AMA ! F7V
scanf5FDdF,[s.Hava7V
s.totAs.c.s.cpp.s.HavaV
s.avgA5float7s.totC;V
if5s.cGA24 [[ s.cppGA24 [[ s.HavaGA247
K
strcpy5s.res,F,AF7V
if5s.avgGA347
strcpy5s.div,FQI#TF7V
else
strcpy5s.div,F>-"@9F7V
L
else
K
strcpy5s.res,FQAI+F7V
strcpy5s.div,F@" 9IMII"@F7V
L
clrscr57V
printf5Ftudent 9etailsF7V
printf5FPn0000000000000000000000000F7V
printf5FPntudent number ! DdF,s.sno7V
printf5FPntudent name ! DsF,s.sname7V
printf5FPn$arks in - ! DdF,s.c7V
printf5FPn$arks in -,, ! DdF,s.cpp7V
printf5FPn$arks in /AMA ! DdF,s.Hava7V
printf5FPnTotal $arks ! DdF,s.tot7V
printf5FPnAverage $arks ! D.2fF,s.avg7V
printf5FPn#esult ! DsF,s.res7V
printf5FPn9ivision ! DsF,s.div7V
getch57V
L
Array of Structures :
+ike any other data type, structure arrays can be
defined, so that each array element can be of structure
data type.
Qor >xample,
struct student sI:44JV
which defines an array called s, that contains
:44 elements, each element is defined to be of type
struct student.

1rogram : struct:ar"'c
NincludeEstdio.hG
NincludeEconio.hG
struct item
K
int codeV
char nameI24JV
float costV
LV
void main57
K
struct item itI:44JV
int n,iV
float Bf,f:V
clrscr57V
fA[f:V
BfAf:V
printf5F>nter numer of records ! F7V
scanf5FDdF,[n7V
for5iA4ViEnVi..7
K
printf5FPnPn>nter #ecord ! DdF,i.:7V
printf5FPnPn>nter Item code ! F7V
scanf5FDdF,[itIiJ.code7V
printf5F>nter Item @ame ! F7V
fflush5stdin7V
gets5itIiJ.name7V
printf5F>nter Item cost ! F7V
scanf5FDfF,[itIiJ.cost7V
L
clrscr57V
printf5FD0:4sD0:2s
DsF,F-"9>F,F@A$>F,F-"TF7V
printf5FPn000000000000000000000000000000F7V
for5iA4ViEnVi..7
K
printf5FPnD0:4dD0:2s
D.2fF,itIiJ.code,itIiJ.name,itIiJ.cost7V
L
getch57V
L
The allocation of variables in heap contains 2 parts.
"ne is for integral data types 5int and char7, and the
other is for float type.
1eap 5main memory7
?ut it is different in case when we declare a structure.
%hen we define a structure with ; data types, one
is of integer, other is of character and another is of
float type, the memory for all the ; types are
allocated in integral part. %hen we compile this type
of programs we did not get any error but when we
execute the program, the program terminates
abnormally. This is because the link fails when
connecting floating values to the structure.
To solve this problem, we have to declare 2
float values, one is of pointer type and the other is
normal float value. The memory for float pointer is
allocated in the integral part 5because pointer has 2
bytes of unsigned int memory7 and the memory for
other float variable is allocated in the float part. This
normal variable points to the float pointer 5which
resides in integral part7, and thus we will established
a link between integral part and float part of heap.
Thus the programs executes normally.
1eap for tructure variables
1rogram : struct:ar2'c
To accept student details and to calculate and
display result using arrays
NincludeEstdio.hG
NincludeEconio.hG
NincludeEstring.hG
struct student
K
int sno,c,cpp,Hava,totV
float avgV
char snameI24J,resI:4J,divI:2JV
LV
void main57
K
struct student sI:44JV
int idA4,iV
clrscr57V
while5:7
K
printf5FPn>nter tudent number ! F7V
scanf5FDdF,[sIidJ.sno7V
printf5F>nter tudent name ! F7V
fflush5stdin7V
gets5sIidJ.sname7V
printf5F>nter marks in - ! F7V
scanf5FDdF,[sIidJ.c7V
printf5F>nter marks in -,, ! F7V
scanf5FDdF,[sIidJ.cpp7V
printf5F>nter marks in /AMA ! F7V
scanf5FDdF,[sIidJ.Hava7V
sIidJ.totAsIidJ.c.sIidJ.cpp.sIidJ.HavaV
sIidJ.avgA5float7sIidJ.totC;V
if5sIidJ.cGA24 [[ sIidJ.cppGA24 [[
sIidJ.HavaGA247
K
strcpy5sIidJ.res,F,AF7V
if5sIidJ.avgGA347
strcpy5sIidJ.div,FQI#TF7V
else
strcpy5sIidJ.div,F>-"@9F7V
L
else
K
strcpy5sIidJ.res,FQAI+F7V
strcpy5sIidJ.div,F@" 9IMII"@F7V
L
id..V
printf5FPn9o u want to add another #ecord5yCn7 ! F7V
fflush5stdin7V
scanf5FDcF,[ch7V
if5chAAana^^chAAa@a7
breakV
L
for5iA4ViEidVi..7
K
clrscr57V
printf5Ftudent 9etailsF7V
printf5FPn0000000000000000000000000F7V
printf5FPntudent number ! DdF,sIiJ.sno7V
printf5FPntudent name ! DsF,sIiJ.sname7V
printf5FPn$arks in - ! DdF,sIiJ.c7V
printf5FPn$arks in -,, ! DdF,sIiJ.cpp7V
printf5FPn$arks in /AMA ! DdF,sIiJ.Hava7V
printf5FPnTotal $arks ! DdF,sIiJ.tot7V
printf5FPnAverage $arks ! D.2fF,sIiJ.avg7V
printf5FPn#esult ! DsF,sIiJ.res7V
printf5FPn9ivision ! DsF,sIiJ.div7V
printf5FPnPn,ress any key to continue........F7V
L
L
1assing structure as function argument :
+ike any other data type a structure may also be
used as function argument.
Aeturning structure :
A function can not only receive a structure as its
arguments, but also can return them.
1rogram : s:pasret'c
NincludeEstdio.hG
NincludeEconio.hG
struct student
K
int snoV
char snameI24J,courseI24JV
float feeV
LV
struct student accept57
K
struct student sV
printf5F>nter tudent @umber ! F7V
scanf5FDdF,[s.sno7V
printf5F>nter tudent @ame ! F7V
fflush5stdin7V
gets5s.sname7V
printf5F>nter -ourse ! F7V
gets5s.course7V
printf5F>nter Qee ! F7V
scanf5FDfF,[s.fee7V
return sV
L
void disp5struct student s7
K
printf5FPnPnT*9>@T 9>TAI+F7V
printf5FPn0000000000000000000PnF7V
printf5FPntudent @umber ! DdF,s.sno7V
printf5FPntudent @ame ! DsF,s.sname7V
printf5FPn-ourse ! DsF,s.course7V
printf5FPnQees ! D.2fF,s.fee7V
L
void main57
K
struct student stV
clrscr57V
stAaccept57V CCreturning structure
disp5st7V CCpassing structure
getch57V
L
1ointers and Structures :
A pointer can also point to a structure.
>or e(ample :
struct student
K
int snoV
char snameI24J, courseI24JV
float feeV
JK
struct student sV
struct student BpV
1ere p is defined to be a pointer, pointing to student
structure,
we can write
p A [sV
After making such an assignment we can access
every data item of student structure through p '
S5Arrow7
00000000000000
0G is the combination of 0 followed by G .
It is used to access the data items of a structure
with the help of
structure pointer.
syntax!
tructOpointer 0G data itemV
>g!
pI snoV
pI snameV
pI courseV
pI feeV
1rogram : stru:ptr'c
NincludeEstdio.hG
NincludeEconio.hG
struct student
K
int snoV
char snameI24J,courseI24JV
float feeV
LV
void main57
K
struct student sV
struct student BpV
float Bf,f:V
fA[f:V
BfAf:V
pA[sV
clrscr57V
CB Indirect method
printf5F>nter tudent @umber ! F7V
scanf5FDdF,[5Bp7.sno7V
printf5F>nter tudent @ame ! F7V
fflush5stdin7V
gets55Bp7.sname7V
printf5F>nter -ourse ! F7V
gets55Bp7.course7V
printf5F>nter Qee ! F7V
scanf5FDfF,[5Bp7.fee7V
printf5FPnPnT*9>@T 9>TAI+F7V
printf5FPn0000000000000000000PnF7V
printf5FPntudent @umber ! DdF,5Bp7.sno7V
printf5FPntudent @ame ! DsF,5Bp7.sname7V
printf5FPn-ourse ! DsF,5Bp7.course7V
printf5FPnQees ! D.2fF,5Bp7.fee7V BC
CB 9irect method
printf5F>nter tudent @umber ! F7V
scanf5FDdF,[p0Gsno7V
printf5F>nter tudent @ame ! F7V
fflush5stdin7V
gets5p0Gsname7V
printf5F>nter -ourse ! F7V
gets5p0Gcourse7V
printf5F>nter Qee ! F7V
scanf5FDfF,[p0Gfee7V
printf5FPnPnT*9>@T 9>TAI+F7V
printf5FPn0000000000000000000PnF7V
printf5FPntudent @umber ! DdF,p0Gsno7V
printf5FPntudent @ame ! DsF,p0Gsname7V
printf5FPn-ourse ! DsF,p0Gcourse7V
printf5FPnQees ! D.2fF,p0Gfee7V BC
getch57V
L
1re defined structures :
"' struct 0ate :

It used to get current system date. It is included in
the header file E9".1G
Synta( :
struct date
K
int daOyearV CC current year
char daOdayV CC day of month
char daOmonV CC month 5/anA:7
LV
getdate :
It gets the current system date.
Synta( : void getdate5struct date Bd7V
1rogram : sysdate'c
To get current system date
NincludeEstdio.hG
NincludeEconio.hG
NincludeEdos.hG
void main57
K
struct date dV
clrscr57V
getdate5[d7V
printf5F-urrent ystem 9ate !
D.2d0D.2d0
DdF,d.daOday,d.daOmon,d.daOyear7V
L
2' struct Time :

It used to get current system time. It is included in
the header file E9".1G
Synta( :
struct time
K
unsigned char tiOminV CC minutes
unsigned char tiOhourV CC hours
unsigned char tiOsecV CC seconds
LV
gettime :
It gets the current system time.
Synta( : void gettime5struct time Bt7V
1rogram : systime'c
To get current system time
NincludeEstdio.hG
NincludeEconio.hG
NincludeEdos.hG
void main57
K
struct time tV
gettime5[t7V
printf5F-urrent ystem Time !0
D.2d!D.2d!D.2dF,t.tiOhour,t.tiOmin,t.tiOsec7V
L
struct ff%l+ :
*sing this structure we can get information of a file
or a directory.
It is included in the header file Edir.hG
Synta( :
struct ffblk
K
char ffOreservedI2:JV CCreserved by 9"
char ffOattribV CCattribute found
char ffOftimeV CCfile time
char ffOfdateV CCfile date
char ffOfsi8eV CCfile si8e
char ffOnameV CCfound file name
LV
findfirst2# :
It searches a disk directory for file.
Synta( : int findfirst5const char Bpathname,struct
ffblk Bf,int attrib7V
List of attri%utes :
These attributes are included in the header file
Edos.hG
C;NSTANT
0ESCAI1TI;N
QAO#9"@+b #ead only
attribute
QAO1I99>@ 1idden files
QAObT>$ ystem files
QAO+A?>+ Molume label
QAO9I#>- 9irectory
QAOA#-1 Archive file 5ie. all files
except
system
files and volume lebel7
Aeturn ?alue :
4 on success5match found7, else returns non 8ero
value.
findne(t2# :
It continues the search.
Synta( : int findnext5struct ffblk Bf7V
Aeturn ?alue :
4 on success5match found7, else returns non 8ero
value.
1rogram : ff%l+'c
NincludeEstdio.hG
NincludeEconio.hG
NincludeEdir.hG
NincludeEdos.hG
void main57
K
struct ffblk fV
int d,countA4V
char stI24JV
clrscr57V
printf5F>nter ,ath ! F7V
gets5st7V
dAfindfirst5st,[f,QAOA#-17V
while5]d7
K
printf5FPnDsF,f.ffOname7V
count..V
if5countD24AA47
K
printf5FPnPn,ress any key to -ontinue......F7V
getch57V
clrscr57V
L
dAfindnext5[f7V
L
printf5FPnPtTotal number of Qiles ! DdF,count7V
getch57V
L
;utput
:7 >nter path !
B.B
It will display all file in the current working
directory.
27 >nter path !
B.c
It will display all .- programs in the current
working directory.
Bit fields :
- permits us to use small bit fields to hold data.
%e have been using integer field of si8e :3 bit to
store data. The data item requires much less than :3
bits of space, in such case we waste memory space.
In this situation we use small bit fields in structures.
The bit fields data type is either int or unsigned
int. the maximum value that can store in unsigned int
filed is !0 52 power n 7 c : and in int filed is !0 2
power 5 n c :7 . 1ere YnZ is the bit length.
Note :
scanf57 statement cannot read data into bit fields
because scanf57 statement, scans on format data into 2
bytes address of the filed.
Synta( :
struct structOname
K
unsigned 5or7 int identifier: ! bitOlengthV
unsigned 5or7 int identifier2 ! bitOlengthV
UUUUUUUUUUUUUUUU.
UUUUUUUUUUUUUUUU.
unsigned 5or7 int identifier@ ! bitOlengthV
LV
1rogram : %it:stru'c
NincludeEstdio.hG
NincludeEconio.hG
struct emp
K
unsigned eno!)V
char enameI24JV
unsigned age!3V
float salV
unsigned ms!:V
LV
void main57
K
struct emp eV
int nV
clrscr57V
printf5F>nter eno ! F7V
scanf5FDdF,[n7V
e.enoAnV
printf5F>nter ename ! F7V
fflush5stdin7V
gets5e.ename7V
printf5F>nter age ! F7V
scanf5FDdF,[n7V
e.ageAnV
printf5F>nter salary ! F7V
scanf5FDfF,[e.sal7V
printf5F>nter $arital tatus ! F7V
scanf5FDdF,[n7V
e.msAnV
clrscr57V
printf5F>mploy number ! DdF,e.eno7V
printf5FPn>mploy name ! DsF,e.ename7V
printf5FPn>mploy age ! DdF,e.age7V
printf5FPn>mploy salary ! D.2fF,e.sal7V
printf5FPn$arital status ! DdF,e.ms7V
getch57V
L
<nion :
A union is similar to struct, except it allows us to
define variables that share storage space.
Synta( :
union unionOname
K
data item:V
data item2V
UUUU
data item0nV
L I varOlist JV

Eg :
union test
K
int nV
char chV
float ftV
L tV
1rogram : un:stru'c
NincludeEstdio.hG
NincludeEconio.hG
struct test:
K
int nV
char cV
float fV
Lt:V
union test2
K
int nV
char cV
float fV
Lt2V
void main57
K
clrscr57V
printf5F$emory si8e of struct ! Dd
bytesF,si8eof5t:77V
printf5FPn$emory si8e of union ! Dd
bytesF,si8eof5t277V
getch57V
L
;utput
"# $emory si8e of struct ! ) bytes
$emory si8e of union ! < bytes
STA<CT <NI;N
A group of data items that
belongs to different data
types
It is also same as struct, but
the only maHor difference is
memory allocation.
Allocates memory of all
declared data items in it
Allocates memory of
biggest data items in it
%e can access all data
items at a time
%e can access only one
data items at a time
>ach and every data items
has its own storage space
All the items shared
common storage space.
1rogram : union'c
NincludeEstdio.hG
NincludeEconio.hG
union emp
K
int enoV
char enameI24JV
float salV
LV
void main57
K
union emp eV
printf5F>nter empno ! F7V
scanf5FDdF,[e.eno7V
printf5F>mp number ! DdF,e.eno7V
printf5FPnPn>nter empname ! F7V
fflush5stdin7V
gets5e.ename7V
printf5F>mp name ! DsF,e.ename7V
printf5FPnPn>nter empsal ! F7V
scanf5FDfF,[e.sal7V
printf5Falary ! D.2fF,e.sal7V
L
Cdir'hD functions :
"' getc,d :
It gets the current working directory
Synta( : char B getcwd5char B buf, int buflen7V
1rogram : getc,d'c
NincludeEstdio.hG
NincludeEconio.hG
NincludeEdir.hG
void main57
K
char stI'4JV
clrscr57V
getcwd5st,'47V
printf5F-urrent %orking 9irectory ! DsF,st7V
getch57V
L
2' chdir :
It changes the current working directory
Synta( : int chdir5const char Bpath7V
Aeturn ?alue :
It returns 4 if success, else non 8ero.
1rogram : chdir'c
NincludeEstdio.hG
NincludeEconio.hG
NincludeEdir.hG
void main57
K
char stI'4JV
int nV
clrscr57V
printf5F>nter path ! F7V
gets5st7V
nAchdir5st7V
if5nAA47
printf5F9irectory -hangedF7V
else
printf5F9irectory not -hangedF7V
getch57V
L
;utput
>nter path !
>!PraHiPcppPprograms
%e will get the message S9irectory -hangedT
To check whether we have changed to that specific
directory or not, run the getcwd.c program in that
directory, then it will display the current working
directory as ! >!PraHiPcppPprograms.
)' m+dir :
It creates a directory
Synta( : int mkdir5const char Bpath7V
Aeturn ?alue :
It returns 4 if success, else non 8ero.
1rogram : m+dir'c
NincludeEstdio.hG
NincludeEconio.hG
NincludeEdir.hG
void main57
K
char stI'4JV
int nV
clrscr57V
printf5F>nter ,ath ! F7V
gets5st7V
nAmkdir5st7V
if5nAA47
printf5F9irectory -reatedF7V
else
printf5F*nable to -reate a 9irectoryF7V
L
;utput
>nter path !
rr
9irectory is created in our current directory.
If we gives the same path once again, we will get
S*nable to create a dirT
=o to command prompt and type dir, then directory
rr is displayed at the last
*' rmdir :
It removes a dos file directory.
Synta( : int rmdir5const char Bpath7V
Aeturn ?alue :
It returns 4 if success, else non 8ero.
rmdir deletes the directory whose path is given by
the path. The directory named by the path ,
216. must be
empty
217. must not
be current working directory
218. must not
be the root directory.
1rogram : rmdir'c
NincludeEstdio.hG
NincludeEconio.hG
NincludeEdir.hG
void main57
K
char stI24JV
int nV
clrscr57V
printf5F>nter ,ath ! F7V
gets5st7V
nArmdir5st7V
if5nAA47
printf5F9irectory is deletedF7V
else
printf5F*nable to delete a 9irectoryF7V
getch57V
L
;utput
=oto 9" shell
>!PraHiPcPprogramsG md raHi
>!PraHiPcPprogramsG cd raHi
9irectory created
>!PraHiPcPprogramsPraHiG copy con aa.txt
=ood morning
%elcome
-trl . 8
5:7 file is copied
>!PraHiPcPprogramsG type aa
Then the above data is displayed
>!PraHiPcPprogramsG cd ram
9irectory created
@ow our current working directory has 2 sub
directories, called raHi and ram.
%e can delete the sub directory ram by using rmdir57,
but we cannot deleted the directory raHi, because raHi
contain one text file aa.txt, but ram is empty, hence
we can remove it.
If we want to deleted the directory raHi, then again
goto 9" shell
>!PraHiPcPprogramsPraHiG del aa.txt
219. file
deleted.
@ow we can delete the folder raHi with the help of
rmdir57.
2iles
- language permits the usage of limited input and
output functions to read and write data. These
functions are used for only smaller volume of data
and it becomes difficult to handle longer data
volumes. Also the entire data is lost when the
program over.
To overcome these difficulties a flexible method
was developed by employing the concept of files to
store, to read and write data and to return them even
when the program over.
0efinition :
A file is one, which enable the user to store, to
read and write a group of related data.
5or7
220. A file is
a collection of records stored in a particular area
on the disk.
- supports a number of functions to have the ability
to perform the basic file operations, which include !
T naming a file
T opening a file
T reading data from file
T writing data to file
T closing a file
Types of files :
Qiles are two types, @amely
221. Text files
5or7 equential files
222. ?inary
files
Te(t files :
*sed for reading and writing data in the form of
characters. The memory si8e of each and every
character is : byte.
Binary files :
*sed for reading and writing data in the form of
data blocks. The memory si8e of each and every data
block depends on its data type.
>ILE :
It is a pre0defined structure and is used to declare a
file pointer. *sing this pointer, we can perform all file
operations.
0eclaration of >ILE pointer :
Synta( !
QI+> Bidentifier.

Eg: QI+> BfpV
>ile handling functions :
fopen : It opens a file stream.
S1nta5 :
;I.E U fopen5const char U%lename ,
const char Umode,G
filename ! file that the function opens
mode string :
There are various modes and are described
in the following
table
String 0escription
r
.
"pen for reading only.
, -reate for writing.
If a file by that name already exists, it will be
overwritten
a Append !
open for writing at end of file, or create for
writing if the file does not exist.
r7 "pen an existing file for update 5reading and
writing7
,7 -reate a new file for update 5reading and
writing7.
If a file by that name already exists, it will be
overwritten.
a7 "pen for append !
open for update at the end of the file, or create
if the file does not exist.
223. To specify
that a given file is being opened or created in text mode,
append FtF to the string 5rt, w.t, etc.7.
224. To specify
binary mode, append FbF to the string 5wb, a.b, etc.7.
fclose : It closes the file stream.
Synta( :
int fclose5QI+> Bstream7V
fcloseall : It closes all open file stream.
Synta( :
int fcloseall57V
1rogram : file"'c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
QI+> BfpV
clrscr57V
fpAfopen5Fa.txtF,FwF7V
if5fpAA@*++7
printf5F*nable to open a fileF7V
else
printf5FQile is openedF7V
getch57V
L
If we specify the mode SaT, we will get the same
output 5i.e. Qile is opened7, if we specify the mode
SrT, we will get the output as *nable to open a file.
Textfiles
getc : It is a macro that gets one character from a file
stream.
Synta( : int getc5QI+> Bstream7V
putc : It is a macro that outputs a character to a file
stream.
Synta( : int putc5int c,QI+> Bstream7V
fgetc : It is a function that gets one character from a
file stream.
Synta( :
int fgetc5QI+> Bstream7V
fputc : It is a function that outputs a character to a
file stream.

Synta( :
int fputc5int c,QI+> Bstream7V
E;> :
It is a constant indicating that end of file has
been reached on a Qile. To get this character from
keyboard press -trl.6 5or7 Q3
1rogram : file2'c
To create a te't file and to store data into tat file
troug key"oard
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
char chV
QI+> BfpV
clrscr57V
fpAfopen5Fa.txtF,FwtF7V
printf5F>nter data 5-trl.6 to stop7 ! F7V
chAgetchar57V
while5ch]A>"Q7
K
putc5ch,fp7V
chAgetchar57V
L
fclose5fp7V
printf5F9ata stored successfullyF7V
getch57V
L
1rogram : file)'c
To open a file and to read data from te file and
display on te monitor
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
QI+> BfpV
char chV
clrscr57V
fpAfopen5Fa.txtF,FrtF7V
printf5F#eading data from the filePnPnF7V
chAgetc5fp7V
while5ch]A>"Q7
K
printf5FDcF,ch7V
chAgetc5fp7V
L
fclose5fp7V
getch57V
L
1rogram : file*'c
To enter a file and display its contents on te
monitor
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
QI+> BfpV
char ch,stI24JV
clrscr57V
printf5F>nter Qile name ! F7V
gets5st7V
fpAfopen5st,FrtF7V
if5fpAA@*++7
K
printf5FPnQile not foundF7V
getch57V
exit547V
L
printf5FPnPn#eading data from the filePnPnF7V
chAgetc5fp7V
while5ch]A>"Q7
K
printf5FDcF,ch7V
chAgetc5fp7V
delay5:47V
L
fclose5fp7V
getch57V
L
1rogram : file'c
To enter a file and num"er of lines, words and
caracters in te file
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
QI+> BfpV
char ch,ch:,stI24JV
int ncA4,nwA4,nlA4V
clrscr57V
printf5F>nter Qile name ! F7V
gets5st7V
fpAfopen5st,FrtF7V
if5fpAA@*++7
K
printf5FPnQile not foundF7V
getch57V
exit547V
L
chAgetc5fp7V
while5ch]A>"Q7
K
if5chAA;2 ^^ chAAaPna7
K
nw..V
if5chAAaPna7
nl..V
L
else
nc..V
ch:AchV
chAgetc5fp7V
if5chAA;2 [[ ch:AAch7
nw00V
L
printf5FPn @umber of characters ! DdF,nc7V
printf5FPn @umber of words ! DdF,nw7V
printf5FPn @umber of lines ! DdF,nl7V
fclose5fp7V
getch57V
L
If more than one space is there between 2 words, the
compiler will treats the additional space as a word. To
avoid this use the above technique.
re,ind2# :
It repositions file pointer to file streamZs beginning
Synta( : void rewind5QI+> Bstream7V
1rogram : file!'c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
QI+> BfpV
char chV
clrscr57V
fpAfopen5Fa.txtF,Fa.tF7V
printf5F>nter data 5-trl.6 to stop7 ! F7V
chAgetchar57V
while5ch]A>"Q7
K
putc5ch,fp7V
chAgetchar57V
L
printf5FPn9ata stored successfullyF7V
rewind5fp7V
printf5FPnPn#eading data from the filePnPnF7V
chAgetc5fp7V
while5ch]A>"Q7
K
printf5FDcF,ch7V
chAgetc5fp7V
L
fclose5fp7V
getch57V
L
fgets2# :
It gets a string from file stream.
Synta( : char B fgets5char Bs,int n,QI+> Bstream7V
fgets reads characters from stream into the string s. it
stops when it reads either n0: characters of a new line
character which ever comes first.
fputs2# :
It outputs a string to a file stream.
Synta( : char B fputs5char Bs,QI+> Bstream7V
fgets copies the null terminated string s to the given
output stream. It doesnot append a new line character
and the terminating null character is not copied.
foef :

It is a macro that tests if end of file has been reached
on a file stream.
Synta( : int feof5QI+> Bstream7V

Aeturn &alue :
225. #eturns
non 8ero if an end of file indicator was detected
on the last input operation on the named stream.
226. #eturns
4 if end of file has not been reached.
1rogram : file-'c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
QI+> BfpV
char stI'4JV
clrscr57V
fpAfopen5Fstr.txtF,Fa.tF7V
printf5F>nter string data 5aenda to stop7 ! F7V
gets5st7V
while5strcmp5st,FendF7]A47
K
fputs5st,fp7V
fputs5FPnF,fp7V
gets5st7V
L
rewind5fp7V
printf5FPn#eading data from the file !PnF7V
fgets5st,'4,fp7V
while5]feof5fp77
K
printf5FDsF,st7V
fgets5st,'4,fp7V
L
getch57V
fclose5fp7V
L
;utput
>nter data !
#aHi
#am
#amraHi
#aHiram
?unny
end
fprintf:
It sends formatted output to a file stream.
S1nta5 : int fprintf5;I.E Ustream , const char
Uformat V,argument,0...W,G
fscanf :
It scans and formats input from a file stream.
S1nta5 :
int fscanf5;I.E Ustream,const char Uformat
V,address,0...W,G
Note :
In fprintf and fscanf, between formats at least one
space is necessary.
1rogram : file.'c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int enoV
char enameI24JV
float salV
QI+> BfpV
clrscr57V
fpAfopen5Femp.txtF,FwtF7V
printf5F>nter empno ! F7V
scanf5FDdF,[eno7V
printf5F>nter ename ! F7V
fflush5stdin7V
gets5ename7V
printf5F>nter sal ! F7V
scanf5FDfF,[sal7V
fprintf5fp,FDd Ds D.2fF,eno,ename,sal7V
fclose5fp7V
printf5FPn#ecord saved successfullyF7V
getch57V
L
1rogram : file/'c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int enoV
char enameI24JV
float salV
QI+> BfpV
clrscr57V
fpAfopen5Femp.txtF,FrtF7V
fscanf5fp,FDd Ds DfF,[eno,ename,[sal7V
fclose5fp7V
printf5FPn#eading data from fileF7V
printf5FPnPn>nter empno ! DdF,eno7V
printf5FPn>nter ename ! DsF,ename7V
printf5FPn>nter sal ! D.2fF,sal7V
getch57V
L
1rogram : file"E'c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
int enoV
char enameI24J,chV
float salV
QI+> BfpV
clrscr57V
fpAfopen5Femp:.txtF,Fa.tF7V
do
K
printf5F>nter empno ! F7V
scanf5FDdF,[eno7V
printf5F>nter ename ! F7V
fflush5stdin7V
gets5ename7V
printf5F>nter sal ! F7V
scanf5FDfF,[sal7V
fprintf5fp,FDd Ds D.2f PnF,eno,ename,sal7V
printf5FPnPn#ecord saved successfullyF7V
printf5FPnPn9o u want to add another record 5yCn7 !
F7V
fflush5stdin7V
scanf5FDcF,[ch7V
L while5ch]Aana7V
rewind5fp7V
printf5FPn#eading data from filePnPnF7V
printf5FD0:4sD0:2sDsF,F>@"F,F>@A$>F,FA+F7V
printf5FPn000000000000000000000000000000000000000000000000F7V
while5]feof5fp77
K
fscanf5fp,FDd Ds DfPnF,[eno,ename,[sal7V
printf5FPnD0:4dD0:2sD.2fF,eno,ename,sal7V
L
fclose5fp7V
getch57V
L
ftell :
It returns the current file pointer
Synta( ! long ftell5QI+> Bstream7V
fsee+ :
It repositions the file pointer of a filestream
Synta( :
int fseek5QI+> Bstream, long offset, int whence7V
stream : %hose file pointer fseek sets
offset :
9ifference in bytes between whence and new
position.
,hence :
"ne of three >>WOxxx file pointer locations 54, :,
or 27
Constant ?alue >ile location
>>WO>T 4 eeks from
beginning of file
>>WO-*# : eeks from
current position

>>WO>@9 2 eeks from end of
file
1rogram : file""'c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
QI+> BfpV
char chV
clrscr57V
fpAfopen5F8.txtF,FwF7V
printf5F-urrent file pointer position ! DluF,ftell5fp77V
printf5FPnPn>nter data 5-trl.6 to stop7! F7V
chAgetchar57V
while5ch]A>"Q7
K
putc5ch,fp7V
chAgetchar57V
L
printf5F,resent file pointer position ! DluF,ftell5fp77V
fseek5fp,0:4,:7V CC fseek5fp,0:4,>>WO-*#7V
printf5FPn@ew file pointer position ! DluF,ftell5fp77V
fclose5fp7V
getch57V
L
Binary files :
get, :
It gets an integer from a file stream.
Synta( : int getw5QI+> Bstream7V
put,:
It outputs an integer on a file stream.
Synta( : int putw5int w, QI+> Bstream7V
1rogram : file"2'c
NincludeEstdio.hG
NincludeEconio.hG
void main57
K
QI+> BfpV
int nV
clrscr57V
fpAfopen5Fa.datF,Fa.bF7V CC append in binary format
printf5F>nter data 54 to stop7 ! PnPnF7V
scanf5FDdF,[n7V
while5n]A47
K
putw5n,fp7V
scanf5FDdF,[n7V
L
fseek5fp,4,47V CCrewind5fp7V
printf5FPnPn#eading data from file ! PnPnF7V
nAgetw5fp7V
while5]feof5fp77
K
printf5FDdPnF,n7V
nAgetw5fp7V
L
fclose5fp7V
getch57V
L
f,rite :
It appends a specified number of equal si8ed data blocks to an
output file.
Synta( :
si8eOt fwrite5void Bptr , si8eOt si8e , si8eOt n , QI+> Bstream7V
fread :
It reads specified number of equal si8ed data blocks from an
input file.
Synta( :
si8eOt fread5void Bptr , si8eOt si8e , si8eOt n , QI+> Bstream7V
argumnet description
ptr pointer to a block into which the data is readCwrite
si8e length of each item readCwrite in bytes
n number of items readCwrite
stream points to output C input stream
1rogram : file")'c
NincludeEstdio.hG
NincludeEconio.hG
struct student
K
int snoV
char snameI24J,courseI:4JV
float feeV
LV
void main57
K
struct student sV
QI+> BfpV
clrscr57V
fpAfopen5Fstudent.datF,FwbF7V
printf5F>nter student number ! F7V
scanf5FDdF,[s.sno7V
printf5F>nter student name ! F7V
fflush5stdin7V
gets5s.sname7V
printf5F>nter course ! F7V
gets5s.course7V
printf5F>nter fee ! F7V
scanf5FDfF,[s.fee7V
fwrite5[s,si8eof5s7,:,fp7V
fclose5fp7V
printf5FPnPn#ecord save sucessfullyF7V
getch57V
L
1rogram : file"*'c
NincludeEstdio.hG
NincludeEconio.hG
struct student
K
int snoV
char snameI24J,courseI:4JV
float feeV
LV
void main57
K
struct student sV
QI+> BfpV
clrscr57V
fpAfopen5Fstudent.datF,FrbF7V
fread5[s,si8eof5s7,:,fp7V
printf5F#eading data from filePnPnF7V
printf5Ftudent number ! DdF,s.sno7V
printf5FPntudent name ! DsF,s.sname7V
printf5FPn-ourse ! DsF,s.course7V
printf5FPnQee ! D.2fF,s.fee7V
fclose5fp7V
getch57V
L
1rogram : file"'c
NincludeEstdio.hG
NincludeEconio.hG
struct student
K
int snoV
char snameI24J,courseI2JV
float feeV
LV
void main57
K
QI+> BfpV
char chV
struct student sV
clrscr57V
fpAfopen5Fstudent.datF,Fa.bF7V
do
K
printf5F>nter student number ! F7V
scanf5FDdF,[s.sno7V
printf5F>nter student name ! F7V
fflush5stdin7V
gets5s.sname7V
printf5F>nter course ! F7V
fflush5stdin7V
gets5s.course7V
printf5F>nter fee ! F7V
scanf5FDfF,[s.fee7V
fwrite5[s,si8eof5s7,:,fp7V
printf5FPn#ecord saved successfullyF7V
printf5FPnPn9o you want to add another record 5yCn7 ! F7V
fflush5stdin7V
scanf5FDcF,[ch7V
L
while5ch]Aana7V
fseek5fp,4,47V
fread5[s,si8eof5s7,:,fp7V
while5]feof5fp77
K
clrscr57V
printf5FPntudent number ! DdF,s.sno7V
printf5FPntudent name ! DsF,s.sname7V
printf5FPncourse ! DsF,s.course7V
printf5FPnQee ! D.2fF,s.fee7V
printf5FPnPn,ress any kee to contune.........F7V
getch57V
fread5[s,si8eof5s7,:,fp7V
L
fclose5fp7V
getch57V
L
enum$typedef
Command line arguments :
These are the parameters supplied to aprogram,
when the program is invoked. This parameter may
represent a file name that the program should
process. -ommand line arguments are typed by the
user. The first argument is always the file name.
%e know that, every - program should have one
main function and it can take arguments like other
functions. If we want to work with command line
arguments, the main function can take 2 arguments
called argc and argv and the information contained in
the command line is processed onto the program
through these command line arguments.
The variable argc is an argument counter that
counts @o of arguments on the command line. The
argument argv is an argument vector that represents
an array of character pointers that points to the
command line arguments. The si8e of this array is
equal to the value of argc.
1rogram : cla"'c
NincludeEstdio.hG
NincludeEconio.hG
void main5int argc,char BargvIJ7
K
int iV
clrscr57V
printf5F@umber of argument ! DdF,argc7V
for5iA4ViEargcVi..7
K
printf5FPnargumentIDdJ ! DsF,i,argvIiJ7V
L
getch57V
L
1rogram : cla2'c
NincludeEstdio.hG
NincludeEconio.hG
void main5int argc,char BargvIJ7
K
QI+> BfpV
char chV
clrscr57V
if5argc]A27
K
printf5FInvalid argumentsF7V
exit547V
L
fpAfopen5argvI:J,FwF7V
chAgetchar57V
while5ch]A>"Q7
K
putc5ch,fp7V
chAgetchar57V
L
fclose5fp7V
printf5FQile createdF7V
getch57V
L
1rogram : cla)'c
NincludeEstdio.hG
NincludeEconio.hG
void main5int argc,char BargvIJ7
K
QI+> BfpV
char chV
clrscr57V
if5argc]A27
K
printf5FInvalid argumentsF7V
exit547V
L
fpAfopen5argvI:J,FrF7V
if5fpAA@*++7
K
printf5FQile not foundF7V
exit547V
L
chAgetc5fp7V
while5ch]A>"Q7
K
printf5FDcF,ch7V
chAgetc5fp7V
L
fclose5fp7V
getch57V
L
1rogram : cla*'c
NincludeEstdio.hG
NincludeEconio.hG
void main5int argc,char BargvIJ7
K
QI+> Bfp:,Bfp2V
char chV
if5argc]A;7
K
printf5FInvalid argumentsF7V
exit547V
L
fp:Afopen5argvI:J,FrF7V
if5fp:AA@*++7
K
printf5FQile not foundF7V
exit547V
L
fp2Afopen5argvI2J,FrF7V
if5fp2]A@*++7
K
printf5FQile already exists ! DsF,argvI2J7V
printf5FPn9o you want to override 5yCn7 ! F7V
scanf5FDcF,[ch7V
if5chAAana^^chAAa@a7
K
printf5F*nable to copy a fileF7V
fcloseall57V
exit547V
L
fclose5fp27V
fp2Afopen5argvI2J,FwF7V
chAgetc5fp:7V
while5ch]A>"Q7
K
putc5ch,fp27V
chAgetc5fp:7V
L
fcloseall57V
L
printf5FQile copied successfullyF7V
L
1rogram : design'h
NincludeEstdio.hG
NincludeEconio.hG
void hline5int r,int sc,int ec7
K
int iV
for5iAscViEAecVi..7
K
gotoxy5i,r7V
printf5FDcF,:&37V
L
L
void vline5int c,int sr,int er7
K
int iV
for5iAsrViEAerVi..7
K
gotoxy5c,i7V
printf5FDcF,:)&7V
L
L
void box5int sc,int sr,int ec,int er7
K
gotoxy5sc,sr7V
printf5FDcF,2:'7V
gotoxy5ec,sr7V
printf5FDcF,:&:7V
gotoxy5sc,er7V
printf5FDcF,:&27V
gotoxy5ec,er7V
printf5FDcF,2:)7V
hline5sr,sc.:,ec0:7V
hline5er,sc.:,ec0:7V
vline5sc,sr.:,er0:7V
vline5ec,sr.:,er0:7V
L
1rogram : shuffle'c
NincludeEprocess.hG
NincludeEstdlib.hG
NincludeFdesign.hF
void main57
K
void generate5int IJI<J7V
int aI<JI<JV
int i,H,r,c,countA4V
char chV
Osetcursortype5O@"-*#"#7V
generate5a7V
while5:7
K
clrscr57V
gotoxy5;2,27V
printf5F1*QQ+> =A$>F7V
box522,;,2',:&7V
hline5),2;,2)7V
hline5::,2;,2)7V
hline5:2,2;,2)7V
vline5;4,<,:'7V
vline5<4,<,:'7V
vline524,<,:'7V
gotoxy5;2,2:7V
printf5F@umber of key strokes ! DdF,count7V
gotoxy5;2,2;7V
printf5F>sc to exitF7V
gotoxy5;2,227V
printf5FAlt. to shuffleF7V
for5iA4ViE<Vi..7
K
for5HA4VHE<VH..7
K
gotoxy522.HB:4,2.iB<7V
if5aIiJIHJ]A47
printf5FDdF,aIiJIHJ7V
else
K
rAiV
cAHV
L
L
L
chAgetch57V
if5chAA47
chAgetch57V
count..V
if5countG2447
K
clrscr57V
textcolor5:2.:2'7V
gotoxy5;2,:27V
printf5F=A$> "M>#F7V
getch57V
exit547V
L
switch5ch7
K
case '4!
if5rG47
K
aIrJIcJAaIr0:JIcJV
aIr0:JIcJA4V
L
breakV
case )2!
if5rE;7
K
aIrJIcJAaIr.:JIcJV
aIr.:JIcJA4V
L
breakV
case ))!
if5cG47
K
aIrJIcJAaIrJIc0:JV
aIrJIc0:JA4V
L
breakV
case )2!
if5rE;7
K
aIrJIcJAaIrJIc.:JV
aIrJIc.:JA4V
L
breakV
case ;:!
generate5a7V
countA4V
breakV
case 2)!
exit547V
L
if5aI4JI4JAA: [[ aI4JI:JAA2 [[ aI4JI2JAA; [[
aI4JI;JAA< [[
aI:JI4JAA2 [[ aI:JI:JAA3 [[ aI:JI2JAA) [[
aI:JI;JAA' [[
aI2JI4JAA& [[ aI2JI:JAA:4 [[ aI2JI2JAA::
[[ aI2JI;JAA:2 [[
aI;JI4JAA:; [[ aI;JI:JAA:< [[ aI;JI2JAA:2
[[ aI;JI;JAA47
K
clrscr57V
textcolor5:2.:2'7V
gotoxy5;2,:27V
cprintf5F=A$> *-->F7V
gotoxy5;2,:27V
printf5F@umber of key strokes ! DdF,count7V
getch57V
exit547V
L
L
L
void generate5int aIJI<J7
K
int xI:3J,n,idA4,i,HV
char ckV
randomi8e57V
while5idE:37
K
nArandom5:37V
ckA4V
for5iA4ViEidVi..7
K
if5xIiJAAn7
K
ckA:V
breakV
L
L
if5ckAA47
K
xIidJAnV
id..V
L
L
idA4V
for5iA4ViE<Vi..7
K
for5HA4VHE<VH..7
K
aIiJIHJAxIidJV
id..V
L
L
L
Con&ersion functions:
atoi : It is a macro that converts string to int.
Synta( : int atoi5const

You might also like