Professional Documents
Culture Documents
CSE 203G: Introduction To C: Lecture - 5
CSE 203G: Introduction To C: Lecture - 5
Lecture – 5
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.
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
EXAMPLE 2.7 Several unsigned and long integer constants are shown below
* 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
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
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
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
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]
a + b + Addition operator
x = y = Assignment operator
Area = 100.
providing an empty expression statement in places where thi
null statement.
#define PI 3.141593
#define TRUE 1
#define FALSE 0