#include <stdio.h>
int main(){
char ch;
while((ch=getchar())!=EOF){
putchar(ch);
}
char ch2 = 'A';
printf("ch2=======>%c\n",ch2);
ch2 = getchar();
printf("ch2=======>%d\n",ch2);
return 0;
}
为什么在win下,输入Ctrl+Z,会等待ch2的getchar()输入,而Mac下,ch2的getchar()直接跳过,而且ch2的值总是-1(EOF)?
你需要查明快捷键的作用,并不是所有系统都是Ctrl+Z作为EOF文件结束符号的。而且这类组合键信号可能不止占用一个char,也就是说,可能被判断为若干个连续输入。
很多类Unix操作系统都是用Ctrl+Z作为挂起进程,Ctrl+D作为EOF文件结束符的。
我的Linux目前就是用Ctrl+D能和Windows Ctrl+Z作用一样,而ctrl+z进程直接强行终止了。因为MacOS也是基于类Unix系统,和Linux相似的可能性很高。
"The notation ^D is used to indicate a control charachter. Control-D,or ^D,is the default end-of-file character."
--摘自《Advanced programming in the Unix environment》section 1.6,figure 1.7之后的注释。