You are on page 1of 41

lmporLance of C

W'C' ls a robusL language LhaL has a numerous seL


of bullLln funcLlons and operaLors whlch can be
used Lo wrlLe any complex program
Wrograms wrlLLen ln 'C' are efflclenL and fasL
W'C' ls hlghly porLable
Wuser deflned funcLlons can be creaLed and used
ln accordance Lo our need
LrucLure of a Crogram
Documentat|on Sect|on
Llnk ecLlonlnsLrucLlons Lo Lhe compller Lo llnk funcLlons from Lhe sysLem llbrary
(header flles)
ueflnlLlon ecLlon symbollc consLanLs
Clobal ueclaraLlon varlable declaraLlon funcLlon declaraLlon
Maln() luncLlon ecLlon

ueclaraLlon arL
LxecuLable arL

ubprogram secLlon
Iunct|on 1
luncLlon 2

luncLlon n
user ueflned luncLlons
maln() luncLlon name
LarL of rogram

rogram LaLemenLs

Lnd of rogram
Iormat of s|mp|e 'C' programs
Lxample 1
#lncludesLdloh Landard llbrary funcLlon
maln()

rlnLf(l see l remember")

Maln() A speclal funcLlon used by Lhe 'C' program Lo


lndlcaLe Lhe sLarL of Lhe program Lo Lhe compuLer
rlnLf lL ls a predeflned sLandard 'C' funcLlon for
prlnLlng Lhe ouLpuL
Lvery 'C' sLaLemenL should end wlLh a semlcolon()
utput I see I remember
rlnLf(l see n l remember")
utput I see
I remember
rlnLf(n l n see nl n remember")
utput
I
See
I
remember
Characters and tokens
W CharacLers are Lhe baslc bulldlng blocks ln C program
equlvalenL Lo 'leLLers' ln Lngllsh language
W lncludes every prlnLable characLer on Lhe sLandard Lngllsh
language keyboard excepL ` $ and [
W CharacLer caLegorles
1 LeLLers a z and A Z
2 ulglLs 0 9
3 peclal characLers ? / ( ) * eLc
4 WhlLe spaces (blank)
C Lokens smallesL lndlvldual unlLs used ln Lhe program
C 1okens
keywords ldenLlflers Lrlngs
ConsLanLs peclal ymbols
CperaLors
keywords and ldenLlflers
W keywords serve as baslc bulldlng blocks for
program sLaLemenLs
W hould be Lyped ln lowercase
auto double int struct
break
case
char
const
continue
default
do
else
enum
extern
float
for
goto
if
long
register
return
short
signed
sizeof
static
switch
typedef
union
unsigned
void
volatile
while
Ident|f|ers
W Words used Lo represenL cerLaln program enLlLles (program
varlables funcLlon names eLc)
W 1hese are user deflned names unllke Lhe keywords
W amp|e
W lnL my_name
W my_name ls an ldenLlfler used as a program varlable
W vold CalculaLe1oLal(lnL value)
W CalculaLe1oLal ls an ldenLlfler used as a funcLlon name
u|es for Construct|ng Ident|f|ers
1 Ident|f|ers can cons|st of cap|ta| |etters 4 to 2 the |owercase o to t
the d|g|t 0 to 9 and underscore character _
2 1he f|rst character must be a |etter or an underscore
3 1here |s v|rtua||y no |ength ||m|tat|on nowever |n many
|mp|ementat|ons of the C |anguage the comp||ers recogn|ze on|y the
f|rst 32 characters as s|gn|f|cant
4 1here can be no b|anks
S keywords or eserved words cannot be used as |dent|f|ers
6 Ident|f|ers are case sens|t|ve 1herefore 1ox and tox both d|fferent
Constants
W LnLlLles LhaL appear ln Lhe program code as f|ed va|ues
W A consLanL cannoL be modlfled durlng program execuLlon buL a
varlable can be modlfled
W Lxample consL a4 forever
lnL b4 keeps changlng
W 4 Lypes of consLanLs
lnLeger consLanLs
8eal consLanLs
CharacLer consLanLs
Lrlng LlLeral
Integer Constant
oslLlve or negaLlve whole numbers wlLh no fracLlonal parL
CpLlonal + or slgn before Lhe dlglL
lL can be declmal (base 10) ocLal (base 8) or hexadeclmal (base 16)
Pexadeclmal ls very useful when deallng wlLh blnary numbers
amp|e
consL lnL MAx_nuM 10
consL lnL ocLal_number 037
consL lnL Pexadeclmal_number 0xf87
13 730
20000 spaces commas nondlglL characLers are noL allowed
$1000
lnLeger ConsLanLs
W 1he largesL value LhaL can be sLored ls
machlne dependenL(2
13
132767)
W Lo sLore larger lnLeger consLanLs append
quallflers such as uL and uL Lo Lhe consLanL
Lxample 32768u (unslgned lnLeger)
98373026uL (unslgned long lnLeger)
9876343L (long lnLeger)
ea| Constants (f|oat|ngpo|nt)
oslLlve or negaLlve declmal numbers wlLh an |nteger parL(opLlonal) a
dec|ma| po|nt and a fract|ona| parL (opLlonal)
amp|e 008323 +43698
792 lnvalld
lL can be wrlLLen ln exponenLlal noLaLlon
mant|ssa e eponent
02033L+2 (02033 x 10
2
)
L or e sLand for eponent"
Lx ConsL floaL lnLeresL
8eal or lnLeger only lnLeger
CharacLer consLanLs
W A slngle characLer enclosed wlLh ln slngle
quoLes
W Lxample 'A' 'b' '4' '+' '['
'23' lnvalld
A" lnvalld
A characLer ls lnLernally represenLed by ACll
value(0 Lo 233)
no negaLlve ACll values
CharacLer consLanLs
W CperaLlon on a characLer ls noLhlng buL
operaLlon on lLs correspondlng ACll value
Lxample 'A' '8' (63 66) x
'A' + '8' 63 + 66 131
Lrlng consLanL
W A collecLlon of characLers enclosed wlLhln
double quoLes
W A sLrlng ls alphanumerlc
W lL may conLaln alphabeLs speclal characLers and
dlglLs
Lxample 8ama 8ao" 123" +$"
|nteger
'' character
"# str|ng
'1066' x "1066# str|ng
Lrlng consLanL
W Lvery sLrlng ls LermlnaLed by a null characLer
(0 backslash zero)
W Lxample hyd"
W ACll value of 0 ls 0
x
'A' A"
h y d 0
h y d 0 s e
8ackslash characLer consLanLs
(escape sequences)
used ln ouLpuL funcLlons
C-S1A-1 MA-I-
AlerL (8ell) a
8ackspace b
lorm leed f
new llne n
Carrlage 8eLurn r
PorlzonLal 1ab L
verLlcal 1ab v
8ackslash
lngle CuoLe
uouble CuoLe
CuesLlon Mark ?
null 0
8ackslash characLer consLanLs
W Lxample
W prlnLf(hydn")
W prlnLf(secn")
8eplace 'n' wlLh all Lhe backslash consLanLs
glven ln Lhe Lable and observe Lhe changes
Iar|ab|e -ames
W A varlable ls a daLa name LhaL may be used
Lo sLore Lhe daLa value
W A varlable may Lake dlfferenL values aL
dlfferenL Llmes durlng execuLlon
W ln 'C' all Lhe varlables musL be declared
before Lhey are used usually aL Lhe
beglnnlng of Lhe funcLlon before any
execuLable sLaLemenLs
W Maxlmum number of characLers ls '8' for a
varlable
W varlable names may conslsL of leLLers dlglLs
and Lhe underscore(_) characLer
varlable declaraLlon
W Lg lnL number_9
floaL amounL
number_9 and amount are var|ab|es
lnL hyderabad
lnL hyderabadl x
daLaLype v1v2vn
userdeflned Lype declaraLlon
W Allows users Lo deflne an ldenLlfler LhaL would
represenL an exlsLlng daLa Lype
W Lx
Lypedef lnL marks
(1hls can be laLer used Lo declare varlables)
marks maLhs soclal
Lypedef Lype ldenLlfler
Asslgnlng values Lo varlables
W Lx
lnlLlal_value100
8alance13340
yes'x'
varlable _name consLanL
W MulLlple asslgnmenLs
Lx
lnlLlal_value100 8alance13340 yes'x'
W ueclaraLlon + asslgnmenL
Lx
lnL lnlLlal_value100 (lnlLlallzaLlon)
floaL 8alance13340
char yes'x'
Lx
qs0 lnlLlallzaLlon of more Lhan one varlable
xyzMAx
uaLa 1ypes
A daLa Lype ls deflned as a Lype of value LhaL can
be sLored ln a varlable
1 rlmary daLa Lypes
2 userdeflned daLa Lypes
3 uerlved daLa Lypes
4 LmpLy daLa seL
econdary
daLa Lypes
uaLa 1ypes
Data Type Description TypicaI Memory
int integer quantity 2 bytes
long int 4 bytes
unsigned int 2 bytes
float floating point number 4 bytes
double double -precision 8 bytes
floating point number
char single character 1 byte
uaLa 1ypes
1lnL
W Maxlmum lnLeger 2
13
1 32767
W Mlnlmum lnLeger 2
13
32768
W 1o represenL a greaLer value use long
D1S D14 D0
1 0 1 0 0 1 1 1 1 1 0 0 0 0 1 1
lCn MACnl1uuL
uaLa 1ypes
2 Long (48) lL's an exLenslon Lo lnL
W Maxlmum long value 2
31
1
W Mlnlmum long value 2
31
3 unslgned lnL (28) no slgn blL le all are magnlLude
blLs
W Maxlmum unslgned lnL 2
16
1 63333
W Mlnlmum lnLeger 0
uaLa 1ypes
4 lloaL (48)
ueclmal polnL
(lLs noL sLored ln memory)
8ange?
D31 D30D6 DSD0
lgn blL lnLeger parL(23 blLs) fracLlonal parL(6 blLs)
uaLa 1ypes
3 uouble (88) lLs an exLenslon Lo floaL
Also known as long floaL
8ange 2
63
1 Lo 2
63
llrsL 32 blLs lnLeger parL
12 blLs fracLlonal parL
Lxample 23 lnL
23L long
1026f floaL
1026Lf double
6 Char (18) no slgn blL le all are magnlLude blLs
8ange 0 Lo 233 (Ascll values)
rlmary uaLa 1ypes
ueflnlng symbollc consLanLs
W ConsLanLs whlch may appear repeaLedly ln a
number of places ln Lhe program
Lx pl3142
Lx
#deflne pl 3142
#deflne symbollc_name value of consLanL
8ules for symbollc consLanLs
1 ame as varlable names
2 no blank space beLween # and deflne
3 #deflne sLaLemenLs musL noL end wlLh Lhe
semlcolon
4 AsslgnmenL operaLor should noL be used()
3 #deflne sLaLemenLs may appear anywhere ln
Lhe program before lL ls referenced
Lxamples of symbollc consLanLs
1 # deflne x 22
2 #deflne x 22
lllegal
3 #deflne x22
4#deflne x 22 y 33
3 #ueflne x 22
ldenLlfy ynLax errors ln Lhe followlng
program
W lncludemaLhh
W Maln
W (
W lLCA1 x
W x2
W ?x
W rlnL(xy)
CperaLors
CperaLorsppL

You might also like