1.2 The feature of C
– C is terse,it has fewer keywords
– C has a lot of operators and data types
– C is structured and modular.
– C is the basis for C++ and Java
– C is portable.Code written on one machine can
be easily moved to another.
< >
>
>
>>
The C Programming Language Chapter 1 An Overview of C
32 keywords,(由系统定义,不能重作其它定义 )
auto break case char const
continue default do double else
enum extern float for goto
if int long register return
short signed sizeof static struct
switch typedef unsigned union void
volatile while
<
The C Programming Language Chapter 1 An Overview of C
34 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,( ) -
<
The C Programming Language Chapter 1 An Overview of C
1.3The pattern of a C program
Exampl 1.1 the first c program Hello,World!
/* example1.1 The first C Program*/
#include <stdio.h>
main()
{
printf(“Hello,World!”);
}
>
notation
Include information
about standard library
define a function named main
statement
output:
Hello,World!
The C Programming Language Chapter 1 An Overview of C
exp1.2 /* example1.1 calculate the sum of a and b*/
#include <stdio.h>
/* This is the main program */
main()
{ int a,b,sum;
a=10;
b=24;
sum=add(a,b);
printf(”sum=%d\n",sum);
}
/* This function calculates the sum of x and y */
int add(int x,int y)
{ int z;
z=x+y;
return(z);
}
运行结果:
sum=34
函数语句预处理命令注释
>
The C Programming Language Chapter 1 An Overview of C
– 格式特点
习惯用 小写 字母,大小写敏感
不使用行号,无程序行 概念
可使用空行和空格
常用 锯齿形 书写格式
< >
main( )
{ ……………….
………………
…………..
…………..
………
……….
……………
…………….
……………….
………………..
}
main( )
{
int i,j,sum;
sum=0;
for(i=1; i<10;i++)
{
for(j=1;j<10;j++)
{
sum+=i*j ;
}
}
printf(“%d\n”,sum);
}
优秀程序员的素质之一,
使用 TAB缩进
{}对齐
有足够的注释
有合适的空行
The C Programming Language Chapter 1 An Overview of C
– 结构特点
函数与主函数
– 程序由一个或多个函数组成
– 必须有且只能有一个主函数 main()
– 程序执行从 main开始,在 main中结束,其它函数通过嵌套调用得以执行。
程序语句
– C程序由语句组成
– 用,;”作为语句终止符
注释
– /* */为注释,不能嵌套
– 不产生编译代码
< >
例,/*This is the main /* of example1.1*/
*/
非法编译预处理命令
The C Programming Language Chapter 1 An Overview of C
编辑链接编译执行
§ 1.4 C程序的上机步骤
C程序开发步骤
< >
开 始编 辑编 译连 接执 行有错?
结果正确?
结 束有源程序
f i l e,c
目标程序
f i l e,obj
库函数和其它目标程序可执行目标程序无正确不正确
file.exe
程序代码的录入,
生成源程序 *.c
语法分析查错,翻译生成目标程序 *.obj
与其它目标程序或库链接装配,生成可执行程序 *.exe
′ 3ì Dò ±ê 3ì Dò?é?′ D 3ì Dò
ú èY 3ì Dò éè óú óú ó
é?′ D 2é ò? 2é òé ò?
t oó ×,c,o b j,e x e
The C Programming Language Chapter 1 An Overview of C
第二章 算法 algorithm
2.1 算法的概念
2.2 简单算法举例
2.3 算法的特性
2.4 算法的表示
2.5 结构化程序设计方法
2.1 算法的概念程序 =数据结构 +算法对数据的描述对操作的描述算法分类:
数值运算算法 ---- 用于求数值解(如:求方程的根 … )
非数值运算算法 ---- 多用于管理领域(如:图书管理、人事管理 … )
例:求 1+2+3+…+100=?
1,1+2,再加 3,再加 4….,最后加 100,等于 5050
2,100+( 1+99) +( 2+98) +…+ ( 49+51) +50=100+49*100+50=5050
2.2 简单算法举例例:求两个数的和
step1:给定两个数的值
step2:做加法运算
step3:将结果保存
step4:输出结果
step1,2? x,3?y
step2,x+y ( 2+3)
step3,5? z
step4:输出 z
#include <stdio.h>
void main( )
{ int x,y,z;
x=2; y=3;
z=x+y;
printf(“z=%d\n”,z);
printf(“%d+%d=%d\n”,x,y,z);
}
输出结果,
z=5
2+3=5
2.3 算法的特性
1,有穷性:一个算法包含有限的操作步骤
2,确定性:算法中的每一个步骤是确定的,含义是唯一的
3,有零个或多个输入
4,有一个或多个输出
5,有效性:算法中每一个步骤应能有效运行
2.4 算法的表示
1,用自然语言表示优点是使用日常用语,通俗易懂缺点是文字冗长,容易出现歧义
2,用流程图表示,用图框表示各种操作优点是直观形象,易于理解起止框处理框输入输出框判断框流程线连接 点注释框常用流程图符号
3,三种基本结构( 表示一个良好算法的基本单元 )
① 顺序结构 ② 选择结构( 分支结构 ) ③ 循环结构( 重复结构 )
A
B
P
A B
成立 不成立成立
A
P 不成立
A
P 成立不成立
4,N-S流程图
A
B
A B
成立 不成立
P
A
当 P成立直到 P成立
A
While( 当型 )循环 Until( 直到型 )循环例:输入 10个数,找出其中最大的数,并输出。
step1,输入一个数,存放在一个变量 max中;
step2,设置用来累计比较次数的计数器 i(也是一个变量 )
1?i;
step3,输入一个数,存放在另一个变量 x中;
step4,比较 max和 x中的数,若 x>max,则将 x的值送入 max,
否则,max的值不变;
step5,i 增加 1,即 i+1?i ;
step6,若 i<9,则返回 step3,继续执行,
否则输出 max中的数,此时 max中的数即为最大数。
输入一个数? max
1? i
输入 x
x?max?是 否
x? max
i+1? i
当 i < 9
输出 max
#include <stdio.h>
void main( )
{ int x,max,i ;
scanf(“%d”,&max);
i=1;
do
{ scanf(“%d”,&x);
if (x>max) max=x;
i=i+1;
}
while ( i<9) ;
printf(“max=%d”,max) ;
}
5、伪代码介于自然语言与计算机语言之间,用文字与符号来描述算法。
例,求 5!
开始置 t 的初值为 1
置 i 的初值为 2
当 i <= 5,执行下面操作,
使 t = t * i
使 i = i + 1
(循环到此结束 )
打印 t 的值结束或
BEGIN(算法开始 )
1 => t
2 => i
while i <= 5
{ t * i => t
i + 1 => i
}
print t
END(算法结束 )
2.5 结构化程序设计方法基本思路,把一个复杂问题的求解过程分阶段进行,每个阶段处理的问题都控制在人们容易理解和处理的范围内。
1、自顶向下
2、逐步细化
3、模块化设计
4、结构化编码需要解决的问题 P
子问题 p1 子问题 p2 子问题 p3
p12p11 p32p31 p33
设计 设计 设计 设计 设计 设计编码 编码 编码 编码 编码 编码
Preparations
Data types
Constants and Variables
Type Conversions
Operators,and Expressions
Chapter 3 Data types,Operators,
and Expressions
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
方法:连续除以基,从低到高记录余数,直至商为 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
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
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
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
– 数值的表示方法 ——原码、反码和补码
原码:最高位为符号位,其余各位为数值本身的绝对值
反码:
– 正数:反码与原码相同
– 负数:符号位为 1,其余位对原码取反
补码:
– 负数:最高位为 1,其余位为原码取反,再对整个数加 1
– 正数:原码、反码、补码相同
<
原码 反码 补码
+7 00000111 00000111 00000111
-7 10000111 11111000 11111001
(用一字节表示数)
Chapter 3 Data types,Operators,and Expressions
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
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
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……..
例,判断下列标识符号合法性
s m Sum M.D.John day Date 3days
student_name #33 lotus_1_2_3
char a>b _above $123
< >
M.D.John 3days
#33
char $123a>b
Chapter 3 Data types,Operators,and Expressions
– 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
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……
<
问题:
0123 = ( )10
0x123 = ( )10
0Xff = ( )10
83
291
255
问题:
( 10
10
ff ( )10
Chapter 3 Data types,Operators,and Expressions
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
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’
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
<
注:
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
编译程序根据变量定义为其分配指定字节的内存单元 …...
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
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
例 /*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
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
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
– 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
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,( ) -
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
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
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
注意,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
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
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
注:
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)))
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
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
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
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
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
! (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
! (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
例 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