1
Preparations
Data types
Constants and Variables
Type Conversions
Operators,and Expressions
Chapter 3 Data types,Operators,
and Expressions
2
Preparations
各种进制之间的转换
convert Binary,Octal,Hexadecimal into Decimal
100123452 592121202121211 1 1 0 1 1 )()(例
100128 94868381136 )()(例
10012316 7 9 7 81610162161516121 )()(例AF
< >
Chapter 3 Data types,Operators,and Expressions
3
方法:连续除以基,从低到高记录余数,直至商为 0
例 把十进制数 59转换成二进制数
592
292
142
72
32
12
0
(59)10=(111011)2
1
1
0
1
1
1
1 1 1 0 1 1
余余余余余余例 把十进制数 159转换成八进制数
1598
198
28
0
(159)10=(237)8
2 3 7
余 7
余 3
余 2
例 把十进制数 459转换成十六进制数
45916
2816
116
0
(459)10=(1CB)16
1 C B
余 11
余 12
余 1
Chapter 3 Data types,Operators,and Expressions
各种进制之间的转换
convert Binary,Octal,Hexadecimal into Decimal
4
Binary and Octal
Binary into Octal,从右向左,每 3位一组(不足 3位左补 0),转换成八进制
Octal into Binary,用 3位二进制数代替每一位八制数例 (1101001)2=(001,101,001)2=(151)8
例 (246)8=(010,100,110)2=(10100110)2
000 ~ 0
001 ~ 1
010 ~ 2
011 ~ 3
100 ~ 4
101 ~ 5
110 ~ 6
111 ~ 7
< >
Chapter 3 Data types,Operators,and Expressions
5
Binary and Hexadecimal
Binary into Hexadecimal,从右向左,每 4位一组(不足 4
位左补 0),转换成十六进制
Hexadecimal into Binary,用 4位二进制数代替每一位十六进制数例 (11010101111101)2=(0011,0101,0111,1101)2=(357D)16
例 (4B9E)16=(0100,1011,1001,1110)2=(100101110011110)2
0000 ~ 0
0001 ~ 1
0010 ~ 2
0011 ~ 3
0100 ~ 4
0101 ~ 5
0110 ~ 6
0111 ~ 7
1000 ~ 8
1001 ~ 9
1010 ~ A
1011 ~ B
1100 ~ C
1101 ~ D
1110 ~ E
1111 ~ F
< >
Chapter 3 Data types,Operators,and Expressions
6
Byte and Bit
Memory以 Byte为单元组成
每个 Byte有一个 Address
一个 Byte一般由 8个 Bit组成
每个 Bit的值是 0或 1
01234567
0
1
2
3
4
5
6
7
8
9
10 ……..
.
< >
7
6
4
3
2
5
1
Chapter 3 Data types,Operators,and Expressions
7
– 数值的表示方法 ——原码、反码和补码
原码:最高位为符号位,其余各位为数值本身的绝对值
反码:
– 正数:反码与原码相同
– 负数:符号位为 1,其余位对原码取反
补码:
– 负数:最高位为 1,其余位为原码取反,再对整个数加 1
– 正数:原码、反码、补码相同
<
原码 反码 补码
+7 00000111 00000111 00000111
-7 10000111 11111000 11111001
(用一字节表示数)
Chapter 3 Data types,Operators,and Expressions
8
3.1 Data types
< >
数据类型决定:
1,数据占内存字节数
2,数据取值范围
3,其上可进行的操作
Chapter 3 Data types,Operators,and Expressions
Data
Types
Constructed
types
pointers
void
Character types char
enum
integer
floating single precision floating point float
double precision floating point double
short int
long int
int
arrays
structures
unions
Basic
types
Arithmetic
types
9
Basic types
< >
Type Keywords Available range of valuesBits
integer
(signed)int 16 -32768~32767
(signed)short 16 -32768~32767
(signed)long 32 -2147483648~2147483647
16unsigned int 0~65535
32 0~4294967295unsigned long
unsigned short 16 0~65535
float 32 1e-37~1e38
double 64 1e-307~1e308
char 8
说明,数据类型所占位数随机器硬件不同而不同,上表以微机为例,
long double 80 1e-4931~1e4932
character
Chapter 3 Data types,Operators,and Expressions
10
3.2 Constants and Variables
– Identifiers (标志符 )
An identifier is a sequence of letters,digits and
underscore _ ;
The first character must be a letter or an underscore _;
Upper and lower case letters are different ;
Can not use keywords as variable names ;
It‘s wise to choose variable names that are related to the
purpose of the variable ;
Notice,such as l and I,o and 0……..例,判断下列标识符号合法性sum Sum M.D.John day Date 3days
student_name #33 lotus_1_2_3
char a>b _above $123
< >
M.D.J 3da
#33
$123a>b
Chapter 3 Data types,Operators,and Expressions
11
– Use upper case letters
– It is macro definition,not statements.
Direct constants
Integer constants
Floating-point constants
Character constants
String constants
如 #define PRICE 30
Constants
Symbolic constants,A #define line defines a symbolic
name or symbolic constant to be a particular string of characters,
#define identifier replacement text (constant)
>
>
>
>
< >
例 符号常量举例 (ch2_1.c)
#define PRICE 30
main()
{
int num,total;
num=10;
total=num*PRICE;
printf("total=%d",total);
}
运行结果,total=300
Chapter 3 Data types,Operators,and Expressions
12
Integer constants
Decimal integer,as in 123,-456,0……
Octal integer,a leading 0(zero) on an integer
constant means octal,as in 0123,011……
Hexadecimal integer,a leading 0x or 0X on an
integer constant means hexadecimal.as in 0x123,0Xff……
<
注,
A long constant is written with a terminal
l(ell) or L,as in 123L; unsigned constants are
written with a terminal u or U,and the suffix ul
or UL indicates unsigned long,as in 123u,0xful……
问题:
0123 = ( )10
0x123 = ( )10
0Xff = ( )10
83
291
255
问题:
0123 = ( )10
0x123 = ( )10
0Xff = ( )10
Chapter 3 Data types,Operators,and Expressions
例 12 与 12L例 30000 为 int型65536 为 long int 型
13
Floating-point constants
Decimal form:( must have decimal point) as in
0.123,.123,123.0,0.0,123,……
Exponential form:( must have digits before e or
E; exponent must be integer) as in
12.3e3,123E2,1.23e4,e-5,1.2E-3.5
<
注:
The type of floating-point constants is double
,unless suffixed.The suffixes f or F indicate a
float constant;l or L indicate a long double,
Chapter 3 Data types,Operators,and Expressions
14
Character constants
A character constant is an integer,written as
one character within single quotes;
The value of a character constant is the numeric
value of the character in the ASCⅡcharacter set ;
如 ‘ \101‘ -----------?A‘?\012‘ -----------‘\n‘
\376‘ -----------‘?‘?\x61‘ -----------‘a‘
\60‘ -----------‘0‘?\483‘ ----------(?)
例,
A‘-------‘\101‘-------‘\x41‘--------65
<
如 ‘ A‘——65,‘ a‘——97,
‘ 0‘——48,?\n‘——10
如 ‘ a’ ‘A’ ‘?’ ‘\n’ ‘\101’
Certain characters can be represented in character
and string constants by escape sequences like
\n(newline);
例 转义字符举例 (ch2_001.c,ch2_004.c)
main()
{
printf("\101 \x42 C\n");
printf("I say:\"How are you?\"\n");
printf("\\C Program\\\n");
printf("Turbo \'C\'");
}
运行结果,(屏幕显示 )
A B C
Isay:”How are you?”
\C Program\
Turbo ‘C’
例 main()
{ printf(―Y\b=\n‖);
}
运行结果:
屏幕显示,=
The complete set of escape sequences is:
vertical tab\v
hexadecimal number\xhhhorizontal tab\t
octal number\dddcarriage return\r
double quote\''new line \n
single quote\'formfeed\f
question mark\?backspace\b
backslash\\alert (bell) character \a
Chapter 3 Data types,Operators,and Expressions
15
<
注:
Character constants and string constants are different.
String constants
A sequence of zero or more characters surrounded
by double quotes ―‖;
The internal representation of a string has a
null character?\0‘ at the end.
h e l l o \0例 字符串,hello‖在内存中
a a \0例 ‘ a‘ ―a‖
例 空串,” \0
例,char ch;
ch=―A‖;
例,char ch;
ch=?A‘;
Chapter 3 Data types,Operators,and Expressions
16
编译程序根据变量定义为其分配指定字节的内存单元 …...
address
int a=1,b=-3,c;
a
b
c
2 bytes
2 bytes
2 bytes
address
address
…...
内存
1
-3
random
number
– Variables
Name and Value
Declarations,
data type var1 [,var2,…,var n] ;
< >
A variable may be initialized in its declaration;
例,
int a,b,c;
float data;
Determine allocated bytes
and the range of the value
valid identifiers
例,
int a=2,b,c=4;
float data=3.67;
char ch=?A‘;
int x=1,y=1,z=1;
int x=y=z=1;
All variables must be declared before use.
例 1
int student;
stadent=19; //Undefined symbol ‘statent’ in function main
例 2
float a,b,c;
c=a%b; // illegal use of floating point in function main
The declarations are generally at the beginning of the function.
main()
{ int a,b=2;
float data;
a=1;
data=(a+b)*1.2;
printf(―data=%f\n‖,data);
}
变量定义可执行语句
main()
{ int a,b=2;
a=1;
float data;
data=(a+b)*1.2;
printf(―data=%f\n‖,data);
}
3
a
name
value
unit
Chapter 3 Data types,Operators,and Expressions
17
Integer variables
Generally takes 2 bytes;
short≤ int≤ long
sizeof( type )
Floating variables
Float,takes 4 bytes,has 7 significant digits
Double,takes 8 bytes,has 15~16 significant digits
Character variables
Stores the numeric value of the character in the
ASCⅡ character set ;
Characters participate in numeric operations just
as any other integers.
例 float a;
a=111111.111; /* a=111111.1*/
double b;
b=111111.111; /* b=111111.111*/
例 a=?D‘; /* a=68; */
x=?A‘+5; /* x=65+5; */
s=?!‘+?G‘ /* s=33+71; */< > There is no string variables
Chapter 3 Data types,Operators,and Expressions
18
例 /*ch2_003.c*/
#define PRICE 12.5
main()
{ int num=3;
float total;
char ch1,ch2=?D‘;
total=num*PRICE;
ch1=ch2-?A‘+?a‘;
printf(―total=%f,ch1=%c\n‖,total,ch1);
}
macro definition
declarations of variables
output
运行结果:
total=37.500000,ch1=d
Chapter 3 Data types,Operators,and Expressions
19
3.3 Type conversions
– Implicit conversions (automatic)
If an operator that takes two operands(a
binary operator) has operands of different
types;
Conversions take place across assignment;
Printf converts its arguments under control
of the format,
Conversions also take place when arguments
are passed to functions,
< >
Chapter 3 Data types,Operators,and Expressions
20
double float
long
unsigned
int char,short低高说明,
Must convert
Convert when operands
are diffrent
例 char ch;
int i;
float f;
double d;
ch/i + f*d - (f+i)
int
int
double
double
double
double
double
double
int
int
double
double
double
double
double
double
10+?a‘ +i*f - d/l
例 int i;
float f;
double d;
long l;
< >
Chapter 3 Data types,Operators,and Expressions
21
– Explicit conversion( cast)
( type) ( expression)
例 (int)(x+y)
(int)x+y
(double)(3/2)
(int)3.6
Note that the cast produces the value of the
variable in the proper type,the variable
itself is not altered,例 main()
{ float x;
int i;
x=3.6;
i=(int)x;
printf(―x=%f,i=%d‖,x,i);
}
结果,x=3.600000,i=3
Precision lost
Higher types convert into lower types may…
< >
Chapter 3 Data types,Operators,and Expressions
22
3.4 Operators,and Expressions
< >
Chapter 3 Data types,Operators,and Expressions
operators:
Arithmetic operators,+ - * / % ++ --
Relational operators,< <= == > >= !=
Logical operators,! && ||
Bitwise operators,<< >> ~ | ^ &
Assighment operators,= += -=
Conditional operators,?,
Comma operators,,
Address operators,* &
Sizeof operators,sizeof
Casts,(type-name)
Structure member operators,,->
Subscript operators,[ ]
others,( ) -
23
You should notice when you study operators:
Function
Relationship with operands
– How many operands?
– Types of operands?
Precedence
Associativity
Type of result
< >
Chapter 3 Data types,Operators,and Expressions
24
Arithmetic Operators and Expressions
Arithmetic Operators,+ - * / %
– Associativity,left-to-right
– Precedence,- ---->* / % -----> + -
(1) (2) (3)
注,
– When - is unary operator right-to-left;
– The result of / must be integer;
– The operands of % must have integral type.例 5/2 =
-5/2 =
例 5%2 =
-5%2 =
1%10 =
5%1 =
5.5%2
< >
例 5/2 2
-5/2 = -2
(向零取整)
例 1
- -1
1
0
(?)
Chapter 3 Data types,Operators,and Expressions
25
Increment and decrement operators ++ --
– Adds 1 or subtracts 1 to its operand;
Prefix operators,++i,--i
(increment(or decrement) i before its value is used)
Postfix operators,i++,i--
(increment(or decrement) i after its value has been used)
例 j=3; k=++j;
j=3; k=j++;
j=3; printf(―%d‖,++j);
j=3; printf(―%d‖,j++);
a=3;b=5;c=(++a)*b;
a=3;b=5;c=(a++)*b;
< >
//k=4,j=4
//k=3,j=4
//4
//3
//c=20,a=4
//c=15,a=4
Chapter 3 Data types,Operators,and Expressions
26
注意,The increment and decrement operators can
only be applied to variables;expressions
and constants are illegal,(right-to-left)
如 8++ (x*y)++ wrong
j= -(++k); k=4 j= -4
j= -(k++); k=4 j= -3
例 k=3;
j= -++k;
j= -k++;
directions to using expressions
1,The sequence may be different in different
compiler.
例,i=3;
y=(i++)+(i++)
① y=6 i=5 y=3+3; i++; i++;
② y=7 i=5 y=3+4; i++;
Chapter 3 Data types,Operators,and Expressions
27
2.don’t make misunderstanding
例 1:i=3;j=1;x=i+++j;
例 3,i=3; printf(“%d,%d”,i,i++);
① x=i+(++j); x=5 i=3 j=2
② x=(i++)+j; x=4 i=4 j=1
例 2,i=3;j=1;x=i-++j ;
等价于 x=i-(++j); x=1 i=3 j=2
可能输出,① 3,3 ② 4,3
3.The order in which function arguments are
evaluated is right-to-left in normal system.
Chapter 3 Data types,Operators,and Expressions
28
Assignment Operators and Expressions
Simple assignment Operators
=
identifier = expression
Compound assignment Operators
+= -= *= /= %=,=,= &= ^= |=
exp1 op= exp2? exp1 = exp1 op exp2
a+=3 a=a+3
x*=y+8 x=x*(y+8)
x%=3 x=x%3
< >
例 a=3;
d=func();
c=d+2;
Chapter 3 Data types,Operators,and Expressions
29
注:
Associativity,right-to-left
Precedence,14
Left operands must be variables,can not be constants and
expressions
Assignment expressions can be nested
例 3=x-2*y;
a+b=3;
例 float f;
int i;
i=10;
f=i;
则 f=10.0
例 int i;
i=2.56; //结果 i=2;
例,a=b=c=5
a=(b=5)
a=5+(c=6)
a=(b=4)+(c=6)
a=(b=10)/(c=2)
< >
//表达式值为 5,a,b,c值为 5
// b=5;a=5
//表达式值 11,c=6,a=11
//表达式值 10,a=10,b=4,c=6
//表达式值 5,a=5,b=10,c=2
Chapter 3 Data types,Operators,and Expressions
Automatic conversions take place across assignment
例,a=12;
a+=a-=a*a
例,int a=2;
a%=4-1;
a+=a*=a-=a*=3;
//a=-264 等价于 a=a+(a=a-(a*a))
//a=0 等价于 a=a+(a=a*(a=a-(a=a*3)))
30
Comma Operators and Expressions
expression1,expression2,…… expression n
Associativity,left-to-right
Precedence,15
The value of the result are the value of the expression n
例 a=3*5,a*4
a=3*5,a*4,a+5
例 x=(a=3,6*3)
x=a=3,6*a
例 a=1;b=2;c=3;
printf(―%d,%d,%d‖,a,b,c);
printf(―%d,%d,%d‖,(a,b,c),b,c);
< >
//a=15,表达式值 60
//a=15,表达式值 20
//赋值表达式,表达式值 18,x=18
//逗号表达式,表达式值 18,x=3
//1,2,3
//3,2,3
Chapter 3 Data types,Operators,and Expressions
例,
/*ch2_6.c*/
#include <stdio.h>
main()
{ int x,y=7;
float z=4;
x=(y=y+6,y/z);
printf("x=%d\n",x);
}
运行结果,x=3
31
Relational Operators and Expressions
< <= == >= > !=
Associativity,left-to-right
Precedence:
<
<=
>
>=
==
!=
Precedence,6(高)
Precedence,7(低)例 c>a+b //c>(a+b)
a>b!=c //(a>b)!=c
a==b<c //a==(b<c)
a=b>c //a=(b>c)
The results of relational expressions are represented
by 1 (true) or 0 (false)
例 int a=3,b=2,c=1,d,f;
a>b
(a>b)==c
b+c<a
d=a>b
f=a>b>c< >
//表达式值 1
//表达式值 1
//表达式值 0
//d=1
//f=0
Chapter 3 Data types,Operators,and Expressions
32
Notice:
< >
例 若 a=0; b=0.5; x=0.3;
则 a<=x<=b的值为 0
例 5>2>7>8在 C中是允许的,
值为 0
例 int i=1,j=7,a;
a=i+(j%4!=0);
则 a= 2
例 ‘ a‘>0 结果为
‘ A‘>100 结果为
1
0
Chapter 3 Data types,Operators,and Expressions
33
Notice:
例 注意区分,=‖与,==‖
int a=0,b=1;
if(a=b)
printf(―a equal to b‖);
else
printf(―a not equal to b‖);
例 应避免对 实数 作相等或不等的判断如 1.0/3.0*3.0==1.0 结果为可改写为,fabs(1.0/3.0*3.0-1.0)<1e-6
0
Chapter 3 Data types,Operators,and Expressions
34
a b !a !b a&&b a||b
T
F
T
F
FF
T
T
Logical Operators and Expressions
! && ||
Operands,0 represented,false”
!0 represented,true”
Results,0 represented,false”
1 represented,true”
< >
T
F
F
F
F
F
T
T
F
F
T
T
T
F
T
T
Chapter 3 Data types,Operators,and Expressions
35
! (2)
&& (11)
|| (12)
高低例 a<=x && x<=b
a>b&&x>y
a==b||x==y
!a||a>b
Precedence:
Associativity:
!,right-to-left
&&,left-to-right
||,left-to-right
< >
// (a<=x) && (x<=b)
//(a>b)&&(x>y)
//(a==b)||(x==y)
//(!a)||(a>b)
Chapter 3 Data types,Operators,and Expressions
例 a=4;b=5;
!a
a&&b
a||b
!a||b
4&&0||2
5>3&&2||8<4-!0
c‘&&?d‘
//值为 1
//值为 0
//值为 1
//值为 1
//值为 1
//值为 1
//(5>3)&&2||(8<(4-(!0))) 值为 1
36
! (2)
&& (11)
|| (12)
高低
Precedence:
Associativity:
!,right-to-left
&&,left-to-right
||,left-to-right
短路特性,
< >
例 a&&b&&c //只在 a为真时,才判别 b的值;
只在 a,b都为真时,才判别 c的值例 a||b||c //只在 a为假时,才判别 b的值;
只在 a,b都为假时,才判别 c的值例 a=1;b=2;c=3;d=4;m=1;n=1;
(m=a>b)&&(n=c>d) //结果 m=0,n=1
Chapter 3 Data types,Operators,and Expressions
37
例 if (a>b)
printf(“%d”,a);
else
printf(“%d”,b);
printf(“%d”,a>b?a:b);
例 求 a+|b|
printf(“a+|b|=%d\n”,b>0?a+b:a-b);
expr1
取 expr2值 取 expr3值非 0 =0
例 (a==b)Y?:?N?
(x%2==1)?1:0
(x>=0)?x:-x
(c>=?a? && c<=?z?)?c-?a?+?A?:c
Conditional operators can be nested;
如 x>0?1:(x<0?-1:0)
Precedence,13
Associativity,right-to-left
如 a>b?a:c>d?c:d? a>b?a:(c>d?c:d)
expr1,expr2,expr3 may be different types,the value
of the expression takes the higher type.
< >
例 xa?:?b? //x=0,表达式值为‘ b?; x?0,表达式值为‘ a?
x>y?1:1.5 //x>y,值为 1.0; x<y,值为 1.5
Chapter 3 Data types,Operators,and Expressions
Conditional Operators and Expressions
expr1? expr2,expr3
like if statements
Preparations
Data types
Constants and Variables
Type Conversions
Operators,and Expressions
Chapter 3 Data types,Operators,
and Expressions
2
Preparations
各种进制之间的转换
convert Binary,Octal,Hexadecimal into Decimal
100123452 592121202121211 1 1 0 1 1 )()(例
100128 94868381136 )()(例
10012316 7 9 7 81610162161516121 )()(例AF
< >
Chapter 3 Data types,Operators,and Expressions
3
方法:连续除以基,从低到高记录余数,直至商为 0
例 把十进制数 59转换成二进制数
592
292
142
72
32
12
0
(59)10=(111011)2
1
1
0
1
1
1
1 1 1 0 1 1
余余余余余余例 把十进制数 159转换成八进制数
1598
198
28
0
(159)10=(237)8
2 3 7
余 7
余 3
余 2
例 把十进制数 459转换成十六进制数
45916
2816
116
0
(459)10=(1CB)16
1 C B
余 11
余 12
余 1
Chapter 3 Data types,Operators,and Expressions
各种进制之间的转换
convert Binary,Octal,Hexadecimal into Decimal
4
Binary and Octal
Binary into Octal,从右向左,每 3位一组(不足 3位左补 0),转换成八进制
Octal into Binary,用 3位二进制数代替每一位八制数例 (1101001)2=(001,101,001)2=(151)8
例 (246)8=(010,100,110)2=(10100110)2
000 ~ 0
001 ~ 1
010 ~ 2
011 ~ 3
100 ~ 4
101 ~ 5
110 ~ 6
111 ~ 7
< >
Chapter 3 Data types,Operators,and Expressions
5
Binary and Hexadecimal
Binary into Hexadecimal,从右向左,每 4位一组(不足 4
位左补 0),转换成十六进制
Hexadecimal into Binary,用 4位二进制数代替每一位十六进制数例 (11010101111101)2=(0011,0101,0111,1101)2=(357D)16
例 (4B9E)16=(0100,1011,1001,1110)2=(100101110011110)2
0000 ~ 0
0001 ~ 1
0010 ~ 2
0011 ~ 3
0100 ~ 4
0101 ~ 5
0110 ~ 6
0111 ~ 7
1000 ~ 8
1001 ~ 9
1010 ~ A
1011 ~ B
1100 ~ C
1101 ~ D
1110 ~ E
1111 ~ F
< >
Chapter 3 Data types,Operators,and Expressions
6
Byte and Bit
Memory以 Byte为单元组成
每个 Byte有一个 Address
一个 Byte一般由 8个 Bit组成
每个 Bit的值是 0或 1
01234567
0
1
2
3
4
5
6
7
8
9
10 ……..
.
< >
7
6
4
3
2
5
1
Chapter 3 Data types,Operators,and Expressions
7
– 数值的表示方法 ——原码、反码和补码
原码:最高位为符号位,其余各位为数值本身的绝对值
反码:
– 正数:反码与原码相同
– 负数:符号位为 1,其余位对原码取反
补码:
– 负数:最高位为 1,其余位为原码取反,再对整个数加 1
– 正数:原码、反码、补码相同
<
原码 反码 补码
+7 00000111 00000111 00000111
-7 10000111 11111000 11111001
(用一字节表示数)
Chapter 3 Data types,Operators,and Expressions
8
3.1 Data types
< >
数据类型决定:
1,数据占内存字节数
2,数据取值范围
3,其上可进行的操作
Chapter 3 Data types,Operators,and Expressions
Data
Types
Constructed
types
pointers
void
Character types char
enum
integer
floating single precision floating point float
double precision floating point double
short int
long int
int
arrays
structures
unions
Basic
types
Arithmetic
types
9
Basic types
< >
Type Keywords Available range of valuesBits
integer
(signed)int 16 -32768~32767
(signed)short 16 -32768~32767
(signed)long 32 -2147483648~2147483647
16unsigned int 0~65535
32 0~4294967295unsigned long
unsigned short 16 0~65535
float 32 1e-37~1e38
double 64 1e-307~1e308
char 8
说明,数据类型所占位数随机器硬件不同而不同,上表以微机为例,
long double 80 1e-4931~1e4932
character
Chapter 3 Data types,Operators,and Expressions
10
3.2 Constants and Variables
– Identifiers (标志符 )
An identifier is a sequence of letters,digits and
underscore _ ;
The first character must be a letter or an underscore _;
Upper and lower case letters are different ;
Can not use keywords as variable names ;
It‘s wise to choose variable names that are related to the
purpose of the variable ;
Notice,such as l and I,o and 0……..例,判断下列标识符号合法性sum Sum M.D.John day Date 3days
student_name #33 lotus_1_2_3
char a>b _above $123
< >
M.D.J 3da
#33
$123a>b
Chapter 3 Data types,Operators,and Expressions
11
– Use upper case letters
– It is macro definition,not statements.
Direct constants
Integer constants
Floating-point constants
Character constants
String constants
如 #define PRICE 30
Constants
Symbolic constants,A #define line defines a symbolic
name or symbolic constant to be a particular string of characters,
#define identifier replacement text (constant)
>
>
>
>
< >
例 符号常量举例 (ch2_1.c)
#define PRICE 30
main()
{
int num,total;
num=10;
total=num*PRICE;
printf("total=%d",total);
}
运行结果,total=300
Chapter 3 Data types,Operators,and Expressions
12
Integer constants
Decimal integer,as in 123,-456,0……
Octal integer,a leading 0(zero) on an integer
constant means octal,as in 0123,011……
Hexadecimal integer,a leading 0x or 0X on an
integer constant means hexadecimal.as in 0x123,0Xff……
<
注,
A long constant is written with a terminal
l(ell) or L,as in 123L; unsigned constants are
written with a terminal u or U,and the suffix ul
or UL indicates unsigned long,as in 123u,0xful……
问题:
0123 = ( )10
0x123 = ( )10
0Xff = ( )10
83
291
255
问题:
0123 = ( )10
0x123 = ( )10
0Xff = ( )10
Chapter 3 Data types,Operators,and Expressions
例 12 与 12L例 30000 为 int型65536 为 long int 型
13
Floating-point constants
Decimal form:( must have decimal point) as in
0.123,.123,123.0,0.0,123,……
Exponential form:( must have digits before e or
E; exponent must be integer) as in
12.3e3,123E2,1.23e4,e-5,1.2E-3.5
<
注:
The type of floating-point constants is double
,unless suffixed.The suffixes f or F indicate a
float constant;l or L indicate a long double,
Chapter 3 Data types,Operators,and Expressions
14
Character constants
A character constant is an integer,written as
one character within single quotes;
The value of a character constant is the numeric
value of the character in the ASCⅡcharacter set ;
如 ‘ \101‘ -----------?A‘?\012‘ -----------‘\n‘
\376‘ -----------‘?‘?\x61‘ -----------‘a‘
\60‘ -----------‘0‘?\483‘ ----------(?)
例,
A‘-------‘\101‘-------‘\x41‘--------65
<
如 ‘ A‘——65,‘ a‘——97,
‘ 0‘——48,?\n‘——10
如 ‘ a’ ‘A’ ‘?’ ‘\n’ ‘\101’
Certain characters can be represented in character
and string constants by escape sequences like
\n(newline);
例 转义字符举例 (ch2_001.c,ch2_004.c)
main()
{
printf("\101 \x42 C\n");
printf("I say:\"How are you?\"\n");
printf("\\C Program\\\n");
printf("Turbo \'C\'");
}
运行结果,(屏幕显示 )
A B C
Isay:”How are you?”
\C Program\
Turbo ‘C’
例 main()
{ printf(―Y\b=\n‖);
}
运行结果:
屏幕显示,=
The complete set of escape sequences is:
vertical tab\v
hexadecimal number\xhhhorizontal tab\t
octal number\dddcarriage return\r
double quote\''new line \n
single quote\'formfeed\f
question mark\?backspace\b
backslash\\alert (bell) character \a
Chapter 3 Data types,Operators,and Expressions
15
<
注:
Character constants and string constants are different.
String constants
A sequence of zero or more characters surrounded
by double quotes ―‖;
The internal representation of a string has a
null character?\0‘ at the end.
h e l l o \0例 字符串,hello‖在内存中
a a \0例 ‘ a‘ ―a‖
例 空串,” \0
例,char ch;
ch=―A‖;
例,char ch;
ch=?A‘;
Chapter 3 Data types,Operators,and Expressions
16
编译程序根据变量定义为其分配指定字节的内存单元 …...
address
int a=1,b=-3,c;
a
b
c
2 bytes
2 bytes
2 bytes
address
address
…...
内存
1
-3
random
number
– Variables
Name and Value
Declarations,
data type var1 [,var2,…,var n] ;
< >
A variable may be initialized in its declaration;
例,
int a,b,c;
float data;
Determine allocated bytes
and the range of the value
valid identifiers
例,
int a=2,b,c=4;
float data=3.67;
char ch=?A‘;
int x=1,y=1,z=1;
int x=y=z=1;
All variables must be declared before use.
例 1
int student;
stadent=19; //Undefined symbol ‘statent’ in function main
例 2
float a,b,c;
c=a%b; // illegal use of floating point in function main
The declarations are generally at the beginning of the function.
main()
{ int a,b=2;
float data;
a=1;
data=(a+b)*1.2;
printf(―data=%f\n‖,data);
}
变量定义可执行语句
main()
{ int a,b=2;
a=1;
float data;
data=(a+b)*1.2;
printf(―data=%f\n‖,data);
}
3
a
name
value
unit
Chapter 3 Data types,Operators,and Expressions
17
Integer variables
Generally takes 2 bytes;
short≤ int≤ long
sizeof( type )
Floating variables
Float,takes 4 bytes,has 7 significant digits
Double,takes 8 bytes,has 15~16 significant digits
Character variables
Stores the numeric value of the character in the
ASCⅡ character set ;
Characters participate in numeric operations just
as any other integers.
例 float a;
a=111111.111; /* a=111111.1*/
double b;
b=111111.111; /* b=111111.111*/
例 a=?D‘; /* a=68; */
x=?A‘+5; /* x=65+5; */
s=?!‘+?G‘ /* s=33+71; */< > There is no string variables
Chapter 3 Data types,Operators,and Expressions
18
例 /*ch2_003.c*/
#define PRICE 12.5
main()
{ int num=3;
float total;
char ch1,ch2=?D‘;
total=num*PRICE;
ch1=ch2-?A‘+?a‘;
printf(―total=%f,ch1=%c\n‖,total,ch1);
}
macro definition
declarations of variables
output
运行结果:
total=37.500000,ch1=d
Chapter 3 Data types,Operators,and Expressions
19
3.3 Type conversions
– Implicit conversions (automatic)
If an operator that takes two operands(a
binary operator) has operands of different
types;
Conversions take place across assignment;
Printf converts its arguments under control
of the format,
Conversions also take place when arguments
are passed to functions,
< >
Chapter 3 Data types,Operators,and Expressions
20
double float
long
unsigned
int char,short低高说明,
Must convert
Convert when operands
are diffrent
例 char ch;
int i;
float f;
double d;
ch/i + f*d - (f+i)
int
int
double
double
double
double
double
double
int
int
double
double
double
double
double
double
10+?a‘ +i*f - d/l
例 int i;
float f;
double d;
long l;
< >
Chapter 3 Data types,Operators,and Expressions
21
– Explicit conversion( cast)
( type) ( expression)
例 (int)(x+y)
(int)x+y
(double)(3/2)
(int)3.6
Note that the cast produces the value of the
variable in the proper type,the variable
itself is not altered,例 main()
{ float x;
int i;
x=3.6;
i=(int)x;
printf(―x=%f,i=%d‖,x,i);
}
结果,x=3.600000,i=3
Precision lost
Higher types convert into lower types may…
< >
Chapter 3 Data types,Operators,and Expressions
22
3.4 Operators,and Expressions
< >
Chapter 3 Data types,Operators,and Expressions
operators:
Arithmetic operators,+ - * / % ++ --
Relational operators,< <= == > >= !=
Logical operators,! && ||
Bitwise operators,<< >> ~ | ^ &
Assighment operators,= += -=
Conditional operators,?,
Comma operators,,
Address operators,* &
Sizeof operators,sizeof
Casts,(type-name)
Structure member operators,,->
Subscript operators,[ ]
others,( ) -
23
You should notice when you study operators:
Function
Relationship with operands
– How many operands?
– Types of operands?
Precedence
Associativity
Type of result
< >
Chapter 3 Data types,Operators,and Expressions
24
Arithmetic Operators and Expressions
Arithmetic Operators,+ - * / %
– Associativity,left-to-right
– Precedence,- ---->* / % -----> + -
(1) (2) (3)
注,
– When - is unary operator right-to-left;
– The result of / must be integer;
– The operands of % must have integral type.例 5/2 =
-5/2 =
例 5%2 =
-5%2 =
1%10 =
5%1 =
5.5%2
< >
例 5/2 2
-5/2 = -2
(向零取整)
例 1
- -1
1
0
(?)
Chapter 3 Data types,Operators,and Expressions
25
Increment and decrement operators ++ --
– Adds 1 or subtracts 1 to its operand;
Prefix operators,++i,--i
(increment(or decrement) i before its value is used)
Postfix operators,i++,i--
(increment(or decrement) i after its value has been used)
例 j=3; k=++j;
j=3; k=j++;
j=3; printf(―%d‖,++j);
j=3; printf(―%d‖,j++);
a=3;b=5;c=(++a)*b;
a=3;b=5;c=(a++)*b;
< >
//k=4,j=4
//k=3,j=4
//4
//3
//c=20,a=4
//c=15,a=4
Chapter 3 Data types,Operators,and Expressions
26
注意,The increment and decrement operators can
only be applied to variables;expressions
and constants are illegal,(right-to-left)
如 8++ (x*y)++ wrong
j= -(++k); k=4 j= -4
j= -(k++); k=4 j= -3
例 k=3;
j= -++k;
j= -k++;
directions to using expressions
1,The sequence may be different in different
compiler.
例,i=3;
y=(i++)+(i++)
① y=6 i=5 y=3+3; i++; i++;
② y=7 i=5 y=3+4; i++;
Chapter 3 Data types,Operators,and Expressions
27
2.don’t make misunderstanding
例 1:i=3;j=1;x=i+++j;
例 3,i=3; printf(“%d,%d”,i,i++);
① x=i+(++j); x=5 i=3 j=2
② x=(i++)+j; x=4 i=4 j=1
例 2,i=3;j=1;x=i-++j ;
等价于 x=i-(++j); x=1 i=3 j=2
可能输出,① 3,3 ② 4,3
3.The order in which function arguments are
evaluated is right-to-left in normal system.
Chapter 3 Data types,Operators,and Expressions
28
Assignment Operators and Expressions
Simple assignment Operators
=
identifier = expression
Compound assignment Operators
+= -= *= /= %=,=,= &= ^= |=
exp1 op= exp2? exp1 = exp1 op exp2
a+=3 a=a+3
x*=y+8 x=x*(y+8)
x%=3 x=x%3
< >
例 a=3;
d=func();
c=d+2;
Chapter 3 Data types,Operators,and Expressions
29
注:
Associativity,right-to-left
Precedence,14
Left operands must be variables,can not be constants and
expressions
Assignment expressions can be nested
例 3=x-2*y;
a+b=3;
例 float f;
int i;
i=10;
f=i;
则 f=10.0
例 int i;
i=2.56; //结果 i=2;
例,a=b=c=5
a=(b=5)
a=5+(c=6)
a=(b=4)+(c=6)
a=(b=10)/(c=2)
< >
//表达式值为 5,a,b,c值为 5
// b=5;a=5
//表达式值 11,c=6,a=11
//表达式值 10,a=10,b=4,c=6
//表达式值 5,a=5,b=10,c=2
Chapter 3 Data types,Operators,and Expressions
Automatic conversions take place across assignment
例,a=12;
a+=a-=a*a
例,int a=2;
a%=4-1;
a+=a*=a-=a*=3;
//a=-264 等价于 a=a+(a=a-(a*a))
//a=0 等价于 a=a+(a=a*(a=a-(a=a*3)))
30
Comma Operators and Expressions
expression1,expression2,…… expression n
Associativity,left-to-right
Precedence,15
The value of the result are the value of the expression n
例 a=3*5,a*4
a=3*5,a*4,a+5
例 x=(a=3,6*3)
x=a=3,6*a
例 a=1;b=2;c=3;
printf(―%d,%d,%d‖,a,b,c);
printf(―%d,%d,%d‖,(a,b,c),b,c);
< >
//a=15,表达式值 60
//a=15,表达式值 20
//赋值表达式,表达式值 18,x=18
//逗号表达式,表达式值 18,x=3
//1,2,3
//3,2,3
Chapter 3 Data types,Operators,and Expressions
例,
/*ch2_6.c*/
#include <stdio.h>
main()
{ int x,y=7;
float z=4;
x=(y=y+6,y/z);
printf("x=%d\n",x);
}
运行结果,x=3
31
Relational Operators and Expressions
< <= == >= > !=
Associativity,left-to-right
Precedence:
<
<=
>
>=
==
!=
Precedence,6(高)
Precedence,7(低)例 c>a+b //c>(a+b)
a>b!=c //(a>b)!=c
a==b<c //a==(b<c)
a=b>c //a=(b>c)
The results of relational expressions are represented
by 1 (true) or 0 (false)
例 int a=3,b=2,c=1,d,f;
a>b
(a>b)==c
b+c<a
d=a>b
f=a>b>c< >
//表达式值 1
//表达式值 1
//表达式值 0
//d=1
//f=0
Chapter 3 Data types,Operators,and Expressions
32
Notice:
< >
例 若 a=0; b=0.5; x=0.3;
则 a<=x<=b的值为 0
例 5>2>7>8在 C中是允许的,
值为 0
例 int i=1,j=7,a;
a=i+(j%4!=0);
则 a= 2
例 ‘ a‘>0 结果为
‘ A‘>100 结果为
1
0
Chapter 3 Data types,Operators,and Expressions
33
Notice:
例 注意区分,=‖与,==‖
int a=0,b=1;
if(a=b)
printf(―a equal to b‖);
else
printf(―a not equal to b‖);
例 应避免对 实数 作相等或不等的判断如 1.0/3.0*3.0==1.0 结果为可改写为,fabs(1.0/3.0*3.0-1.0)<1e-6
0
Chapter 3 Data types,Operators,and Expressions
34
a b !a !b a&&b a||b
T
F
T
F
FF
T
T
Logical Operators and Expressions
! && ||
Operands,0 represented,false”
!0 represented,true”
Results,0 represented,false”
1 represented,true”
< >
T
F
F
F
F
F
T
T
F
F
T
T
T
F
T
T
Chapter 3 Data types,Operators,and Expressions
35
! (2)
&& (11)
|| (12)
高低例 a<=x && x<=b
a>b&&x>y
a==b||x==y
!a||a>b
Precedence:
Associativity:
!,right-to-left
&&,left-to-right
||,left-to-right
< >
// (a<=x) && (x<=b)
//(a>b)&&(x>y)
//(a==b)||(x==y)
//(!a)||(a>b)
Chapter 3 Data types,Operators,and Expressions
例 a=4;b=5;
!a
a&&b
a||b
!a||b
4&&0||2
5>3&&2||8<4-!0
c‘&&?d‘
//值为 1
//值为 0
//值为 1
//值为 1
//值为 1
//值为 1
//(5>3)&&2||(8<(4-(!0))) 值为 1
36
! (2)
&& (11)
|| (12)
高低
Precedence:
Associativity:
!,right-to-left
&&,left-to-right
||,left-to-right
短路特性,
< >
例 a&&b&&c //只在 a为真时,才判别 b的值;
只在 a,b都为真时,才判别 c的值例 a||b||c //只在 a为假时,才判别 b的值;
只在 a,b都为假时,才判别 c的值例 a=1;b=2;c=3;d=4;m=1;n=1;
(m=a>b)&&(n=c>d) //结果 m=0,n=1
Chapter 3 Data types,Operators,and Expressions
37
例 if (a>b)
printf(“%d”,a);
else
printf(“%d”,b);
printf(“%d”,a>b?a:b);
例 求 a+|b|
printf(“a+|b|=%d\n”,b>0?a+b:a-b);
expr1
取 expr2值 取 expr3值非 0 =0
例 (a==b)Y?:?N?
(x%2==1)?1:0
(x>=0)?x:-x
(c>=?a? && c<=?z?)?c-?a?+?A?:c
Conditional operators can be nested;
如 x>0?1:(x<0?-1:0)
Precedence,13
Associativity,right-to-left
如 a>b?a:c>d?c:d? a>b?a:(c>d?c:d)
expr1,expr2,expr3 may be different types,the value
of the expression takes the higher type.
< >
例 xa?:?b? //x=0,表达式值为‘ b?; x?0,表达式值为‘ a?
x>y?1:1.5 //x>y,值为 1.0; x<y,值为 1.5
Chapter 3 Data types,Operators,and Expressions
Conditional Operators and Expressions
expr1? expr2,expr3
like if statements