当前位置:网站首页>5 运算符、表达式和语句
5 运算符、表达式和语句
2022-07-28 08:26:00 【你回到了你的家】
3.3 递增运算符:++
递增运算符(increment operator)执行简单的任务,将其运算对象递增1。该运算符以两种方式出现。第1种方式,++出现在其作用的变量前面,这是前缀模式;第2种方式,++出现在其作用的变量后面,这是后缀模式。两种模式的区别在于递增行为发生的时间不同。我们先解释它们的相似之处,再分析它们不同之处。程序清单5.10中的程序示例演示了递增运算符是如何工作的。
程序5.10
/* add_one.c -- incrementing: prefix and postfix */
#include <stdio.h>
int main(void){
int ultra = 0, super = 0;
while (super < 5){
super++;
++ultra;
printf("super = %d, ultra = %d \n", super, ultra);
}
return 0;
}
运行该程序后,输出如下:
super = 1, ultra = 1
super = 2, ultra = 2
super = 3, ultra = 3
super = 4, ultra = 4
super = 5, ultra = 5
利用递增运算符的优势,我们可以写出这样的程序:
shoe = 2.0;
while (++shoe < 18.5){
foot = SCALE*shoe + ADJUST;
printf("%10.1f %20.2f inches\n", shoe, foot);
}
如上代码所示,把变量的递增过程放入while循环的条件中。这种结构在C语言中很普遍,我们来仔细分析一下。
首先,这样的while循环是如何工作的?很简单。shoe 的值递增1,然后和18.5作比较。如果递增后的值小于18.5,则执行花括号内的语句-次。然后, shoe的值再递增1,重复刚才的步骤,直到shoe的值不小于18.5为止。注意,我们把shoe的初始值从3.0改为2.0,因为在对foot第1次求值之前,shoe已经递增了1 (见图5.4)。
其次,这样做有什么好处?它使得程序更加简洁。更重要的是,它把控制循环的两个过程集中在–个地方。该循环的主要过程是判断是否继续循环(本例中,要检查鞋子的尺码是否小于18.5),次要过程是改变待测试的元素(本例中是递增鞋子的尺码)。
如果忘记改变鞋子的尺码,shoe的值会一直小于 18.5,循环不会停止。计算机将陷入无限循环(infinite loop)中,生成无数相同的行。最后,只能强行关闭这个程序。把循环测试和更新循环放在一处, 就不会忘记更新循环。
但是,把两个操作合并在-一个表达式中,降低了代码的可读性,让代码难以理解。而且,还容易产生计数错误。
递增运算符的另一个优点是,通常它生成的机器语言代码效率更高,因为它和实际的机器语言指令很相似。尽管如此,随着商家推出的C编译器越来越智能,这一优势可能会消失。-一个智能的编译器可以把x =x+1当作++x对待。
最后,递增运算符还有一个在某些场合特别有用的特性。我们通过程序清单5.11来说明。
程序5.11
/* post_pre.c -- postfix vs prefix */
#include <stdio.h>
int main(void){
int a = 1, b = 1;
int a_post, pre_b;
a_post = a++; // value of a++ during assignment phase
pre_b = ++b; // value of ++b during assignment phase
printf("a a_post b pre_b \n");
printf("%1d %5d %5d %5d\n", a, a_post, b, pre_b);
return 0;
}
编译后程序的输出应该是:
a a_post b pre_b
2 1 2 2
a和b都递增了1,但是,a_ post是a递增之前的值,而b_ pre是b递增之后的值。这就是++的前缀形式和后缀形式的区别(见图5.5)。


边栏推荐
- Design for failure常见的12种设计思想
- A perfect cross compilation environment records the shell scripts generated by PETA
- Mobaxtermsession synchronization
- 台大林轩田《机器学习基石》习题解答和代码实现 | 【你值得拥有】
- Go synergy
- A new method of exposing services in kubernetes clusters
- How to execute the SQL assembled in ODPs SQL function and get the return value?
- 【单细胞高级绘图】07.KEGG富集结果展示
- Why can ThreadLocal achieve thread isolation?
- Two dimensional array and operation
猜你喜欢

蓝牙技术|2025年北京充电桩总规模达70万个,聊聊蓝牙与充电桩的不解之缘

中国地图省>市>级>区>镇>村5级联动下载【2019和2021】

Vs2015 use dumpbin to view the exported function symbols of the library

Data analysis interview question summary

c语言数组指针和指针数组辨析,浅析内存泄漏

Alibaba technology has four sides + intersection +hr, and successfully got the offer. Can't double non undergraduate students enter the big factory?

Machine learning: self paced and fine tuning

Flink window & time principle

JSON file storage

修改虚拟机IP地址
随机推荐
Flink window & time principle
完善的交叉编译环境记录 peta 生成的shell 脚本
1299_ Task status and switching test in FreeRTOS
How to execute the SQL assembled in ODPs SQL function and get the return value?
Let me teach you how to assemble a registration center?
Argocd Web UI loading is slow? A trick to teach you to solve
剑指offer
This flick SQL timestamp_ Can ltz be used in create DDL
Review the past and know the new MySQL isolation level
侯捷STL标准库和泛型编程
ES6 let与const
View the dimensions of the list
10. Learn MySQL like clause
Oracle SQL problems
From development to testing: I started from scratch and worked for six years of automated testing
Basic syntax of jquey
How to obtain the subordinate / annotation information of KEGG channel
IDC脚本文件运行
1w5 words to introduce those technical solutions of distributed system in detail
Mobaxtermsession synchronization