7/21/2019 Lec2_ConstantVariableDataType_DrHarun050112
1/22
Constants,Variables & Data
Types
Dr A R M Harunur Rashid
7/21/2019 Lec2_ConstantVariableDataType_DrHarun050112
2/22
Theoretical Questions
What is variable? !plain local & "lobal variable#
$rie%ly e!plain basic data types ith e!a'ple & their typical 'e'ory
re(uire'ents#What is an identi%ier? What are the rules %or usin" an identi%ier?
What is the di%%erence beteen )ey* ord & identi%ier? What are
the )ey ords so %ar you have used?
What is sy'bolic constant?
7/21/2019 Lec2_ConstantVariableDataType_DrHarun050112
3/22
+ocal & lobal Variables
+ocal lobal
curly brace Declared inside %unction,that is inside curlybraces,i#e# -local varpro"ra' .
Declared outside %unction,that is outside curlybraces,i#e# "lobal var- pro"ra'.
use location used by the pro"ra'bloc) inside curly braceused by any pro"ra'se"'ent anyhere in thepro"ra'
7/21/2019 Lec2_ConstantVariableDataType_DrHarun050112
4/22
/y'bolic ConstantA sy'bolic constant is de%ined ith a preprocessor directive
that assi"ns an identi%ier to the constant#
Here identi%ier0na'e "iven to the sy'bolic constant1 usually
ritten in all upper case letters#
2t can appear anyhere in a C pro"ra' thou"h usually it is'entioned in the be"innin" o% the pro"ra' a%ter the header
%iles#
The co'piler ill replace each occurrence o% the sy'bolic
constant a%ter it is de%ined ith the constant value assi"ned#
3de%ine sy'bolicna'e value4o%4constant
e#"# 3de%ine 52 6#787
9o /e'icolon
7/21/2019 Lec2_ConstantVariableDataType_DrHarun050112
5/22
/trin" constants
nclosed in double (uotes
e#"# :Hello;
7/21/2019 Lec2_ConstantVariableDataType_DrHarun050112
6/22
7/21/2019 Lec2_ConstantVariableDataType_DrHarun050112
7/22
Identifier naming rule:
The identi%iers 'ust con%or' to the %olloin" rules#
7#=irst character 'ust be an alphabet 0or underscore1
>#2denti%ier na'es 'ust consists o% only letters, di"its and
underscore#
6#identi%ier na'e should have less than 67 characters#
8#Any standard C lan"ua"e )eyord cannot be used as avariable na'e#
#identi%ier should not contain a space#
7
7/21/2019 Lec2_ConstantVariableDataType_DrHarun050112
8/22
Character
A character denotes any alphabet,
di"it or special sy'bol# 2n 'ost cases,
space is also considered as character#
7/21/2019 Lec2_ConstantVariableDataType_DrHarun050112
9/22
Constants and VariablesCharacter Set: The character set in C +an"ua"e can be "rouped into the %olloin" cate"ories#
7# +etters
># Di"its
6# /pecial Characters
8# White /paces
White /paces are i"nored by the co'piler until they are a part o% strin" constant#
White /pace 'ay be used to separate ords, but are strictly prohibited hile
usin" beteen characters o% )eyords or identi%iers#
9
7/21/2019 Lec2_ConstantVariableDataType_DrHarun050112
10/22
A constant is an entity that does not
chan"e#
A variable is an entity that 'ay chan"e
7/21/2019 Lec2_ConstantVariableDataType_DrHarun050112
11/22
Constants A constant value is the one hich does not chan"e durin" the e!ecution o% a
pro"ra'# C supports several types o% constants#
7# 2nte"er Constants
># Real Constants
6# /in"le Character Constants
8# /trin" Constants
Integer Constants:
An inte"er constant is a se(uence o% di"its#
11
7/21/2019 Lec2_ConstantVariableDataType_DrHarun050112
12/22
Real Constants:
Real Constants consists o% a %ractional part in their representation#
2nte"er constants are inade(uate to represent (uantities that vary
continuously# These (uantities are represented by nu'bers containin"
%ractional parts li)e >@#B># !a'ple o% real constants are
#>@ *# 86#> E8B#
Real 9u'bers can also be represented by e!ponential notation# The
"eneral %or' %or e!ponential notation isF mantissa e exponent# 2t'eans 'antissa ti'es a poer o% 7 The 'antissa is either a real
nu'ber e!pressed in deci'al notation or an inte"er# The e!ponent is an
inte"er nu'ber ith an optional plus or 'inus si"n# Gsually 'antissa
has an absolute value "reater than or e(ual to 7 and less than 7
#@e8 7>e*> 6#7B6 *7#>e*> 7#eE
12
7/21/2019 Lec2_ConstantVariableDataType_DrHarun050112
13/22
Single Character Constants:
A /in"le Character constant represent a sin"le character hich is enclosed
in a pair o% (uotation sy'bols# !a'ple %or character constants areF
I I!I IJI I I
All character constants have an e(uivalent inte"er value hich are called
A/C22 Values#
String Constants: A strin" constant is a set o% characters enclosed in double (uotation 'ar)s#
The characters in a strin" constant se(uence 'ay be a alphabet, nu'ber,
special character and blan) space# !a'ple o% strin" constants are
KHi thereLK K7>68K Kod $lessK KL#####?K
Backslash Character Constants [Escape Sequences]:
$ac)slash character constants are special characters used in output
%unctions# Althou"h they contain to characters they represent only one
character#
13
7/21/2019 Lec2_ConstantVariableDataType_DrHarun050112
14/22
Variables na'in" rule
Any variable declared in a pro"ra' should con%ir' to the %olloin"
7# They 'ust alays be"in ith a letter, althou"h so'e syste's per'it
underscore as the %irst character#
># The len"th o% a variable 'ust not be 'ore than B characters#
6# White space is not alloed and
8# A variable should not be a
7/21/2019 Lec2_ConstantVariableDataType_DrHarun050112
15/22
Variable Analo"y
A
77876
$
7787
C
7787B
D
7787
77877>
=778776
/ay, there are
si! seats in a
'icrobus,
na'ely
A,$,C,D,,=#
Today A seat
as bou"ht by
77876
student, $
seat by
7787 and so
on#
7/21/2019 Lec2_ConstantVariableDataType_DrHarun050112
16/22
Variable Analo"y
A
7787@
$
77877
C
77877
D
77877
77877B
=77876
/ay, there are
si! seats in a
'icrobus,
na'ely
A,$,C,D,,=#
To'orro A
seat 'ay be
bou"ht by
7787@
student, $
seat by
77877 and so
on#
7/21/2019 Lec2_ConstantVariableDataType_DrHarun050112
17/22
/o here the seat location o% A is %i!ed,
the passen"er ho is occupyin" it
chan"in"# /i'ilarly one variable ta)es a speci%ic
location in the co'puter 'e'ory# Here
the value o% the variable 'ay chan"ebut the 'e'ory location o% the variable
in the co'puter ill not be chan"ed#
7/21/2019 Lec2_ConstantVariableDataType_DrHarun050112
18/22
Data Types
A C lan"ua"e pro"ra''er has to tell the syste' be%ore*hand, the type o% nu'bers
or characters he is usin" in his pro"ra'# These are data types# There are 'any
data types in C lan"ua"e# A C pro"ra''er has to use appropriate data type as per
his re(uire'ent#
C lan"ua"e data types can be broadly classi%ied as
7#5ri'ary data type
>#Derived data type
6#Gser*de%ined data type
Primary data type:
18
7# 2nte"er int
># Character char
6# =loatin" 5oint %loat
atatype
Range of !alues
char *7>B to 7>int *6>@B to E6>@
%loat6#8 e*6B to 6#8eE6B
7/21/2019 Lec2_ConstantVariableDataType_DrHarun050112
19/22
Tips
2% a %or'ula or e(uation is used %or
solvin" a proble', all ter's e!cept
nu'bers in the %or'ula should bedeclared as variables#
7/21/2019 Lec2_ConstantVariableDataType_DrHarun050112
20/22
Declaration o% Variables
very variable used in the pro"ra' should be declared to the co'piler# The
declaration does to thin"s#
7# Tells the co'piler the variables na'e#
># /peci%ies hat type o% data the variable ill hold#
The "eneral %or'at o% any declaration
datatype v7, v>, v6, ## vnJ
Where v7, v>, v6 are variable na'es# Variables are separated by co''as# A
declaration state'ent 'ust end ith a se'icolon#
E"ampleF
int su'J
%loat nu'ber, salaryJ
20
7/21/2019 Lec2_ConstantVariableDataType_DrHarun050112
21/22
21
7/21/2019 Lec2_ConstantVariableDataType_DrHarun050112
22/22
22