0% found this document useful (0 votes)
11 views25 pages

UNIT-1_PSTC

The document provides an overview of the C programming language, including its structure, components, and data types. It discusses the organization of a C program, including sections for documentation, linking, declarations, and the main function. Additionally, it covers tokens, identifiers, keywords, and primitive and derived data types in C.

Uploaded by

akhilakrosuri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views25 pages

UNIT-1_PSTC

The document provides an overview of the C programming language, including its structure, components, and data types. It discusses the organization of a C program, including sections for documentation, linking, declarations, and the main function. Additionally, it covers tokens, identifiers, keywords, and primitive and derived data types in C.

Uploaded by

akhilakrosuri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

FOPK

C PROGIRAMMINGI

Tntoduet bn
BcPL-Båsic combined Progvamming Loguar
Lby Martin Richards at cambriclge

Pirst initil Prom BcPL


Lanuage
ken ken ThomSon in110

nital Pron BcPL


C Langa Second

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

Tocorory oPe laguage


ALGIOL

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.

Tu tpes o Comment lines


ingle line coment nes
elck Comment lines / *
T h s eection ioptioral
Link Bection(essertial)
This section prouides instructlons t he compiler
to k functions Prom the stem libray.
Ex include <stdioh>
#inehude <.sting h
include < conio h>
* include <araphie.h
include < std libh> tosolano
#inelade < mal lut h
inehude < process-h
#inelude <
ctype h ldehaos3

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

# dePine PRINT Print Ci).


Global declaration ection Loptionad)
There areSome variables that are used in

more t a n one tundtion. Such are called Grlobal

declaration variabless

are declaredgloal declaration


in Section
These
tthat i outside a l l t e funetions.

ThiSSection also declares all the user-defined

Punctions
Smoinc tunction SectionrCessential)

muSt haUe one min


euery c
og'mhas
unetion section

Th eection etainS 2 puts lb

DOeclarotion patt
Eecutabe port

Declaration port J t declaress all the oriobes


used in the executable pa
executable port There is atteas tatement
n the eiecutable part

These 2
parts must appear blo openirg
clasing braceS

The pogram eiecution bepins at the openin


brace & ends at
closin brace
The closi brace othe nmain tunction is the
Iical erd te programn.
All statements in the p declaration executobi

Rart erd with semicolon. Jer


sk
bproram ection as
The subprqram section Contains all the user
-deined unctions that are Called n the

mainc) frunctiorn.

defined unctions are


ser-
generally placed
tmnediately after the mainc) tuncion,
othough they may ppear in any Drder.
xample
include <std io h>
int ainc )

PintfCWelcome tu c Prmramming\n

Tetun 0

Qutput
welcome to c programming o

CTOkens uhluors
Building blockstor wvitig/creating a Cprogro

are Caled c-Tokens


can uwrite y Token.
Each instrudtion/statement
L nt1=lo

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

vser detined words vanables, aa


irs painters. e t
combination doPper/
louver/digtts/understoe
CSe
f l o a t a=20-2
R-cse
cse X
IdentHier
R@cse *

shouldnt allow spectal character

Rules

Ostarts with alphabets only


Cehouldnt starts
starts witth
digit
spaceS not alloued naarome

shouldnot use special ymbols except undersom


match wth Reywods
ahuudnot

Lengt d Tdentitier< 31 characters


6) names must be mean essat A -65
a-94
case senstive, A#a,xf* O-4&
Tdertifier should beunigue

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

Pre deired wovels


Hred meanirgs and these
All keywords haue
be changed
meanin9% Cannot
There are 3 2 keyworcds in C ramming

keyorcs eerve aS bhic building blocks Por a


progreum stutement

must be wrttten in louwercase nl


All KeyuocAS

Stucdt
Atto double int
Suwitch
break else Long-
ypedef
Case enum qister

char extern retunn- uním


Short unsigned
Const Hoat
void
Continue for ined
Volatile
defaut size o

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

O Pimitiue pata eeschar int, PHoatb,double, void


allouas to store: onl single Value
aras,'pointers ett
Deried Data Pe stn
T t alloue to store mutiple talues oame ype

eer Data e e struoture, union typesdelerumet


combination H t h Primitive & denved data

ypes
NOte

Pimitive Datn bypeS are BaSic bata types

Derved Data types are collection of Basi


t a types They are dened by Primitae o

Erstrin9 icollection acharncterss


Primitive Deriued ata ype are
deined by
the pogram deueloperS

s e r atn types are defined by USer.

Rrimitie pata pes


short 2 PHtes-16 eits
int
nt 2
Bytes/4 eytes
2 8ts) CGBits9
> Long 4
Pybes
char-1 eyte
> Ploat 4
Bytes hok
Ploat
dloube8Bytes
Long double -

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

oo00000o o000oo00 CO)10


(65535)\
result 1s d
65536 Ooo0000o o0oo000
8b stese
values
Signed -ue, O, +tue

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

Tota= 26 26+ lo + I5d 212 characters

AScTT Ameican standard code Pov Intormoctíon


J Tnstite
developed unicode system

To store these a12 charaoters, 8 bits are


veguired. e, 1 Byte e s
*floa
toat
4Bytes C32 Bits)
Motisa - ue oT e

Por representation allocated.

4 bit C 1 -ve & o+Ve)

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

Usigned int o to 6G3S

s o r t nt -31168 to 32469

gecd short int 2 O -32368 to 326

Unsiared shot int O O to 65535

onint 7. Sd
21448 1644
Sared long int Rd -2) MEI648

ursgred lorg int o to 29L164295

loat 4 3:4E38 to 3-4Et

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

Int Floating oint


eharacter
stg
Constant Constanb
Consbart
constant

0nteger constant+

muSt have atleast n e digit

shouud not hove a decinmal poit


tue ve-
c o u n be ov

Ex 1 9 234,999.

Dlootirg oint constan


t 1s a decimal number:
ether decimal point or exponent
I t ontains

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

Consists tuw parts<


aPter,
e eporent
Mantissa exponental ceparated E
Manbissa can haue a tue or-ve n-Ldefautb t

RE-10, O 5eR,tlBe+3, -5-6E-R


character conetant
These are single character erclosed in i n

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

decimal C o n s t ínt 2 e =loo


octal c o n s t int X =03
int
hexadecima Const i t Y=oxqePe

Hot const Hoat PT=3-l45s


Hont
ble const PHoat H 3-145

char char C=A CNO need of const


keyuoord)
stin9 ehor name CoJ=Raukth

P arammr ue need tw use


a
good
e
Consbants in Capital letters

to be
Literals The ixed Value that s goin
ntiali zed o assigned drectty in tvariable
aPter declaration alled Uteral

E int xelo Literal TKe value moy char


Const i t x e t o j Constunt talue maynot dorg
HOperatos
e
defired a s symbol that
An operator a

like
operates on operards and does operations
rotherrat ical,relatioral, Aogical ete

OperatDrS

Unary Binay Terray special


OperatDrS OperatorS Operators OperatDs

onay 9peratorsS
A n Operator which Operates on sngle Opernnd

called Uary OperatoT

c spports 3 unar Operatbrs

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

I increas¢S OperondS value y 4


increment OpeatDr
to use
are 2 ys
There

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]

Operator Name Value returned ePPect on uafa


x
Fst-ircremert Icremented
+tX Incremented
Pre-ircrement

#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

7 t also Post decrement & pre decremen

Operator Name Jue retuwned 6Pfect on uarbble


Post Decremented
-decremeni
Pre Deeremenbed
-decrement

#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

Here a & b are operare


int a-9, b=3, result

Operation Operabor yrtax


statement Resut
Yesultcatb
OAddition atb

bstoctio a-b result =a-b| 6

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

Print C7dy.d=% d\n a, b, a-b


Pint PC %d * Jd = 7.d\n' a,b,a*b)
Print PC.d / . d= dn", b , a/;
PrintP C/.d % d= hd a,b, ab): return 0D
Relational opeatosCcompu ison opernto
Jtcompares tuo operends
perands con be vaiables, consturts or epess
Alunys etun either True or False

OperatoDY Meanin9 Erample


Less than 3 5 gies 1

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

=1ea Equal to to9aves o


perata
Not equal tw to!=9 gives 1

include cstdio-hs
nt mainc )

int a9, b=3:


olte
Print+C"y.d >Zd = d n ' a,b, a >b)
Print C %d e d =7d\n a,b, ae)
PirtP de= %d =/d\na,b accb);
PrintC d ="7.d %d a,b a>=) o
Print f C d =Zd =7.d in a,b, a--b); m
Print PC d!=zd %d\n" ab, a!=h
Teturn O

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

#irclude <std io.h>

int maint )

int 24, 22, 2 3

21- 7>5 & lo>15;

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 )

El int a=¢, b=3, cC dgi ktes shalsi


c alb
Printt C7.d c prints'6
Ioo
ol0 CR

110 L61

Bituise xORC
xOR
eL int a=u,
b=R,c) l/ prints B AA8
Cabo O
print C7d, d;
o0 C

Bituwise NOT C)T


ta-4, c l00 C4j
C=na 10I C5)
PrinttC.d);
hift operators
Operond opeatu um y
Left shift

/ vesutt is8 Cio00)

Left-hiPt s equals to multiplication by R

Right shifE

euals to division
eiaht shi't
Crample to Bitwise OperauDYS

incude <stdio.ha
int mainC )

ínt a=4, be2;

Print f Ctt .d d = 7. d \n ab, alb)


PrintC %dl %d= %d\n" a, b, alb)
Print PC",da zd zd \n') a,b, a^b)
Prirt f Cde /.d n a,b, a) s sulont
Print PC.d ecd= %d\n", a, aec4):
Print fC .d 3> 1- %d\n", a,
a>>1);
retum O

output he3 b ile


4 220
41R=6 g
42 6
.
4LLA
422A
ASsiqoment 9peratorS

de ft side Cuariable) right side cvalue)


Locatoioo

Dint XR,
y=3, Sumj

SumEtH 5
i n t a=b=c =lo
Ca-Cb-Cc=l00))

Operator Desonption Eromple


Add AND Ct=A r) CeCHA

Substtact AND c-=A O) c=C-A

C A r ) c=cA
Multipy AND
Divide AND c/=A or) c=clA

modulus AND c A Cor) =c%4

Left shift eND Cec2 o ) ceceeR


RightshiPt ANDe=2 Cor) cc>>R

Btwisee AND c&R r) C=c2z


eeusi vee
A = Bitwise OR CA2 or)Cc A
Bitwise irelusjue oe cl=2 or) CecJR.
6
include cstdio-h> opdtrub
21
int mainC
ntint a=al, c;
Ca 2

PintPc"operator
is= and e=d\
Prirt Pc operator t e and ce p d \n"e)

Pint P Corerator and c , d\ns) 1A


Pint Coperator is *= and cehd in'e)
PrintP Coperator s =and ced\ne)
Temany o)conditional Operators
s t operator On ree Opeanrds

uar iable = expression 1? exp R erps

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

Pointer-member accessing Operatos

si2ede NO bytes allocated fo uariade/datatype

Comma Multiple expressions

*Epresonsi

Oimple
Contains one operator & 2 operands

ty 3t5 a*
Conplexr kdok
Contaúns 2 or more opeatvrs G operords

atb-cFd 2+5-34 z6-4t5°R etr


ty-2, ,
*THeconversion
To convert vaxiable rom one Data ype t

arother data tupe

Implict
pertormed by the compiler witott programm
me
interventtion.
A I Lower data types converted to hext high
data bypes

Aules

Ploat Converted to double

char or hort canuerted t int


aryone is double then other is also doulble

aryone is o n then other conuerted to long

aryone is unsigred other s also unsigned.


Higher Leuel

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

entax (datatype) expression


RulesF
A l integersCorveted to float

Float types dasble


character intger

a i n t C5)
int C/a-/Gt) 42
2/3
a
a -(double) totad/n; Ploat
a t )Cath) int Lurclo

Precedence ard Arssociatiuity r2


S NO operators Asociativity
Left to Rt
+t
Oc, C 3 , ,
t - , L , sized c. to Let
Right
Cindirection)
Lelt to R g

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

= t = , -5 *= /a, =,=s, etoL

L t oR2

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy