简体   繁体   English

为什么单个数字会附加“D”(在 C 输出中)?

[英]Why single digit numbers are appended with “D” (in C output)?

Why single digit numbers are appended with "D" (in C output)?为什么单个数字会附加“D”(在 C 输出中)?

The following code以下代码

#include <stdio.h>

int main(void)
{
    int c = 0;

    printf("%d\n", c); 

    return 0;
}

once compiled & ran, outputs 0 , as I would expect it to.一旦编译并运行,输出0 ,正如我所期望的那样。
Though, this code虽然,这段代码

#include <stdio.h>

int main(void)
{
    int c = 0;

    while (getchar() != EOF) {
        ++c;
    }

    printf("%d\n", c); 

    return 0;
}

once compiled & ran, after triggering EOF right away -- outputs 0D for some reason, though value of c (as far as I can see) should be absolutely the same as in the first case.一旦编译并运行,在立即触发EOF之后——由于某种原因输出0D ,尽管c的值(据我所知)应该与第一种情况完全相同。
Same happens for all the single digit numbers (ie 1D , 2D , 3D ... 9D ), starting with 10 the appending D is not seen anymore.所有单个数字(即1D2D3D ... 9D )都会发生同样的情况,从10开始,不再看到附加的D

I'd like to know:我想知道:

  1. Why D is appended to the output in the second case, but not in the first (even though c should hold the same value)?为什么在第二种情况下D附加到 output 而不是第一种情况(即使c应该保持相同的值)?

  2. Is it possible to avoid this D appending (and how, if it is)?是否有可能避免这个D附加(以及如何,如果是的话)?

Your code simply cannot output a "D" for whatever reason.无论出于何种原因,您的代码根本无法 output 为“D”。 Not unless something really iffy happens, like a bug in the compiler or glibc.除非确实发生了一些不确定的事情,例如编译器或 glibc 中的错误。 Or maybe a bitflip in memory.或者可能是 memory 中的位翻转。

The "D" is very likely due to the terminal you're using, but your code simply CANNOT output it. “D”很可能是由于您使用的终端,但您的代码根本不能 output 它。

Well, there is a very small chance.嗯,机会很小。 If you read more than INT_MAX characters, then the signed integer c will overflow, thus invoking undefined behavior.如果您读取的字符数超过INT_MAX ,则有符号的 integer c将溢出,从而调用未定义的行为。 It's not likely that this would output a "D", but it's possible.这不太可能 output 为“D”,但有可能。

When posting the question, I thought this behaviour has something to do with cases like this , but, as it turned out, it has nothing to do with the code and is solely terminal behaviour .在发布问题时,我认为这种行为与这种情况有关,但事实证明,它与代码无关,只是终端行为
Suggestion by Some programmer dude in a comment, is exactly right: terminal simply outputs ^D and output of the program then overwrites ^ , but not D , thus for single-digit numbers output ends up being appended with D . 一些程序员老兄在评论中的建议是完全正确的:终端只是输出程序的^D和 output 然后覆盖^ ,但不是D ,因此对于个位数的数字 output 最终会附加D
Two (or more) digit numbers simply overwrite the whole ^D .两个(或更多)数字只会覆盖整个^D

I was able to verify this by changing EOF triggering sequence to Ctrl + L (by stty eof ^L ), then the output becomes 1L , 2L , 3L ... 9L .我可以通过将EOF触发序列更改为Ctrl + L (通过stty eof ^L )来验证这一点,然后 output 变为1L2L3L ... 9L
As Jonathan Leffler said in comment above disabling echoing of control characters (by stty -echoctl ) would solve the issue.正如Jonathan Leffler在上面的评论中所说,禁用控制字符的回显(通过stty -echoctl )将解决该问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM