scanf 函数不以任何特殊方式处理这些字符,它甚至看到这些字符。发生的情况是终端驱动程序(至少在类 UNIX 系统下(a))拦截这些击键并将它们转换为特殊操作。
对于 CTRL-d,它会关闭标准输入文件,这样任何读取它的代码都会得到一个 EOF - 这就是您看到的 -1(表示某些描述的错误正在阅读)。
对于 CTRL-c,它会引发 SIGINT 信号,如果未被捕获,将终止您的程序。
请记住,这些是这些操作的默认键绑定,可以使用stty 更改它们以使用不同的键绑定。默认值(intr 和 eof)如下所示(^C 和 ^D):
pax> stty -a
speed 38400 baud; rows 37; columns 145; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z;
rprnt = ^R; werase = ^W; lnext = ^V; discard = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke -flusho -extproc
请记住,这可能不是你想要的:
while(status=scanf("%d", ¤t_number)) {
循环只会在scanf 返回零时退出,如果无法扫描整数(例如通过输入非数字XYZZY)就会发生这种情况。它将继续用于任何非零值,包括您在错误/文件结束时返回的-1。
更好的循环是:
while((status = scanf("%d", ¤t_number)) == 1) {
事实上,由于循环应该永远运行状态值为1,因此使用它没有什么意义(其他用于对发生的事情做出最终决定)。我更喜欢这样的东西:
#include<stdio.h>
int main(void) {
int stat, curr, sum = 0;
// Prompt and loop while user enters valid numbers.
printf("Enter a number: ");
while ((stat = scanf("%d", &curr)) == 1) {
// Accumulate number to sum, output details and ask for next.
sum += curr;
printf("Entered %d, sum is %d, enter another number: ", curr, sum);
}
// Final status -1 if EOF/error, 0 if item couldn't be scanned.
if (stat == -1) {
prinf("\nEnd of file or I/O error.\n");
} else {
prinf("Non-numeric data.\n");
}
}
(a) 相比之下,从内存中,Windows 只能识别行首的 CTRL-z(然后是 ENTER) 作为文件结束指示符。