Chapter 02
Chapter 02
Chapter 2
Types, Operators & Expressions
Mathematical C 2r
formula:
constant
constant
variable
variable
operator
operator
How to process data?
The C Programming Language
Chapter 2 Types, Operators & Expressions
Pointer type
Data type (指针类型)
数据类型 array (数组)
Construction types struct (结构)
(构造类型) union (联合)
enum (枚举)
void (空类型)
Data Types in C
Guess what type it is
char 6 a b
How does memory store data
Would
Would you
you like
like aa single
single
Data are various. Two room,
room, aa double
double room,
room, aa
Two people,
people, live
live in
in
four-room
four-room or
or aa
First, apply a aa double
double room.
room.
presidential
presidential suite?
suite?
suitable space to
the data according
to the demand of
data (i.e. type),
and memory is
like a hotel.
Types and rooms
Corresponding
short 2byte double room
Corresponding
int 2/4byte Double/four-room
Corresponding
double 8byte presidential suite
The letters in perfume
char
int How many
parts does a
car have?
Keywords bytes Value range
int 16 -32768~32767
short 16 -32768~32767
long 32 -2147483648~2147483647
float 32 3.4e-38~3.4e38
double 64 1.7e-308~1.7e308
Number
Sho
rt
of apples
in the
basket
long
Number of celestial
bodies in the universe
How many
milliliters of
float
orange juice are
there?
The distance
between two cities
doubl
e
Fruit in the
tray
enu
m
Data Sizes
Example:
Legal variable names (or identifier):
sum, average, class, day, month,
student_name, _above,
lotus_1_2_3, basic,
The_C_Programming_Language
Illegal variable names:
M.D.Join, $123, #33, 3D64, a>b
2.3 Constants
Type Example remark
int 123, -456 int constants
long 123456789l or 123456789L
unsigned int 123u or 123U
unsigned long 123456789ul or 123456789UL
octal 037
hexadecimal 0x1f or 0X1F
float or double 123.4 or 1e-2; 123.4l or 123.4L float, double
float ONLY 123.4f or 123.4F constants
‘M’ “hello ”
‘F’
‘a’ “sunshine
‘9’ ”
‘?’ ‘A’ “One”
“A”
Single character
“A ‘ ‘\0’
” A
Chapter 2 Types, Operators & Expressions
2.3 Constants
“”(null string)
\0
i = 0; s[3] m
while (s[i] != ‘\0’)
…… ……
i++;
return(i); s[N-
\0
1]
}
Chapter 2 Types, Operators & Expressions
2.3 Constants
Constant expression:
Is an expression that involves only constants. Such
expressions may be evaluated during compilation rather
than run-time.
Example:
#define MAXLINE 1000
char line[MAXLINE+1];
Chapter 2 Types, Operators & Expressions
2.3 Constants
Roomname
Room name Variablename
Variable name
One Roomtype
type Variabletype
type A variable
Room Variable
room
Guestsstaying
Guests staying Variablevalue
Variable value
Statement of variables
int a = 1; a = 3;
Example
char ch2
char ‘a’;;
ch2 == ‘a’
char ch3
char ‘g’;;
ch3 == ‘g’
ch4 == ‘i’‘i’;;
char ch4
char
ch5 == ‘c’‘c’;;
char ch5
char
Check if the following characters are legitimate
‘M’ ‘&’
‘bool’ ‘5.2’
× ×
Character variable values are stored in memory as ASCII Dec Hex char
codes of characters, i.e. an unsigned integer, so character
data is allowed to perform arithmetic operations. 65 41 A
66 42 B
char ch1
char ‘b’-’a’;;
ch1 == ‘b’-’a’
1 … … …
90 5A Z
char ch2
char ‘c’+1;;
ch2 == ‘c’+1 ‘d’ … … …
97 6A a
98 6B b
char ch3
char ‘a’-32;;
ch3 == ‘a’-32
‘A’
… … …
122 7A z
Chapter 2 Types, Operators & Expressions
2.4 Declarations
2.4 Declarations
All variables MUST be declared before use.
Declaration form:
type var1, var2, …, varN;
int lower, upper, step;
char c, line[1000];
When a variable is not automatic (external and
static), the initializer must be a constant
A variable
expression.mayThe initialization
also is done
be initialized inonce
its only
before the program starts executing.
declaration.
general form:
char esc = ‘\\’;
int i = 0, j, k=1, m;
int limit = MAXLINE+1;
float eps = 1.0e-5;
Chapter 2 Types, Operators & Expressions
2.4 Declarations
const form:
specify that the value will not be changed.
Example:
const double pi = 3.1415926535;
const char message[] = “warning:”;
ATTENTION!!!
type conversion occurs when different operands
Examples:
10.0/4 2.5, 10/4 2, -10/4 -2
10%3 1, -10%3 -1, 10 % 2 0
Chapter 2 Types, Operators & Expressions
2.5 Arithmetic Operators
/* leap year */
#include<stdio.h>
void main()
{
int year;
int i;
for(i=0;i<4;i++)
{
scanf("%d",&year);
if((year%4==0 && year%100!=0) || year%400==0)
printf("%d is a leap year\n", year);
else
printf("%d is not a leap year\n", year);
}
}
Chapter 2 Types, Operators & Expressions
2.6 Relational and Logical Operators
>
>=
what are the<calculating order
<=
of
the following expressions?
==
c>a+b; != c > (a + b)
a>b!=c; (a > b) != c
a=b>c; a = (b > c)
Chapter 2 Types, Operators & Expressions
2.6 Relational and Logical Operators
Relational expression
Examples:
a>b The value of R. Exp is
a+b > b+c a logical value: TRUE /
(a=3) > (b=5)
FALSE
‘a’ < ‘b’ ( In C, 1 -- TRUE , 0 – FALSE )
(a>b) > (b<c)
Logical operators
&& || !
a && b if a, b are both TRUE, then a && b
is TRUE;
a || b if one of a and b is TRUE, then
a || b is TRUE;
!a if a is TRUE, then !a is FALSE.
precedence:
“!” > “&&” > “||”
associativity:
!: unary operator
&&, || : from left to right
Chapter 2 Types, Operators & Expressions
2.6 Relational and Logical Operators
precedence:
Operator
!
arithmetic operators higher
relational operators
&&
||
lower
=
Example:
(1) if a = 4, then !a 0
(2) if a=4,b=5, then a&&b 1,a||b => 1,!a&&b 0
(3) 4&&0||2 1
(4) ‘c’ && ‘d’ 1 (because ‘c’ and ‘d’ are both nonzero)
(5) 5 > 3 && 2 || 8 < 4 - !0
1 && 2 || 8 < 4 - !0
1 || 8 < 4 - !0
1 || 8 < 4 - 1
1 || 8 < 3
1 || 0
1
Chapter 2 Types, Operators & Expressions
2.6 Relational and Logical Operators
double ( float )
higher
unsigned long
long
unsigned
lower
int char, short
Chapter 2 Types, Operators & Expressions
2.7 Type Conversion
Example 1:
Suppose: int i; float f; double d; long e;
10 + ‘a’ + i * f - d /e
(1) 10+ ‘a’,
‘a’ is converted to int 97, the result is int 107
(2) i*f,
i=>double, f=>double
(3) int 107 + the result of i*f ,
107=>double
(4) d/e,
e=>double, d/e=>double
(5) the result’s type of the expression is double.
Chapter 2 Types, Operators & Expressions
2.7 Type Conversion
n = 0;
for (i = 0; s[i] >= '0' && s[i] <= '9'; i++)
n = 10 * n + (s[i] - '0');
return n;
}
Example 3:
/* lower: convert c to lower case; ASCII only */
int lower(int c)
{
if (c >= ‘A’ && c <= ‘Z’)
return c + ‘a’ - ‘A’;
else
return c;
}
x = 3.6;
i = (int)x;
printf(“x = %f, i = %d\n”, x, i);
}
x = 3.600000, i = 3
what is the output of the program?
Chapter 2 Types, Operators & Expressions
2.8 Increment and Decrement Operators
#include<stdio.h>
void squeeze(char s[], int c);
Example 1: void main()
/* squeeze: delete all c from { s */
void squeeze(char s[], int c)char string[]="calculating";
{ printf("%s\n", string);
int i, j; squeeze(string, 'c');
printf("%s\n", string);
for (i = j = 0; s[i] != ‘\0’; }i++)
if (s[i] != c) void squeeze(char s[], int c)
s[j++] = s[i];
{
s[j] = ‘\0’; int i, j;
} for (i = j = 0; s[i] != '\0'; i++)
if (s[i] != c)
s[j++] = s[i];
s[j] = '\0';
}
Chapter 2 Types, Operators & Expressions
2.8 Increment and Decrement Operators
s H o w a r e ‘\0’
:
Example 2: t: y o u ? ‘\0’
s H o w a r e y o u ? ‘\0’
/* strcat: concatenate t :to end of s; s must be big
enough */
void strcat(char s[], char t[])
{
int i, j;
i = j = 0;
while (s[i] != ‘\0’) /* find end of s */
i++;
while ((s[i++] = t[j++]) != ‘\0’) /*copy t*/
;
}
Chapter 2 Types, Operators & Expressions
2.9 Bitwise Operator
Bitwise operation
a b a&b a|b a^b ~a ~b
0 0 0 0 0 1 1
0 1 0 1 1 1 0
1 0 0 1 1 0 1
1 1 1 1 0 0 0
Example 1:
0000 0011
3&5=
& 0000 1001 =1
0000 0001
Chapter 2 Types, Operators & Expressions
2.9 Bitwise Operator
n xxxx…xxxx X???????
&
0177 0000…0000 01111111
↓
n 0000…0000 0???????
Chapter 2 Types, Operators & Expressions
2.9 Bitwise Operator
Bitwise inclusive OR |:
Example4:
| is used to turn bits on:
060 | 017=?
060 0011 0000
017 | 0000 1111
0011 1111
x = x | SET_ON;
If x is 1 and y is 2, then
x & y =? and x&&y=?
0 1
Chapter 2 Types, Operators & Expressions
2.9 Bitwise Operator
#include<stdio.h>
Bitwisevoid main() OR ^:
exclusive
{
if corresponding bits are the same for two operands,
int0,a=1,
the result is b=2, t; 1.
otherwise,
071 |printf("%d
052=? %d\n", a,b);
071 0011 1001
t=a; a=b; b=t;052 /*a=a^b; b=b^a;
^ 0010a=a^b;
1010*/
printf("%d %d\n", a,b); 0001 0011 (023)
}
example5: exchanges two variables’ value:
Solution1: Declares a temporary variable t and then
t=a; a=b; b=t;
Solution2: uses ^:
a=a^b; b=b^a; a=a^b;
Chapter 2 Types, Operators & Expressions
2.9 Bitwise Operator
One’s complement ~:
The unary operator ~ yields the one’s complement of
an integer; that is, it converts each 1-bit into 0-bit
and vice versa.
e.g.
025=010 101
~025=101 010
27 26 25 24 23 22 21 20
0 1 0 0 0 0 0 0
27 26 25 24 23 22 21 20
0 1 1 1 1 1 1 1
Chapter 2 Types, Operators & Expressions
2.9 Bitwise Operator
Precedence:
~ > << = >> > & > ^ > |
Chapter 2 Types, Operators & Expressions
2.9 Bitwise Operator
x
p 0
n
x>>(p+1-n) xxx…………..xxx
5+5
Expression
Assignment Operators
We are not
the same
Amount
= Value
Writing operations
Reading
are assigned values
operations can
on the right side of
be constants,
the equals sign formulas
Initial Value Assignment of Variables
Initial Value Assignment of Variables
int iInt =
1314;
int type
iInt name
1314 constant
Examples of assignment operators
定义整型变量 i 、 j 、 k
定义整型常量 val
变量 = 常数
变量 = 表达式
变量 = 变量 = 变量 = 常量
不能赋值给常量
右值不能被赋值
Data Type Conversion
“Don't hold big things in small cups ”
char 自动转换的兼容顺序图
byte a = 8;
int b = a;
long c = b;
double d = c;
•Coercive
transformation
语法 ( 类型名 ) 要转换的值
int a = 100;
byte b =
(byte)a;
Accuracy Loss Caused by Forced
Conversion
Assignment Expressions:
<Var> <Assignment ops. > <Expressions>