简体   繁体   English

我正在阅读“The C Programming Language”(2ed),但我不明白一个概念:

[英]I am reading “The C Programming Language” (2ed) and I didn't understand a concept:

I am reading "The C programming Language" (2ed) and I didn't understand a concept.我正在阅读“C 编程语言”(第 2 版),但我没有理解一个概念。

There is this code to copy input to output有这段代码可以将输入复制到输出

#include <stdio.h>

main()
{
    int c;
    c = getchar();
    while (c != EOF) {
        putchar(c);
        c = getchar();
    }
}

In the beginning, c is declared as an int type variable, but if I write something like "test line" the output is the expected "test line".一开始, c被声明为一个int类型变量,但如果我写一些类似“测试行”的东西,输出就是预期的“测试行”。 My question is: how is c declared as an int type variable but the program stores space enough to store a lot of characters?我的问题是: c如何声明为int类型变量,但程序存储的空间足以存储大量字符? "Inside" the computer, it registers as an array of characters? “内部”计算机,它注册为一个字符数组? How does this process work?这个过程是如何运作的?

but if i write something like "test line" the output is the expected "test line".但是如果我写一些类似“测试线”的东西,输出就是预期的“测试线”。

how is "c" declared as a int type variabel but the program stores space enough to a lot of characters ? “c”如何声明为int类型变量但程序存储的空间足以容纳大量字符?

All that input is not stored in variable c at the same time, but in input and output buffers.所有输入不会同时存储在变量c中,而是存储在输入和输出缓冲区中。 Variable c handles those characters one at a time.变量c处理一个字符。

Lets walk though the sequence of events.让我们来看看事件的顺序。

  1. main() starts and runs to getchar() . main()启动并运行到getchar()

  2. getchar() waits for input. getchar()等待输入。

  3. User types t , the OS buffers the character.用户键入t ,操作系统会缓冲该字符。

  4. User types e s ...., e the OS buffers those characters.用户类型e s ......, e操作系统的缓冲区这些字符。

  5. User types Enter , the OS buffers the character '\\n' and gives the buffer to stdin for getchar() to start using.用户输入 Enter ,操作系统缓冲字符'\\n'并将缓冲区提供给stdin以供getchar()开始使用。

  6. getchar() returns t and saves in c . getchar()返回t并保存在c

  7. Code continues to putchar(c);代码继续putchar(c); and puts the t in the stdout buffer.并将t放入stdout缓冲区。

  8. Code continues to getchar();代码继续getchar(); , returns the buffered e and saves in c . , 返回缓冲的e并保存在c

  9. Code continues to putchar(c);代码继续putchar(c); and puts the e in the stdout buffer.并将e放入stdout缓冲区。

  10. 8 - 9 repeat for st line .st line重复 8-9 次。

  11. Code continues to getchar();代码继续getchar(); , returns the buffered \\n and saves in c . , 返回缓冲的\\n并保存在c

  12. Code continues to putchar(c);代码继续putchar(c); and puts the \\n in the stdout buffer.并将\\n放入stdout缓冲区。 The buffer is now flushed to the OS output terminal.缓冲区现在刷新到 OS 输出终端。 Output now visible.输出现在可见。

  13. Back to step 2 until no input available.回到第 2 步,直到没有可用的输入。

First, some background.首先,一些背景。

A character is an element of a string.字符是字符串的一个元素。 A character is a number .一个字符就是一个数字

The glyphs you see on the screen ( a , b , etc) are simply how your terminal interprets the numbers it receives (97, 98, etc).您在屏幕上看到的字形( ab等)只是您的终端如何解释它接收到的数字(97、98 等)。

In a C program, there is absolutely no difference between在 C 程序中,两者之间绝对没有区别

'a'
97

Both are integer literals of type int with a value of ninety-seven.两者都是int类型的整数文字,值为 97。 (I'm assuming ASCII-based machines here. They're actually different on an EBCDIC-based machine.) (我在这里假设是基于 ASCII 的机器。它们在基于 EBCDIC 的机器上实际上是不同的。)


Now for your question.现在回答你的问题。

but the program stores space enough to a lot of characters ?但是该程序存储的空间足以容纳很多字符吗?

It does not.它不是。 c only contains a single character at any given time. c在任何给定时间只包含一个字符。 The first time through the loop, 116 aka 't' is assigned to it.第一次通过循环,116 aka 't' 被分配给它。 The second time through the loop, 101 aka 'e' is assigned to it.第二次通过循环,101 aka 'e' 被分配给它。 etc.等等。

how is "c" declared as a int type variabel but the program stores space enough to a lot of characters ? “c”如何声明为 int 类型变量但程序存储的空间足以容纳大量字符?

The source code shown does not store a lot of characters.显示的源代码没有存储很多字符。 It processes one character at a time.它一次处理一个字符。

c = getchar(); puts the code for one input character in c .将一个输入字符的代码放在c Then putchar(c);然后putchar(c); writes that code to output.将该代码写入输出。 Then the program can forget about the character.然后程序可以忘记字符。 It does not need it any more, and it can get a new character code and reuse c for that.它不再需要它了,它可以得到一个新的字符代码并为此重用c

The output stream may hold a lot of characters in a buffer before writing them to the output device, but that is in separate source code that is not directly in the source code shown.在将字符写入输出设备之前,输出流可能会在缓冲区中保存大量字符,但这是在单独的源代码中,而不是直接在所示的源代码中。 It is part of the C standard libraries.它是 C 标准库的一部分。

Suppose I'm mixing drinks for my friends.假设我正在为我的朋友调酒。 I have four glasses in front of me.我面前有四副眼镜。 I want to put 1/8 cup (1 shot) of gin in each glass.我想在每个玻璃杯中放入 1/8 杯(1 杯)杜松子酒。 But I don't have a 1/8 cup measuring cup.但我没有 1/8 杯量杯。 So I get a 1/4 cup measuring cup, and I fill it halfway, and that way I measure 1/8 cup of liquor for each glass.所以我拿了一个 1/4 杯量杯,我把它装了一半,这样我每杯量了 1/8 杯酒。

Is it a problem that I used a 1/4 cup measure to measure 1/8 cup of liquid?我用 1/4 杯量具量出 1/8 杯液体有问题吗? What about all the empty space at the top?顶部的所有空白空间呢? Well, obviously, it won't cause any problem at all.嗯,显然,它根本不会造成任何问题。

Similarly, if you use an int (which is probably a 16- or 32-bit variable) to hold the return value from calling getchar() , this won't cause any problem at all even if most of the character values that come back from getchar are just 8 bits.类似地,如果您使用int (可能是 16 位或 32 位变量)来保存调用getchar()的返回值,即使大多数返回的字符值也不会造成任何问题从getchar只有 8 位。

But it will make a difference when it comes time for getchar to return the code EOF (end of file) to you!但是,当getchar将代码EOF (文件结尾)返回给您时,情况有所不同! EOF is a value that will not fit in 8 bits, because it is (by definition) not an 8-bit character value. EOF是一个适合 8 位的值,因为它(根据定义)不是 8 位字符值。 That's why it's important to use an int variable to handle values coming back from getchar , not char as might seem obvious.这就是为什么使用int变量来处理从getchar返回的值很重要,而不是像看起来很明显的char

  • something is called io buf,other called return value有的叫做 io buf,有的叫做返回值
  • Specifically, it can be understood as getchar read chars to io buffer, and return one by one to api user具体可以理解为getchar读取字符到io缓冲区,并一一返回给api用户

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM