You are on page 1of 12

Introduction to C

1.1. INTRODUCTION
C is a general-purpose structured programming language. Dennis Ritchie
developed C in 1972 at AT & Ts ell !a"oratories. The origins o# C can "e #ound
in a language called C$! %asic Com"ined $rogramming !anguage& and it
successor . C 'as developed along 'ith the ()*+ operating s,stem- it is strongl,
associated 'ith ()*+. This operating s,stem- 'hich 'as also developed at ell
!a"oratories- 'as coded almost entirel, in C. Toda, C is running under num"er o#
operating s,stems including ./-D0/. C is no' used 1ust a"out ever,'here. *ts
in#luence e2tends #rom academic institutions to so#t'are houses and to personal
computer uses. The important characteristic o# C is that its program 'ritten #or
one computer can "e run on another 'ith little or no modi#ications. Another
important #eature o# C is its a"ilit, to e2tend itsel#. A C program is "asicall, a
collection o# #unctions that are supported ", the C li"rar,. 3e can continuousl,
add our o'n #unctions to the C li"rar,. 3ith the a"ilit, o# a large num"er o#
#unctions- the programming tas4 "ecomes simple. The ta"le "elo' sho's the
various stages in evolution o# C language 5
Year Language Developed by Remarks
1967 A!80! *nternational
Committee
Too general- too a"stract
1969 C$! Cam"ridge
(niversit,
:ard to learn- di##icult to
implement
1967 C$! .artin Richards at
Cam"ridge
(niversit,
Could deal 'ith onl,
speci#ic pro"lems
1977 ;en Thompson at
AT & T
Could deal 'ith onl,
speci#ic pro"lems
1972 C Dennis Ritchie at
AT & T
!ost generalit, o# C$!
and restored
1.2. CHARACTER SET
Character set is a set o# valid characters that a language can recogni<e. A
character represents an, alpha"et- digit or special s,m"ols- 'hich can "e used to
#orm varia"les- num"ers and e2pressions. The characters in C are grouped as 5
1. Alpha"ets 2. Digits
9. /pecial characters =. 3hite spaces
1.2 Introduction to C
Alphabets (ppercase letters A . . . >
!o'ercase letters a . . . <
Diits All decimal digits 7 . . . 9
Special characters Tilde ? @2clamation A
At s,m"ol B )um"er sign C
Dollar sign D $ercent sign E
Caret F Ampersand &
$lus sign G .inus sin -
Asteris4 H /lash I
@Jual to K (nderscore L
!e#t parenthesis % Right parenthesis &
!e#t #lo'er "race M Right #lo'er "race N
!e#t "rac4et O Right "rac4et P
Colon 5 /emicolon Q
Apostrophe R Suotation mar4 T
Comma - $eriod %Dot operator& .
!ess than sign U 8reater than sign V
Suestion mar4 W Xertical "ar Y
ac4 slash Z
!hite spaces lan4 space Z" [orm #eed Z#
)e' line Zn Carriage return Zr
:ori<ontal ta" Zt Xertical ta" Zv
Note " The alpha"ets and digits are together called as alphanumeric
characters.
1.#. $E%!ORDS
*n a C program- ever, 'ord is either classi#ied as a 4e,'ord or an identi#ier.
;e,'ords are the 'ords 'hose meaning has alread, "een de#ined in the C
compiler. All the 4e,'ords have #i2ed meanings and these meanings cannot "e
changed ", the programmer and cannot "e used as varia"le names. The
4e,'ords are also called as reserved 'ords.
Introduction to C 1.3
Note " All the 4e,'ords must "e 'ritten in lo'ercase. /ome compilers ma,
use additional 4e,'ords.
The list o# 4e,'ords are 5
auto brea& case char
const continue de'ault do
double else enu( e)tern
'loat 'or oto i'
int lon reister return
short sined si*eo' static
struct s+itch t,pede' union
unsined -oid -olatile +hile
1... IDENTI/IERS
*denti#iers- as the name suggests- are used to identi#, or re#er to the names
o# varia"les- s,m"olic constants- #unctions and arra,s. There are certain rules
regarding identi#ier names- the, are 5
*denti#ier names must "e a seJuence o# letters and digits and must "egin
'ith a letter.
The underscore character %L& is also permitted in identi#iers. *t is usuall,
as a lin4 "et'een t'o 'ords in long identi#iers.
)ames should not "e the same as a 4e,'ord.
C is a case sensitive %i.e. upper and lo'er case letters are treated
di##erentl,&. Thus the names price- $rice and $R*C@ denote di##erent
identi#ier.
Xalid e2amples are 5
Cit, Ae basic0pa, result
date0o'0birth 1ar& nu(1 nu(2
The #ollo'ing identi#iers are invalid 5
Invalid Identifiers Reason For Invalidity
asic $a, lan4 space is not allo'ed
1price .ust start 'ith a letter
Damount /pecial characters is not allo'ed
rea4 "rea4 is a 4e,'ord
1.4 Introduction to C
1.2. CONSTANTS
Constants re#er to #i2ed values that do not change during the e2ecution o# a
program. [igure "elo' sho's the various t,pes o# constants availa"le in C 5
1.2.1. Inteer Constants
*nteger constants are 'hole num"ers 'ithout an, #ractional part.
Rules #or constructing integer constants are 5
1. An integer constant must have at least one digit and must not have a
decimal point.
2. An integer constant ma, "e positive or negative. *# no sign is preceded an
integer constant- it is assumed to "e positive.
9. @m"edded spaces- commas and non-digit characters are not allo'ed.
There are three t,pes o# integer constants namel, 5
%i& Decimal
%ii& 0ctal
%iii& :e2adecimal
3i4 Deci(al Inteer Constants
Decimal integer constants can consist an, com"inations o# digits #rom 7 to 9-
preceded ", an optional G or \ sign.
Xalid e2amples are 5
Constants
)umeric
constant
Character
constant
*nteger
constant
Real constant
/ingle
character
constant
/tring
constant
Introduction to C 1.5
5 6.7 178 92.5 22588
*nvalid e2amples are 5
Invalid Decimal Integer
Constants
Reason For Invalidity
1] 7]7 lan4 space is not allo'ed
27-777 Comma is not allo'ed
D1777 D s,m"ol is not allo'ed
12.]] Decimal point is not allo'ed
3ii4 Octal Inteer Constants
0ctal integer constants can consist o# an, com"inations o# digits #rom 7 to 7.
:o'ever- the #irst digit must "e <ero %7&- in order to identi#, the constant as an
octal num"er. [or instance- decimal integer ^ 'ill "e 'ritten as 717 as octal
integer.
Xalid e2amples are 5
5 5.7 5178 52.5
3iii4 He)adeci(al Inteer Constants
:e2adecimal integer constants can consists o# an, com"ination o# digits #rom
7 to 9 and letters #rom A to [ %either uppercase or lo'ercase&. A he2adecimal
integer constant must "egin 'ith either 72 or 7+. [or instance- decimal integer 12
'ill "e 'ritten as 7+C as he2adecimal integer.
Xalid e2amples are 5
5) 5:2 5)7A 5)bcd
Thus- the num"er 12 'ill "e 'ritten either 12 %as decimal&- 71= %as octal& and
72C %as he2adecimal&.
Note " The larger integer value that can "e stored is machine dependent.
Machine Type Largest Integer alue
16 "it machines 92-767
92 "it machines 2-1=7-=^9-6=7
1.2.2. Real Constants
Real constants are also called as #loating point constants. Real constants are
num"ers having #ractional parts. These ma, "e 'ritten in one o# the t'o #orms 5
1.6 Introduction to C
%i& [ractional #orm %ii& @2ponential #orm
3i4 /ractional /or(
The #ractional #orm consists o# a series o# digits representing the 'hole part
#ollo'ed ", a decimal point and series o# digits representing the #loating part. The
'hole part or #ractional part can "e omitted "ut not "oth. The decimal point
cannot "e omitted.
Rules #or constructing real constants e2pressed in #ractional #orm are 5
1. A real constant must have at least one digit and it must have a decimal
point.
2. A real constant could "e either positive or negative %de#ault sign is
positive&.
9. @m"edded spaces- commas and non-digit characters are not allo'ed.
Xalid e2amples are 5
5.5552 95.8; 178..7 6#1.78
6.2 9..7 178. .88
*nvalid e2amples are 5
Invalid Fractional Form Reason For Invalidity
12-777.]7 Comma is not allo'ed
91 )o decimal point
12.97.=] T'o decimal point
D1777.7] D s,m"ol is not allo'ed
12.]] Decimal point is not allo'ed
1] 7]7.2] lan4 space is not allo'ed
3ii4 E)ponential /or(
The e2ponential %or scienti#ic& #orm o# representation o# real constants is
usuall, used i# the value o# the constant is either too small or too large. *n
e2ponential #orm o# representation- the real constant is represented in t'o parts
namel,- mantissa and e2ponent. The s,nta2 is 5
mantissa e e!ponent [or]
mantissa " e!ponent
The mantissa is either a real num"er e2pressed in decimal notation or an
integer. The e2ponent is an integer num"er 'ith an optional plus or minus sign.
Introduction to C 1.7
The letter e separating the mantissa and e2ponent can "e 'ritten in either
lo'ercase or uppercase.
[or e2ample- 9.] can "e 'ritten as 7.9] 2 17 K 7.9]@1 'here the mantissa
part is 7.] %the part appearing "e#ore @& and the e2ponent part is 1 %the part
appearing a#ter @&.
Rules #or constructing real constants e2pressed in e2ponential #orm are 5
1. The mantissa and the e2ponent should "e separated ", a letter e %either
uppercase or lo'ercase&.
2. The mantissa and e2ponent part ma, have a positive or negative sign
%de#ault sign is positive&.
9. The mantissa and e2ponent part must have at least one digit.
=. @m"edded spaces- commas are not allo'ed.
Xalid e2amples are 5
5.#1e2 12e92 #.1e62 #.1<E. 91.2E92
*nvalid e2amples are 5
Invalid "!ponential Form Reason For Invalidity
12-777e2 Comma is not allo'ed
9.1e 2 lan4 space is not allo'ed
9.1@G2.= @2ponent must "e an integer
Note " [loating point constants are normall, represented as dou"le precision
Juantities. :o'ever- the su##i2es # or [ ma, "e used to #orce single precision and l
or ! to e2tend dou"le precision #urther.
1.2.#. Sinle Character Constants
A single character constant or a character constant consist o# a single
character encloses 'ithin apostrophes.
Rules #or constructing single character constants are 5
1. A character constant is a single alpha"et- a single digit- a single special
s,m"ol or a "lan4 space.
2. The ma2imum length o# a character constant can "e one character.
Xalid e2amples are 5
=>? =@? =6? =A? = ?
*nvalid e2amples are 5
1.8 Introduction to C
=abc? =12#?
1.2... Strin Constants
A string constant is a seJuence o# characters encloses 'ithin dou"le Juotation
mar4s. Technicall,- a string is an arra, o# characters. The compiler automaticall,
places the null character %Z7& at the end o# each such string- so program can
convenientl, #ind the end.
Rules #or constructing string constants are 5
1. The string constant ma, "e letters- num"ers- special characters and "lan4
spaces.
2. @ver, string constant ends up 'ith a null character- 'hich is automaticall,
assigned.
Xalid e2amples are 5
BIndiaC B2552C B!EDCO1EC
BE.EC B>C BA125C
Note "
1. A character constant R is not the same as the string constant that
contains the single character T_. The #ormer is the single character
and the latter a character string consisting o# character and Z7
%null&.
2. A character constant as an eJuivalent integer value- 'hereas single
character string constant does not have an eJuivalent integer value.
*t occupies t'o ",tes- one #or the A/C** code o# and another #or the
null character 'ith a value 7- 'hich is used to terminate strings.
1.;. @ARIA>DES
A Juantit,- 'hich ma, var, during the e2ecution o# a program- is called as
varia"le. *t is a data name that ma, "e used to store a data value. A varia"le
name can "e chosen ", the programmer in a meaning#ul 'a, so as to re#lect its
#unction or nature in the program.
Rules #or constructing a varia"le are 5
1. A varia"le name is an, com"ination o# alpha"ets- digits or underscores.
2. The #irst character in the varia"le name must "e an alpha"et.
9. )o comma or "lan4 spaces are allo'ed 'ithin the varia"le name.
Introduction to C 1.9
=. )o special characters other than an underscore %L& can "e used in a
varia"le name.
]. A varia"le name cannot "e a 4e,'ord.
6. Xaria"le names are case sensitive- i.e.- uppercase and lo'ercase letters
are treated as distinct and di##erent. :ence- .AR;- mar4 and .ar4 are
three separate names.
7. A varia"le name should not "e o# length more than 91 characters.
Xalid e2amples are 5
A@ERAFE heiht subGect1 n1
Cit, (ale0su( 'act n
*nvalid e2amples are 5
Invalid ariable #ame Reason For Invalidity
char char is a 4e,'ord
priceD D sign is illegal
group one lan4 space is not allo'ed
7^6 [irst character must "e an alpha"et
Note "
1. /ome compilers permit underscore as the #irst character.
2. There is no limit on the length o# the varia"le- "ut some compilers
recogni<e onl, #irst eight characters. [or e2ample- the varia"le name
dataname1 and dataname2 mean the same thing to the compiler.
1.7. DECDARATION O/ @ARIA>DES
The declaration o# varia"les must "e done "e#ore the, are used in the
program. *t tells the compiler 'hat the varia"le name is and it speci#ies 'hat t,pe
o# data the varia"le 'ill hold. A varia"le declaration consists o# a data t,pe name
#ollo'ed ", a list o# one or more varia"les o# that t,pe separated ", commas. The
s,nta2 is 5
data$type %& '& ( ( ( n )
3here X1- X2- . . . Xn are the names o# the varia"les.
Xalid e2amples are 5
int su( H
int (ar&1I (ar&2I (ar&# H
'loat basicpa,I netpa, H
char se) H
1.10 Introduction to C
:ere int- #loat and char are the 4e,'ords to represent integer- real and
character data values respectivel,.
1.<. DATA T%JES
C language is rich in its data t,pes. C supports #our classes o# data t,pes-
namel, 5
1. $rimar, %or #undamental& data t,pes
2. (ser-de#ined data t,pes
9. Derived data t,pes
=. @mpt, data set
All C compilers supports #our #undamental data t,pes- namel, 5
1. int \ a signed or unsigned num"er having no special characters
2. #loat - a signed or unsigned num"er having a decimal point
9. dou"le - a dou"le precision #loating point num"er
=. char \ a character in the character set
Ta"le "elo' sho's the si<e and range o# "asic data t,pes 5
Data Type *i+e Range
char 1 ",te -12^ to 127
int 2 ",tes -92-76^ to 92-767
#loat = ",tes 9.=e-9^ to 9.=eG9^
dou"le ^ ",tes 1.7e-97^ to 1.7eG97^
1.<.1. Inteer Data T,pe
*ntegers are 'hole num"ers 'ith a range supported ", a particular machine.
The, have no #ractional parts. *ntegers are represented in C ", int data t,pe. An
identi#ier declared as int "ecomes an integer varia"le and can hold integr values
onl,. An integer constant al'a,s occupies t'o ",tes in memor,. The ta"le "elo'
sho's the range o# values that can "e stored in various #orms o# an integer
num"ers 5
Type Length Range
unsigned int %or&
unsigned short int
16 "its 7 to 6]-]9]
int %or&
short int %or&
signed int %or&
signed short int
16 "its -92-76^ to 92-767
(nsigned long %or&
(nsigned long int
92 "its 7 to =-29=-967-29]
Introduction to C 1.11
long %or&
long int %or&
signed long int
92 "its -2-1=7-=^9-6=^ to 2-1=7-=^9-6=7
1.<.2. /loatin point Data T,pe
The dou"le data t,pe is also used #or handling #loating point num"ers.
[loating point %or real& num"ers are stored in 92 "its %on all 16 "it and 92 "it
machines& 'ith 6 digits o# precision. A num"er having #ractional part is a called
#loating point num"er. [or e2ample the num"er 12 is an integer- "ut 12.7 is a
#loating point num"er. [loating point num"ers can also "e 'ritten in e2ponent
#orm. An identi#ier declared as #loat data t,pe "ecomes #loating point varia"le and
can hold #loating point num"ers onl,. The ta"le "elo' sho's the range o# values
that can "e stored in various #orms o# a #loating point num"ers 5
Type Length Range
#loat 92 "its 9.=e-9^ to 9.=eG9^
1.<.#. Double Jrecision Data T,pe
3hen the accurac, provided ", a #loat num"er is not su##icient- the t,pe
dou"le can "e used to de#ine the num"er. A dou"le data t,pe num"er uses 6= "its
giving a precision o# 1= digits. These are 4no'n as dou"le precision num"ers. To
e2tend the precision #urther- long dou"le 'hich uses ^7 "its. The ta"le "elo'
sho's the range o# values that can "e stored in various #orms o# a dou"le
precision num"ers 5
Type Length Range
dou"le 6= "its 1.7e-97^ to 1.7eG97^
long dou"le ^7 "its 9.=e-=992 to 1.1eG=992
1.<...Character Data T,pe
A single character can "e de#ined as a character %char& t,pe data. An identi#er
declared as char data t,pe "ecomes a character varia"le. Characters are usuall,
stored in 1 ",te %^ "its& o# internal storage.
The Juali#iers signed or unsigned ma, "e e2plicitl, applied to char. A signed
char is same as ordinar, char and has range #rom \12^ to 127- 'hereas unsigned
char has a range #rom 7 to 2]]. The ta"le "elo' sho's the range o# values that
can "e stored in various #orms o# a character 5
Type Length Range
unsigned char ^ "its 7 to 2]]
char %or &
signed char
^ "its -12^ to 127
1.12 Introduction to C
Note " ^ "its K 1 ",te
The ta"le "elo' sho's various data t,pes and their 4e,'ord eJuivalents 5
Data Type ",uivalent -ey.ord
Character char
(nsigned character unsigned char
/igned character signed char
/igned integer signed int %or& int
/igned short integer signed short int %or& short int %or& short
/igned long integer signed long int %or& long int %or& long
(nsigned integer unsigned int %or& unsigned
(nsigned short integer unsigned short int %or& unsigned short
(nsigned long integer unsigned long int %or& unsigned long
[loating point #loat
Dou"le-precision dou"le
@2tended dou"le precision long dou"le
Note " 3hen an ad1ective %Juali#ier& short- long or unsigned is used 'ith a
"asic data t,pe speci#ier- C compilers treat the data t,pe as int. *# 'e 'ant to
declare a character varia"le as unsigned- then 'e must do so using "oth the
terms unsigned char.

You might also like