# Linux, 2020-11-20
在计算机还没有出现之前,有一种叫做电传打字机(Teletype Model 33)的机械打字机,每秒钟可以打10个字符。但是它有一个问题,就是打完一行换行的时候,要用去0.2秒,正好可以打两个字符。要是在这0.2秒里面,又有新的字符传过来,那么这个字符将丢失。
于是,研制人员想了个办法解决这个问题,就是在每行后面加两个表示结束的字符。一个叫做“回车”,告诉打字机把打印头定位在左边界,不卷动滚筒;另一个叫做“换行”,告诉打字机把滚筒卷一格,不改变水平位置。
这就是“换行”和“回车”的由来。
Linux 中输入 enter 到底是什么键
首先通过一个程序
#include <stdio.h>
int main() {
int c;
int n = 0;
while((c = getchar()) != 'Q') {
printf("char %3d is %c code %d\n", n++, c, c);
}
}
// output
hello
char 0 is h code 104
char 1 is e code 101
char 2 is l code 108
char 3 is l code 108
char 4 is o code 111
char 5 is
code 10
Q
发现,当输入 enter 时, Unix 读入 \n
linux系统中的等价关系:
用enter换行 <====> 程序写\n <====> 真正朝文件中写\n(0x0a) <====> 程序真正读取的是\n)
#include<stdio.h> int main(int argc, char *argv[]) { int ch; while(1) { ch = fgetc(stdin); if (ch == 10) { fprintf(stdout, "You have pressed the enter key\n"); } } }