【问题标题】:How ctrl-d, ctrl-c is handled by C/scanfctrl-d, ctrl-c 是如何被 C/scanf 处理的
【发布时间】:2021-01-23 05:55:45
【问题描述】:

我们来看看下面的程序:

# include<stdio.h>
int main(void)
{
    int status, current_number, sum=0;
    printf("Enter a number: ");
    while(status=scanf("%d", &current_number)) {
        sum += current_number;
        printf("Status: %d. The current sum is: %d. Enter another number: ", status, sum);
    }   
}

输入一个数字:2
状态:1.当前总和为:2.输入另一个数字:3
状态:1。当前总和为:5。输入另一个数字:状态:-1。当前总和为:8。输入另一个数字:^C

似乎 CtrlD (EOF) 被识别为-1CtrlC 会导致要退出的程序。这两个转义序列通常在 C 中如何处理?为什么scanf 对待ctrl-cctrl-d 的方式不同?

【问题讨论】:

  • 取决于终端。通常ctrl-d 发送EOF 字符,ctrl-c 发送SIGINT 信号。 SIGINT 的默认信号操作是终止程序。

标签: c escaping


【解决方案1】:

scanf 函数以任何特殊方式处理这些字符,它甚至看到这些字符。发生的情况是终端驱动程序(至少在类 UNIX 系统下(a))拦截这些击键并将它们转换为特殊操作。

对于 CTRL-d,它会关闭标准输入文件,这样任何读取它的代码都会得到一个 EOF - 这就是您看到的 -1(表示某些描述的错误正在阅读)。

对于 CTRL-c,它会引发 SIGINT 信号,如果未被捕获,将终止您的程序。

请记住,这些是这些操作的默认键绑定,可以使用stty 更改它们以使用不同的键绑定。默认值(intreof)如下所示(^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", &current_number)) {

循环只会在scanf 返回零时退出,如果无法扫描整数(例如通过输入非数字XYZZY)就会发生这种情况。它继续用于任何非零值,包括您在错误/文件结束时返回的-1

更好的循环是:

while((status = scanf("%d", &current_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) 作为文件结束指示符。

【讨论】:

    猜你喜欢
    • 2014-02-11
    • 2012-02-07
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 2013-08-19
    相关资源
    最近更新 更多