chapter02(1)
chapter02(1)
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:”;
>
>=
<
<=
==
!=
Chapter 2 Types, Operators & Expressions
2.6 Relational and Logical Operators
Relational expression
Examples:
#include<stdio.h>
int main (void)
{
int a=1>1+2;
printf(“%d\n”,a);
return 0;
}
Chapter 2 Types, Operators & Expressions
2.6 Relational and Logical Operators
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
(3) 4&&0||2 1
(4) ‘c’ && ‘d’ 1 (because ‘c’ and ‘d’ are both nonzero)
(5) 5 > 3 && 2 || 8 < 4 - !0
5>3 && 2 || 8 < 1
5>3 && 2 || 0
1 && 2 || 0
1 || 0
1
Chapter 2 Types, Operators & Expressions
2.6 Relational and Logical Operators
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,
float
(3) int 107 + the result of i*f ,
float
(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
int main()
{
char str[] = "123";
int a;
a = atoi(str); // 将字符串转化为 int 型的数字
printf("%d\n", a);
return 0;
}
Chapter 2 Types, Operators & Expressions
2.7 Type Conversion
Example 3:
/* Tolower: convert c to lower case; ASCII only */
#include <stdio.h>
int main()
{
printf("tolower('0')=%c\n", tolower('0'));
printf("tolower('a')=%c\n", tolower('a'));
printf("tolower('A')=%c\n", tolower('A'));
return 0;
}
Example:
(double)a a’ value is converted the double type;
(int)(x+y) convert the value of x+y to int type;
(float)(5%3) convert the value of 5%3 to float type.
Chapter 2 Types, Operators & Expressions
2.7 Type Conversion
When explicit type converting, a
#include<stdio.h>medial variable is generated , and
original variable’s type is not converted
int main()
{ actually.
float x;
int i;
x = 3.6;
i = (int)x;
printf("x=%f,i=%d\n", x, i);
return 0;
}
x = 3.600000, i = 3
what is the output of the program?
Chapter 2 Types, Operators & Expressions
2.8 Increment and Decrement Operators
Examples2:
#include<stdio.h>
int main()
{
int a=1;
int b=a--;
printf(“a=%d\n”,a);
printf(“b=%d\n”,b);
return 0; Examples2:
} #include<stdio.h>
int main()
{
int a=1;
int b=--a;
printf(“a=%d\n”,a);
printf(“b=%d\n”,b);
return 0;
}
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 1001
9&5=
& 0000 0101 =1
0000 0001
Chapter 2 Types, Operators & Expressions
2.9 Bitwise Operator
Bitwise AND | :
Example2:
0000 1001
9|5=
| 0000 0101 =1
0000 1101
2.10 Assignment Operators and
Expressions
Expression
Expression : An expression is a
sequence of operators and opera
nds that specifies computation o
f a value Expression
5+5
Assignment Operators
We are not
the same
#include<stdio.h>
Assignment Operators int main()
{
1. variable = expression int a;
a=1+1 int b;
int c;
2. variable = constant a = 20;
a= 2 b = a - 10;
c = a + b;
3. variable = variable printf("a=%d\n", a);
a= b printf("b=%d\n", b);
printf("c=%d\n", c);
return 0;
}
Initial Value Assignment of Variables
Initial Value Assignment of Variables
int a = 1314;
int type
a name
13 constant
Examples of assignment operators
定义整型变量 i 、 j 、 k
定义整型常量 val
变量 = 常数
变量 = 表达式
变量 = 变量 = 变量 = 常量
不能赋值给常量
右值不能被赋值
Chapter 2 Types, Operators & Expressions
2.10 Assignment Operators and Expressions
Assignment Expressions:
a=1+1; a=2; a= a+1; a=b.
#include<stdio.h>
int main()
{
int a;
int b;
a = 20;
printf("a=10 the result is:%d\n", a=10);
printf("b=20+30 the result is:%d\n", b=20+30);
return 0;
}
Chapter 2 Types, Operators & Expressions
2.10 Assignment Operators and Expressions
#include<stdio.h>
int main()
{
int a=1;
int b=2;
a+= b*20;
b%= a + 10;
printf("a=%d\n", a);
printf("b=%d\n", b);
return 0;
}
max=a>b?a:b
max=(a>b?a:b)
Chapter 2 Types, Operators & Expressions
2.10 Assignment Operators and Expressions
#include<stdio.h>
int main()
{
int a=9;
int b=0;
b = (a > 10 ? 888 : 666);
printf("b=%d\n", b);
b= (a > 5 ? 888 : 666);
printf("b=%d\n", b);
return 0;
}
Chapter 2 Types, Operators & Expressions
2.12 Precedence and Order of Evaluation