当前位置:网站首页>C programming language (2nd Edition) -- Reading Notes -- 1.5.2
C programming language (2nd Edition) -- Reading Notes -- 1.5.2
2022-07-27 11:28:00 【YovaVan】
1.5.2 Character count
The next program counts characters , Similar to the replication program .
#include <stdio.h>
/* Count characteristics in input, first version*/
main()
{
long nc;
nc=0;
while (getchar() != EOF)
++nc;
printf("%ld\n",nc);
}Its
++nc;Introduce new operator ++, Add 1 operation . You can use nc = nc + 1 Instead of . but ++nc More refined , And often more efficient . And ++ The corresponding is the self subtraction operator --.++ And -- It can be used as prefix operator ( Such as ++nc), It can also be used as suffix operator ( Such as nc++). The first 2 These two forms have different values in the expression , but ++nc And nc++ All make nc The value of the increase 1. At present, only prefix form .
This procedure use long Type variables store count values , Instead of using int Type variable .long Integer numbers ( Long integer ) At least occupy 32 Bit storage unit . On some machines int And long Integer has the same length , But on some machines ,int The value of type may only be 16 The length of the bit storage unit ( The maximum value is 32767), A fairly small input can make int Count variable overflow of type . Conversion instructions ——%ld—— tell printf Function whose corresponding arguments are long integer .
use double The double precision floating-point number type can handle larger numbers . Here it is , no need while, While using for Loop to write another method of this loop :
#include <stdio.h>
/* Count characteristics in input, first version*/
main()
{
#if 0 /* Put this to use , Conditional compilation */
long nc;
nc = 0;
while (getchar() != EOF)
++nc;
printf("%ld\n",nc);
#else
/* Count characteristics in input second version*/
double nc;
for(nc = 0; getchar() != EOF; ++nc)
; // Emphasis ";"
printf("%.0f\n",nc);
#endif
}about float and double type , printf Functions are used %f To illustrate .%.0f Force not to print Decimal point and decimal part , Therefore, the number of decimal places is 0.
In the program for The loop body of the loop statement is empty , Because all the work is being tested ( Conditions ) And increasing the step length is partially completed . but C Language Rule of grammar requirement for A loop statement must have a loop body , So use a separate semicolon instead . Separate semicolons are called empty statements , It just meets for Statement . Put it on a separate line to make it stand out .
Before the end of the , Consideration : If the input does not contain characters , That's the first call getchar Function time , while or for The conditional test in the statement is false from the beginning , The result of program execution is 0, This is also the right result . That's important . while And for One of the advantages of statements : Test the condition before executing the loop body , If the conditions are not met , The loop body... Is not executed , It may happen that the loop body is not executed once . In the presence of 0 When inputting the length , Program processing should be more flexible , When boundary conditions occur , while And for Statements help ensure that the program performs reasonable operations .
边栏推荐
- Knapsack problem acwing 9. grouping knapsack problem
- 数字三角形模型 AcWing 1018. 最低通行费
- (8) Shell function
- Quantitative industry knowledge summary
- The difference between extern and static
- 8 find subsequences with a maximum length of K
- Pat (Grade B) 2022 summer exam
- Verilog implementation of ECG signal acquisition, storage and transmission system based on FPGA
- Smart pointer (shared_ptr, unique_ptr, weak_ptr)
- 力扣——10. 正则表达式匹配
猜你喜欢

XXX packages are looking for funding run 'NPM fund' for details solutions

数字三角形模型 AcWing 1027. 方格取数

什么是私域流量?

The longest ascending subsequence model acwing 1016. The sum of the largest ascending subsequence

An article reveals the NFT strategy of traditional game manufacturers such as Ubisoft

Why is the data service API the standard configuration of the data midrange when we take the last mile of the data midrange?

Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing

Redis high availability principle
![Take you hand-in-hand to develop a complete classic game [Tetris] from scratch, with less than 200 lines of logic.](/img/7f/f42f9267cdff35522c260ef073bab9.png)
Take you hand-in-hand to develop a complete classic game [Tetris] from scratch, with less than 200 lines of logic.

Kepserver configuration
随机推荐
Cancer DDD
(7) Process control
状态压缩DP AcWing 91. 最短Hamilton路径
记忆化搜索 AcWing 901. 滑雪
Description and feelings
最长上升子序列模型 AcWing 1016. 最大上升子序列和
Yiwen counts NFT projects valued at more than US $100million
Ansible
Moveit2 - 4. robot model and robot state
JVM judges that the object is dead, and practices verify GC recycling
tensorflow运行报错解决方法
Pat (Grade B) 2022 summer exam
(10) File contains
Sorry, you guys have something to deal with in the bank recently, which has been delayed
(3) Pass parameters
Miscellaneous records of Finance
(4) Operator
Knapsack problem acwing 9. grouping knapsack problem
Solutions to errors in tensorflow operation
349 sum of intersection of two arrays and 01