Vous êtes sur la page 1sur 30

CSE 203G: Introduction to C

Lecture – 5

Course Teacher: Dr. Md Sadek Ferdous


Assistant Professor, CSE, SUST
E-mail: ripul.bd@gmail.com
Outline
• Identifiers
• Variables
• Expressions
• Statements
Identifiers
• Identifiers are names that are given to various program elements, such as
variables, functions and arrays
• E.g. When we name a variable or a function they are regarded as identifiers
• The rules that are used to name a variable also apply to any identifier
• An identifier can be arbitrarily long
• Some implementations of C recognise only the first eight characters
• However, most implementations recognise more (typically, 31 characters)
• As a rule, an identifier should contain enough characters so that its
meaning is readily apparent
• On the other hand, an excessive number of characters should be avoided
Constants
• There are four basic types of constants in C
• Integer, floating-point, character and string constants
• Moreover, there are several different kinds of integer and floating-point constants
• Integer and floating-point constants represent numbers
• They are often referred to collectively as numeric-type constants
• The following rules apply to all numeric-type constants
• Commas and blank spaces cannot be included within the constant
• The constant can be preceded by a minus (-) sign if desired
• The value of a constant cannot exceed specified minimum and maximum bounds
(the minimum and maximum number that can be represented)
• For each type of constant, these bounds will vary from one C compiler to another
Integer constants
• An integer constant is an integer-valued number
• Integer constants can be written in three different number systems:
decimal (base 10), octal (base 8) and hexadecimal (base 16)
• Beginning programmers rarely, however, use anything other than
decimal integer constants
• A decimal integer constant can consist of any combination of digits
taken from the set 0 through 9
• If the constant contains two or more digits, the first digit must be
something other than 0
can be written in three different number systems: decimal (base lO), octal (base 8) and hexadecimal (base 16
constant contains two or more digits, the first digit must be something other than 0.
Beginning programmers rarely, however, use anything other than decimal integer constants.
A decimal integer constant can consist of any combination of digits taken from the set 0 through 9. If th
constant Integer constants
EXAMPLE
contains two2.4
or moreSeveral
digits, valid decimal
the first integer
digit must constants
be something arethan
other shown
0. below.

EXAMPLE 2.4 Several valid decimal integer constants are shown below.
0 1 743 5280 32767
0 1 743 5280 32767 9999
The following decimal integer constants are written incorrectly for the reasons stated.
The following decimal integer constants are written incorrectly for the reasons stated.
12,245 illegal character (, ).
12,245 illegal character (, ).
36.0 36.0 illegal character (.).
illegal character (.).
10 20 30 illegal
10 20 30illegal character (blank character (blank space).
space).
123-45-6789 illegal character (-).
123-45-6789 illegal character (-).
0900 the first digit cannot be a zero.
0900 the first digit cannot be a zero.
An octal integer constant can consist of any combination of digits taken from the set 0 through 7
However the
Anfirstoctal
digit must be 0,in
integer order to identiQ
constant the constant
can consist as an octal
of any number. of digits taken
combination
Octal and hexadecimal numbers
• Octal numbers are represented using 8 digits: 0 to 7
• We can apply the similar rules, as binary conversion, to convert
decimal to octal and octal to decimal
• (456)8 = (302)10
• (145)10= (221)8
• Hexadecimal numbers are represented using 16 digits: 0 to 9 and A
(10) to F (15)
• Similarly:
• (4DA)16 = (1242)10
• (266)10= (10A)16
10 20 30 illegal character (blank space).
123-45-6789 illegal character (-).
123-45-6789 illegal character (-).
Octal constants0900 0900 thethefirst
first digit cannot
digit cannot be be a zero.
a zero.

An octalAninteger constant
octal integer cancan
constant consist
consistofof any combination
any combination of digits
of digits taken taken
from th
• AnHowever
However octal
the integer
firstthe firstconstant
digit must can
digit must be 0,be 0,consist
in order of
in order any combination
totoidentiQ
identiQ ofandigits
the constant as
the constant asoctal
an number.
octal numb
taken from the set 0 through 7
EXAMPLE 2.5 Several valid octal integer constants are shown below.
• However
EXAMPLE 2.5
the first digit
Several valid must be 0,inconstants
octal integer order to areidentify
shownthe constant as
below.
an octal number 0 01 0743 077777

0
The following 01 constants 0743
octal integer are written incorrectly077777
for the reasons stated.

The following octal integer


743 constants are written begin with 0. for the reasons stated.
Does not incorrectly
05280 Illegal digit (8).
743 0777.777 Does notcharacter
Illegal .). 0.
begin (with
05280 Illegal digit (8).
28 C FUNDAMENTALS
Hexadecimal
28 constants C FUNDAMENTALS [

hexadecimal
• AAhexadecimal
A hexadecimal integer
integerintegerconstant
constant mustbegin
constant must
must begin
begin with
withwith either
eithereither
Ox 0xOxor
or OX. It or
0XOX.
can then beIt followed
can th
combination
combination of digits
of digits takentaken from the
from the sets
sets0 through 9 and 9
0 through a through f (either upper-
and a through or lowercase).
f (either upper- No
• It canthethen
lettersbe followed
a through f (or Aby any F)
through combination of digits
represent the (decimal) taken
quantities from the
10 through 15, respectively.
the letters a through f (or A through F) represent the (decimal) quantities 10 through 1
sets 0 through 9 and a through f (either upper- or lowercase)
EXAMPLE 2.6 Several valid hexadecimal integer constants are shown below.
• Note that the letters a through f (or A through F) represent the
EXAMPLE 2.6 Several valid hexadecimal integer constants are shown below.
(decimal) quantities ox 10 throughox 1 15, respectively
OX7FFF Oxabcd

The following oxinteger


ox hexadecimal 1 OX7FFF
constants Oxabcd
are written incorrectly for the reasons stated.

OX12.34 Illegal character ( .).


The following hexadecimal integer constants are written incorrectly for the reasons stated.
OBE38 Does not begin with Ox or OX.
Illegal character ( .).
Illegal character ( .).
Ox. 4bf f
OX12.34
OXDEFG Illegal character (G).
OBE38 Does not begin with Ox or OX.
Unsigned and long integer constants
• An unsigned integer constant can be identified by appending the
letter U (either upper- or lowercase) to the end of the constant
• With some computers (and some compilers), a long integer constant
will automatically be generated simply by specifying a quantity that
exceeds the normal maximum value
• It is always possible, however, to create a long integer constant by
appending the letter L (either upper- or lowercase) to the end of the
constant
• An unsigned long integer may be specified by appending the letters
UL to the end of the constant
always possible, however, to create a long integer constant by appending the letter L (either
lowercase) to the end of the constant.
An unsigned long integer may be specified by appending the letters UL to the end of the cons
Unsigned and long integer constants
letters may be written in either upper- or lowercase. However, the U must precede the L.

EXAMPLE 2.7 Several unsigned and long integer constants are shown below

Constant Number System


50000U decimal (unsigned)
123456789L decimal (long)
123456789UL decimal (unsigned long)
0123456L octal (long)
077777711 octal (unsigned)
OX50000U hexadecimal (unsigned)
OXFFFFFUL hexadecimal (unsigned long)

* Suppose a particular computer uses a w-bit word.Then an ordinary integer quantity may fall within the range -2w - to
whereas an unsigned integer quantity may vary From 0 to 2w - 1. A short integer may substitute w/2 for w,and a long inte
substitute 2w for w. These rules may vary from one computer to another.
propriate values for your particular version of C.

Floating-point constants
ating-Point
oating-Point Constants
Constants

ating-point constantisisa abase-


oating-point constant base-1010 number
number thatthat contains
contains eithereither a decimal
a decimal point orpoint or an exponent
an exponent (or both).(or both).
• A floating-point constant is a base- 10 number that contains either a
decimal point or an exponent (or both)
AMPLE 2.8 Several valid floating-point constants are shown below.
AMPLE 2.8 Several valid floating-point constants are shown below.
0
0.. 1.
1. 0.2
0.2 827.602
827.602
50000. 0.000743 12.3 31 5.0066
50000. 0.000743 12.3 31 5.0066
2 E-8 0.006e-3 1.6667E+8 .12121212e12
2 E-8 0.006e-3 1.6667E+8 .12121212e12
The following are not valid floating-point constants for the reasons stated.
The following are not valid floating-point constants for the reasons stated.
1 Either a decimal point or an exponent must be present.
11, 0 0 0 . 0 Either
Illegal a decimal
character (, ). point or an exponent must be present.
1,000.0
2E+10.2 Illegal
The character
exponent must be(,an
). integer quantity (it cannot contain a decimal point).
10
2E+10.2
3E The character
Illegal exponent(blank
mustspace)
be an ininteger quantity (it cannot contain a decimal point).
the exponent.
3E 10 Illegal character (blank space) in the exponent.
If an exponent is present, its effect is to shift the location of the decimal point to the right, if the exponent
.. .
0.99999999. , even though it might appear as 1 0 when it is displayed (because
Character of automatic
constants rounding).
have integer values tha
Therefore floating-point values cannot be used for certain purposes,Thus,such the
as counting,
value of aindexing,
characteretc., wheremay vary
constant
exact values are required. We will discuss these restrictions as they arise, in later
however, arechapters of thisofbook.
independent the character set. T

Character constants
Character Constants
particular character set (more about this later).
Most computers, and virtually all personal c
Code for Information Interchange) character set
withsingle
A character constant is a single character, enclosed in apostrophes (i.e., its own uniquemarks).
quotation 7-bit combination (hence a to
• A character constant is a single character, enclosed ASCII character set, showing the decimal equival
in apostrophes
EXAMPLE 2.10 Several (i.e.,
charactersingle are shown below.marks) characters are ordered as well as encoded. In pa
constantsquotation
numerical sequence (0 to 9), and the letters are ar
'A' 'XI '3' uppercase characters preceding lowercase charac
' ? I
I I

with one another, based upon their relative order w


Character
•Notice constants have integer values that are
that the last constant consists of a blank space, enclosed in apostrophes.
determined by the computer's particular character EXAMPLE 2.11 Several character constants and the
setCharacter constants have integer values that are determined byshown below.
the computer's particular character set.
Thus, the value of a character constant may vary from one computer to another. The constants themselves,
Most are
•however, computers, and virtually all personal Constant
independent of the character set. This feature eliminates the dependence of a C program on any
Value
computers,
particular character make
set (moreuse
aboutof thisthe ASCII (i.e., American
later). 'A' 65
Standard Codeandfor
Most computers, Information
virtually all personal Interchange)
computers, make use of the ASCII (i.e.,O X ' American Standard
120
character
Code set Interchange) character set, in which each individual character is'3'
for Information numerically encoded
51
with its own unique 7-bit combination (hence a total of 2' = 128 different characters). ' Table 2-1 contains63the
• Here, each individual character is numerically encoded 7'
ASCII character set, showing the decimal equivalent of the 7 bits that represent each character. Notice that the
withareitsordered
characters
own unique
as well as
7-bit combination
encoded. In
(hence
particular, the
a total
digits are
of consecutively in their proper
ordered
I '
32
2 = 128 different characters)
7
numerical sequence (0 to 9), and the letters are arranged consecutively in These
their proper alphabetical order, with
values will be the same for all computers
CHAP. 21 C FUNDAMENTALS 31

Table 2-1 The ASCII Character Set

Character constants ASCII


Value

0
Character

NUL
ASCII
Value

32
Character

(blank)
ASCII
Value

64
Character
@
ASCII
Value
96
Character

1 SOH 33 1 65 A 97 a
2 STX 34 It
66 B 98 b
3 ETX 35 # 67 C 99 C

4 EOT 36 $ 68 D 100 d
5 ENQ 37 % 69 E 101 e
6 AC K 38 & 70 F 102 f
7 BEL 39 I
71 G 103 g
8 BS 40 ( 72 H 104 h
9 HT 41 1 73 I 105 i
10 LF 42 * 74 J 106 j
11 VT 43 + 75 K 107 k
12 FF 44 1 76 L 108 1
13 CR 45 - 77 M 109 m
14 so 46 78 N 110 n
15 SI 47 I 79 0 111 0

16 DLE 48 0 80 P 112 P
17 DC1 49 1 81 Q 113 q
18 DC2 50 2 82 R 114 r
19 DC3 51 3 83 S 115 S

20 DC4 52 4 84 T 116 t
21 NAK 53 5 85 U 117 U

22 SY N 54 6 86 V 118 V

23 ETB 55 7 87 W 119 W

24 CAN 56 8 88 X 120 X

25 EM 57 9 89 Y 121 Y
26 SUB 58 90 Z 122 z
27 ESC 59 1 91 123 {
28 FS 60 C 92 \ 124 I
29 GS 61 - 93 I 125 }
30 RS 62 > 94 A
126 -
31 us 63 ? 95 - 127 DEL

'he first 32 characters and the last character are control characters. Usually, they are not displayed. However, some
String constants
CHAP. 21 C FUNDAMENTALS 33

• A string constant consists of any number of consecutive characters


(including none),
EXAMPLE enclosed
2.14 Several in (double)
string constants quotation marks
are shown below.
"green" "Washington, D.C. 20005H "270-32-3456"
"$19.95" "THE CORRECT ANSWER IS:' '2 * ( I+3)/J "
II II
" L i n e l \ n L i n e 2 \ n L i n e 3" w II

Note that the string constant " L i n e 1\ n L i n e 2 \ n L i n e 3 " extends over three lines, because of the newline characters
• Note that the string constant "Line 1\nLine 2\nLine 3" extends over three
that are embedded within the string. Thus, this string would be displayed as
lines, because of the newline characters that are embedded within the
string Line 1
Line 2
• Also, noticeLthat i n e 3 the string is a null (empty) string

• The compiler automatically


Also, notice that the string is a nullplaces a null character (\0)at the end of every
(empty) string.
string constant, as the last character within the string (before the closing
double quotation
Sometimes certainmark) special characters (e.g., a backslash or a quotation mark) must be included as a part of
a string constant. These characters must be represented in terms of their escape sequences. Similarly, certain
nonprinting characters (e.g., tab, newline) can be included in a string constant if they are represented in terms
of their corresponding escape sequences.
String constants
• Remember that a character constant (e.g., ‘A’ ) and the corresponding
single-character string constant ("A") are not equivalent
• Also remember that a character constant has an equivalent integer
value
• A single-character string constant does not have an equivalent integer
value and
• In fact, consists of two characters - the specified character followed by the
null character(\0)
Variables
• A variable is an identifier that is used to represent some specified
type of information within a designated portion of the program
• In its simplest form, a variable is an identifier that is used to represent
a single data item; i.e., a numerical quantity or a character constant
• The data item must be assigned to the variable at some point in the
program
• The data item can then be accessed later in the program simply by
referring to the variable name
Variables
• A given variable can be assigned different data items at various places
within the program
• Thus, the information represented by the variable can change during
the execution of the program
• However, the data type associated with the variable cannot change
type associated with the variable cannot change.

Variables EXAMPLE 2.18 A C program contains the following lines.

i n t a, b, c; Type declarations
char d;
. . .
a = 3;
b = 5;
c = a + b ;
d = IaI;
Assigning values
a = 4;
b = 2;
c = a - b ;
d = 'W';
Arrays
• The array is another kind of variable that is used extensively in any
programming language
• An array is an identifier that refers to a collection of data items that all have
the same name
• The data items must all be of the same type (e.g., all integers, all
characters, etc.)
• The individual data items are represented by their corresponding array-
elements (i.e., the first data item is represented by the first array element,
etc.)
• The individual array elements are distinguished from one another by the
value that is assigned to a subscript
Arrays
• int x[10];

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

• Subscript in each array is kept inside []


• Each of x[0], x[1] …. are regarded an element of the array x
• x[5] = 10;
• x[0] = 1;
• Every type of data can have their associated arrays:
• Integer array, char array, float array, double array
Expressions
• An expression represents a single data item, such as a number or a
character
• It may be a single entity, such as a constant, a variable, an array element or a
reference to a function
• It may also consist of some combination of such entities, interconnected by
one or more operators
• The use of expressions involving operators is particularly common in every
programming language
• Expressions can also represent logical conditions that are either true or
false
• However, in C the conditions true and false are represented using numerical integer
values of 1 and 0,respectively
conditions true and false are represented by the inte
expressions really represent numerical quantities.
Expressions
EXAMPLE 2.27 Several simple expressions are shown be

a + b + Addition operator
x = y = Assignment operator

c = a + b Addition and assignment operators


Relational operator, if x is less than or
equal to y
x <= y
x == Y Test for equality
++i Incremental expression, i = i + 1

The first expression involves use of the addition oper


assigned to the variables a and b.
Statements
• A statement causes the computer to carry out some action
• There are three different classes of statements in C
• expression statements, compound statements and control statements
• An expression statement consists of an expression followed by a
semicolon
• The execution of an expression statement causes the expression to be
evaluated
They are expression statements, compound statements and control statements.
An expression statement consists of an expression followed by a semico
expression statement causes the expression to be evaluated.
Statements EXAMPLE 2.28 Several expression statements are shown below.

a = 3; Assignment type statements


c = a + b ;
Increment type statements ++i ;
p r i n t f ( " A r e a = %f
'I,printf function is evaluated
area) ;
9
Does nothing, a null statement
The first two expression statements are assignment-type statements. Each causes the value
of the equal sign to be assigned to the variable on the left. The third expression state
statement, which causes the value of ito increase by 1.
The fourth expression statement causes the p r i n t f function to be evaluated. This i
that writes information out of the computer (more about this in Sec. 3.6). In this case,
displayed, followed by the current value of the variable area. Thus, if a r e a represents th
generate the message

Area = 100.
providing an empty expression statement in places where thi
null statement.

Statements A compound statement consists of several individ


The individual statements may themselves be expr
statements. Thus, the compound statement provides
statements. Unlike an expression statement, a compoun
• A compound statement consists of several
individual statements enclosed within a pair
EXAMPLE 2.29 A typical compound statement is shown b
of braces { }
• The individual statements may themselves be 1 p i = 3.141593;
expression statements, compound statements circumference = 2 . * p i * r a d i u s ;
or control statements area = p i * radius * radius;
• Thus, the compound statement provides a 1
capability for embedding statements within
other statements
• Unlike an expression statement, a compound
statement does not end with a semicolon
Expressions vs Constants
• A statement is like an instruction that your runtime performs
• Programs consist of statements
• Without statements, there is nothing to do
• An expression is a piece of code that can be 'evaluated', meaning it
can be reduced to a value
• 'x = 1' Is an expression that evaluates to 1
• 'x = 1;' is a statement (note the semi-colon at the end)
Symbolic constants
• A symbolic constant is a name that substitutes for a sequence of characters
• The characters may represent a numeric constant, a character constant or a string
constant
• Thus, a symbolic constant allows a name to appear in place of a numeric constant, a
character constant or a string
• When a program is compiled, each occurrence of a symbolic constant is
replaced by its corresponding character sequence
• Example: #define name text
• name represents a symbolic name, typically written in uppercase letters
• text represents the sequence of characters that is associated with the symbolic name
semicolon, since a symbolic constant definition is not a true C statemen
a semicolon, this semicolon would be treated as though it were a pa
Symbolic constants
constant or string constant that is substituted for the symbolic name.

EXAMPLE 2.31 A C program contains the following symbolic constant defi

#define TAXRATE 0.23

#define PI 3.141593

#define TRUE 1
#define FALSE 0

#define FRIEND "Susan"


The lecture slides can be found in the following location!