C程序语言-行数统计
原文
The next program counts input lines. As we mentioned above, the standard library ensures
that an input text stream appears as a sequence of lines, each terminated by a newline. Hence,
counting lines is just counting newlines:
#include <stdio.h>
/* count lines in input */
main()
{
int c, nl;
nl = 0;
while ((c = getchar()) != EOF)
if (c == '\n')
++nl;
printf("%d\n", nl);
}
The body of the while now consists of an if , which in turn controls the increment ++nl . The
if statement tests the parenthesized condition, and if the condition is true, executes the
statement (or group of statements in braces) that follows. We have again indented to show
what is controlled by what.
The double equals sign == is the C notation for ``is equal to'' (like Pascal's single = or
Fortran's .EQ. ). This symbol is used to distinguish the equality test from the single = that C
uses for assignment. A word of caution: newcomers to C occasionally write = when they
mean == . As we will see in Chapter 2, the result is usually a legal expression, so you will get
no warning.
中文翻译:
下一个程序统计输入的行数,接上文所说,标准库确保输入文本流以有顺序的行的形式出现,每行以换行符结尾。因此,计算行数就是计算换行数。
代码
while的主体由if构成,然后控制增量++nl.if语句测试带括号的条件,如果条件是真,就执行随后的语句(或者大括号里的语句)。我们再次使用缩进符,是为了展示哪个语句是是由哪个语句控制的。
双等于符号在c语言的意思是 "是否等于" (例如Pascal语言的"="符号或者Fortran语言的 "EQ").这个相等检验符号区分于c语言中的表示赋值的单等于符号 "=",提醒一句,新手偶尔会用 “=”来表示“==”。我们将在第二章中看到,结果通常是一个合法的表达式,所以你不会得到任何警告。
原文:
A character written between single quotes represents an integer value equal to the numerical
value of the character in the machine's character set. This is called a character constant,
although it is just another way to write a small integer. So, for example, 'A' is a character
constant; in the ASCII character set its value is 65, the internal representation of the character
A . Of course, 'A' is to be preferred over 65 : its meaning is obvious, and it is independent of a
particular character set.
中文翻译:
一个单引号之间的字符等于这个字符在机器字符集中的对应的整型数值。这叫做字符常量。尽管这只是小整数的另一种写法,所以,举个例子 ,'A' 是字符常量.在ASCII字符串集里它的内部表示为65,当然,
A比65的表现方式更直白,他独立于特定的字符串集。
讲解:
举例子
#include <stdio.h>
void main()
{
char a;
char b;
a=65;
b='A';
printf("%c", a);
printf("%c", b);
}
结果 AA;
#include <stdio.h>
void main()
{
int a;
int b;
a='A';
b=65;
printf("%d", a);
printf("%d", b);
}
结果 6565;
相信大家都明白了吧。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构