UNIT-1_PSTC
UNIT-1_PSTC
C PROGIRAMMINGI
Tntoduet bn
BcPL-Båsic combined Progvamming Loguar
Lby Martin Richards at cambriclge
in 197 AT T 8el
Der Ritchie
with
c is amiddle leve larguage ha
level ard low level features
machine
is a structured, Kigh leve,
e
fndeperdent language
BCPL
B
Troditional c
k& RC
Moola
ANSTC
AN&IISo c
eoor
ca
Xstucture o rm
a Collection C one r mom
pr
AunctionS
Every
Aunetion iscollectíon -stubRmeni
a speciPic tusk o
performs
bocumentation 6ection.
Link ection.
Definition section
Grloal declaration section.
main ) function section
Declaration Part
Executable part
olut
subpam section
Function 1
Aunction R
CUse defined unction)
Function O
sbaesieloiu
ebroue
0 Domentation ectonCoptional 3
The documentotion section consists of-a set o
Commert lines uin the name of the
program
e author ar other details, which the
programmer would 1ike to use later.
incude directiue
include the contents of
| T t in sstruets Compiler to
the ile stdioh" Cstardard input output heoder
enclosed within angular bracket
pefinition etionCoptionad
defines all mbolic
The definitfon becbion
constonts
define P 3-1413
# define SIZE loo o porr
declaration variabless
Punctions
Smoinc tunction SectionrCessential)
DOeclarotion patt
Eecutabe port
These 2
parts must appear blo openirg
clasing braceS
mainc) frunctiorn.
PintfCWelcome tu c Prmramming\n
Tetun 0
Qutput
welcome to c programming o
CTOkens uhluors
Building blockstor wvitig/creating a Cprogro
classiicotion
0 Tdentifiers
Vaniables
nol iool ormont
leeyuoords
Data Tpes
constants
Operators
OTdentiers
elermert is called
Name given to proromrning
Tdentifer oitots
NamePe Programming element
Rules
variables
name t memory location
ufuin
I t is a memory location used to store smne
data ualues
values
o
may change thus called varnables
a
O int a = lo
Datatype Vanable
Tnt a, b ci
uoiabe Declavottionspesthic pasticular pryram
Keooreds
These are also called as Reserved w O r d s r
Stucdt
Atto double int
Suwitch
break else Long-
ypedef
Case enum qister
do staic bile
otatHee
which we need to stDre
oP d a t a
The type
in memory enown as
DattatHpes
There are 3 types Datatupes
ypes
NOte
lo
Bytes Fec
void
rothin
IO 1o lo 2S2
2g6-b
98-0
2 u - D ccbovo0o
-D
26 1ooloo
Lolo t Bit9 -
Geity
O01o o oooO0000
25,24+i+°
& 0+2to = l0 32 t0+o+4 tot0=3
A*1inthort)
te
,76643 1 b
plololplololo ospoolep
Min
7 3 2512,1,
Mar
Datau
O ue Uahue
39-8
unsigned
Dololoolololo oololo|o|o|op
-ue
O +ue
- 1 to-32768 Coinimum).
o to 32468 (Maximum)
o to 655
0 td 2
For Unsigoedo t 2
for igred to+o-I
-3246& 32468
-2 t +2-
short
signed sa18 to 2
>onsigned o t 6s535].
signesd-31/64 tb2ext
nt
unesgred-3642Htes Let
Primitive
Data type
- ged
orsred 4eytes3
unsgredP
yte
Signeo -128 to Hy
har unshred-bto -
Char
Tnput character5 Machine larguage
Numeri cal o& 1s
A-65 a-9 O-48 Speeial chr
B-66 b-9 8& 1-49 -35
i space-32
z90 z-122 -5
150
26 26
Exponent 8 bit
Montisa 1bit
eyponent 3 bits
ualue 2 3 bits
a: h D
Precision decimal digits
Range
E a to +3 4E t38
+38
1 x lo to 3:4 x lo
ouble r
8 y b e s - 64 b i t s
Mrtisa .2
bit
Exponment t bits
value a bits
Precsion15 decinal digits
to 19E130}
Rarge 2-3e-3o
2.3xl0D t xloE
Rong double
1 2 ytes 4 E
Montsa
15 bts
o
eponent
alue-8o bts
Rrecision 21 dechnal digits
pes
size
Data tyPe n bytes)Speciier Rar
-l28 to 12
0 chor
2 to 124
ed char
O to 255
Unsgred char /.c
-22468 to 3264
Int
i g n e d tnt d -31968 to 32464
s o r t nt -31168 to 32469
onint 7. Sd
21448 1644
Sared long int Rd -2) MEI648
ctouble 8 -e to
E130
o r double LP
1-1e+432.
Void
ThiS type holds no valu
t s used t
t o peci te retun tye o Punction
ChWhen e function retwns o value
t o speciky the arametersthe Punction
Cwhen te furction accepts ro apumerts fon ale
t o create gererie pointers
5 constartsor) Literalb
be stured in memoy
iS a value that con
Tt
change durig elecition progam.
Canrot be
o
are used tu define fixed values
constants
constants
0nteger constant+
Ex 1 9 234,999.
Tn Fractional om
shald be atleast 4 digib
cawd be We 0r -Ue,
is mus
decinmal point
NoCommas blankS
or blanks
Exponerttial torm
Before e MantiSSa
quotes
T t con be sile alphubet,sin db
Sinde pecial mtol enclosed
sine
i r g constant
I t is Benuence o characterS ercloeed
enclosed y
douuble otes
he lo".
Conantsin rie
to be
Literals The ixed Value that s goin
ntiali zed o assigned drectty in tvariable
aPter declaration alled Uteral
like
operates on operards and does operations
rotherrat ical,relatioral, Aogical ete
OperatDrS
onay 9peratorsS
A n Operator which Operates on sngle Opernnd
unary minus
Tnurement operator
Decrement Operator.
ynary iuS
t retwns the operand mnutiplied hy-
t charges the sign
i n t . a,b=lob
a =-(b)
Reswta=-10
Increment operator
operator aPter
ater
Pyou put the
Post inremen
retuuns the onginal
the 9perard Cpost Pi2,
Ualue d operord CBePore incrementJ
intront
Pre inerement I yo pat operat0r
Operand Cprefiz), it returns ne value o Operand
Cincrementcd]
#include <stdio.h>
int maínC
Outpub
int x -5;
x=
PintPCx dn xtt);
PrintPCx=.d\n", +*x)
retuun o3
fon
Decrement Operator
It decreases valuue ooperand
#include <stdio.h>
int mainc Output
int x S
x
X-5
Print PCx= dn' x
Print fCx =
/.din', ) ;
1 retun 0
Binay 9peratYSCoperate on tuoo operards at ati
Arithwnetic oerators
Relational operators
Logical opevators
Bitwise operator6
A6signment operator6
Arithmetic operators
opeations
These are eed to pertormmathematkal
number
PPplied to ary ntegers, loating9-Foint
choracters
5 aithmetic opeatvrs +,-* 7%
7.can be applied to inteaer operands only
onHoat or double ualues
cannot be used
and
ab rasult caBb. 4
Mutip'
a/b result=a/b 3
oivide
Moolulo a/b result =a.b o
inelude stdio.h>
output
9+31
int maunc 9-3 6
3-22
int a , b=3;
-3
Pirttc"/ d t % d= /.d\n"), a,b, atb); %3c0
r e a t e r than 1 gives o
Less than br
eqtatu o0 =loo
=
gives 1
Greatev than or sD =1ogives o
egual to
include cstdio-hs
nt mainc )
nien
output
13 = O
2 - 3 =0
7 3 =1
9-3o
!-3e1
Logical operatorss
used to conbine 2 or more relationa ezpressicong
3logical operators
ANDCR)
Binary
OR CI1)
NOTC! vnary
returns either o Cfalse) or ue).
AND NOT
OR
A
BALB AllB
0 0
int maint )
2R9>5 I lo>IS
23 C>5);
PrintP c 21e. din =7dn 2s= dn" 2d, 2a z)
return o,
Put
21-0
ot
23=0
Bwie OperetoY
Bitwie operators opernte on Brt
k e s e Operantor.s irelude
bitwie An CL)
bitwie oR C)
bituie X-oC
bitwse OT C) shift left Cee)
bitwie shifts
bitwise
shittt shi?t ight (>)
Bitwise ANDC&)
tnt a=6, b=9, c
C=a 8b
Print+C zd" c); prints o
olo C 63
doo C13
0000 CoJ
Bitwise oR C )
110 L61
Bituise xORC
xOR
eL int a=u,
b=R,c) l/ prints B AA8
Cabo O
print C7d, d;
o0 C
Right shifE
euals to division
eiaht shi't
Crample to Bitwise OperauDYS
incude <stdio.ha
int mainC )
Dint XR,
y=3, Sumj
SumEtH 5
i n t a=b=c =lo
Ca-Cb-Cc=l00))
C A r ) c=cA
Multipy AND
Divide AND c/=A or) c=clA
PintPc"operator
is= and e=d\
Prirt Pc operator t e and ce p d \n"e)
ab
Mat Ca>) a : b
Tue False
even /odd
/2 pvintPC"/h d is odn): prírtC4 is een')
True Faleto
, bc CNested)
MaL-a)?case)?ai c: Cbac)b: T F
Tue Falee
Special OperatorS
derefererce/Indirect operator
Address memory
Period operatD CFor member acceSSin opelato
*Epresonsi
Oimple
Contains one operator & 2 operands
ty 3t5 a*
Conplexr kdok
Contaúns 2 or more opeatvrs G operords
Implict
pertormed by the compiler witott programm
me
interventtion.
A I Lower data types converted to hext high
data bypes
Aules
Rong doble
louble
Hoot
unsigned org int
lJong intE
pnsigned int
Louwer
Leuel
Int
eharhovt
E char c
int
Ptoat
olouble d, T
r=(¢*3) +Gli)-Ctd)
Hoat dable
int ploat olouble
Hoat dauble
Het
double
Ldoube
plit CType casti]
performed b pogammar for his requirement
a i n t C5)
int C/a-/Gt) 42
2/3
a
a -(double) totad/n; Ploat
a t )Cath) int Lurclo
LePt to Right
Letto Right
<>>
LePt t Raht
Left to Right
LePt to Rihe
2
Left to ight
Lelt to Rht
Lto R
L to
R to L
L t oR2