你该逆袭了
第7章:重点摘录
三、逻辑运算符
#include <stdio.h>
#define PERIOD '.' //PERIOD 相当于 单引号—句点—单引号
int main()
{
char ch = 0;
int charcount = 0;
while ((ch = getchar()) != PERIOD)
{
if (ch != '"' && ch != '\'')
charcount++;
}
printf("there are %d non_quoto characters.\n", charcount);
return 0;
}
1、备选拼写:iso646.h 头文件
#include <stdio.h>
#include <iso646.h> //and or not 代替 && || ! 的头文件
#define PERIOD '.' //PERIOD 相当于 单引号—句点—单引号
int main()
{
char ch = 0;
int charcount = 0;
while ((ch = getchar()) != PERIOD) //用 and 代替 &&
{
if (ch != '"' and ch != '\'')
charcount++;
}
printf("there are %d non_quoto characters.\n", charcount);
return 0;
}
2、优先级
优先级顺序:从上到下
( )
!
算数运算符
关系运算符
&&
||
赋值运算符
3、求值顺序
除了两个运算符共享一个运算对象的情况外,C 通常不保证先对复杂表达式中哪部分求值。
例如,下面的语句,可能先对表达式 5+3 求值,也可能先对表达式 9+6 求值:
apples = (5 + 3) * (9 + 6);
C 把先计算哪部分的决定权留给编译器的设计者,以便针对特定系统优化设计。
但是,对于 逻辑运算符 是个例外,C 保证 逻辑表达式 的 求值顺序 是 从左往右。
&& 和 || 运算符都属 序列点,所以程序在从一个运算对象执行到另一个运算对象之前,所有的副作用都会生效。
而且,C 保证一旦发现某个元素让整个表达式无效,便立即停止求值。
4、范围
#include <stdio.h>
#include <ctype.h>
int main()
{
int i = 0;
char str[] = "test string.\n";
while (islower(str[i])) //无论使用哪种特定的字符编码,islower()函数都能正常运行,是可移植的
{
i++;
}
printf("%d\n", i); // 4
return 0;
}
四、一个统计单词的程序
//P195 程序清单7.7
//统计字符数、单词数、行数
#include <stdio.h>
#include <ctype.h> //为isspace( )提供函数原型
#include <stdbool.h>