简体   繁体   English

“ C编程语言”中的代码如何工作?

[英]how does this code from “The C Programming Language” work?

I'm reading "The C Programming Language (2nd ed.) and near the beginning, it has examples like this: 我正在阅读“ The C Programming Language(第二版)”,在一开始的时候,它有这样的示例:

while((c = getchar()) != EOF)
    if(c == '\n'){
        ++n1;

I can see how this would work while reading from a file, and I understand this syntax... But this is just reading from the console--how does one signal end of file when entering characters from a console? 我可以看到在读取文件时这是如何工作的,并且我了解这种语法...但是,这只是从控制台读取的内容-从控制台输入字符时,信号如何指示文件结尾? I'm using Windows XP... MinGW compiler... Anyways, was this book written for waaay earlier systems with like an EOF button or something? 我正在使用Windows XP ... MinGW编译器...无论如何,这本书是为waaay早期系统编写的,具有EOF按钮之类的功能?

Update 更新资料

well, I have one more question, that's just related to how the end-of-file works under Windows. 好吧,我还有一个问题,那就是与文件结束在Windows下的工作方式有关。

If I just while(getchar()!=EOF); 如果我只是while(getchar()!=EOF); , then I can just keep typing characters until I signal EOF via ^Z. ,那么我可以继续输入字符,直到通过^ Z发出EOF信号为止。 But, I have to write a newline, then hit ^Z, then another newline... Why does it have to be on its own line? 但是,我必须写一个换行符,然后按^ Z,然后再输入另一个换行符...为什么必须在自己的行上?

Windows uses Ctrl-Z for EOF, and UNIX uses Ctrl-D. Windows将Ctrl-Z用于EOF,而UNIX使用Ctrl-D。 See http://bytes.com/groups/c/217873-eof-windows , and excellent book choice. 请参阅http://bytes.com/groups/c/217873-eof-windows和出色的书籍选择。 :) :)

^ Z为EOF。

The correct answer has been already given, but a typical usage would be to redirect a file to standard output: 已经给出了正确的答案,但是典型的用法是将文件重定向到标准输出:

program.exe < samplefile.txt

samplefile.txt is "written" to standard out and program.exe reads this from standard out until the EOF is reached. samplefile.txt被“写入”到标准输出中, program.exe从标准输出中读取该文件,直到达到EOF。

Regarding your question on ^Z, the reasonn it behaves like this is because it isn't really a character, it's a signal from the operating system to the C input system. 关于您在^ Z上的问题,其行为之所以如此,是因为它实际上不是字符,它是从操作系统到C输入系统的信号。 As such, it is highly dependant on the interaction between the OS and the C input system buffering. 因此,它高度依赖于OS和C输入系统缓冲之间的交互。 Which is a fancy way of saying that it's just the way things work, for Windows and for your particular C implementation. 这是一种奇妙的说法,它只是Windows和您的特定C实现的工作方式。

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

相关问题 fgets(); 在这部分代码中如何工作? C程序设计 - fgets(); how does this work inside this part of code? C programming 为什么 C 编程语言手册中的一些代码示例不能按预期工作? - Why some code examples from The C Programming Language book don't work as expected? 这段代码在 C 语言中究竟是如何工作的? (一个初学者问题) - How does this code exactly work in C Language? (A Beginner Question) “网络编程”示例中的代码如何工作? - How does this code from “Network programming” examples work? 为什么 Dennis Ritchie 的“C 编程语言”中的这个程序不起作用? - Why does this program in Dennis Ritchie's "The C Programming Language" not work? “ C编程语言第二版”中的这段代码是否包含错误? - Does this code from “The C Programming Language 2nd Edition” contain a bug? 《 The C Programming Book》中的getop()函数如何工作? - How does the function getop() from “The C Programming Book” work? c 编程语言中代码的复杂性 - Complexity of a code in c programming language C 宏如何扩展 C 编程语言的语法和语义? - How does a C macro extend the syntax and semantics of the C programming language? C 编程如何在没有头文件的情况下工作? - How does C programming work with no headers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM