在 C 中打印布尔结果

发布于 2024-10-20 02:20:41 字数 301 浏览 1 评论 0原文

我读到

int c;
while(c = getchar() != EOF)
{
    putchar(c);
}

将根据下一个字符是否为 EOF 来打印值 0 或 1。因为 != 的优先级高于 =

但是当我在 GCC 中运行这个程序时,我得到一个看起来像

|0 0| 的 字符
|0 1|

作为我按 Enter 时的输出。

I read that

int c;
while(c = getchar() != EOF)
{
    putchar(c);
}

will print the value 0 or 1 depending on whether the next character is an EOF or not. Because != has a higher precedence than =.

But when I run this program in GCC, I get a character that looks like

|0 0|
|0 1|

as output when I press Enter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧伤慢歌 2024-10-27 02:20:41

putchar 打印一个字符。通过打印值 0 和 1,您将打印 null标题开始 (SOH) 字符,都是控制字符。您需要将数字 0 和 1 转换为可打印的内容,方法是直接从 0 或 1 计算可打印值:

while (...) {
    // note: the C standard (§ 5.2.1-3 of C99) ensures '0'+1 is '1', which is printable
    putchar(c+'0');
}

或使用 c 决定要打印的内容。

while (...) {
    if (c) {
        ...
    } else {
        ...
    }
    // or:
    //putchar(c ? ... : ...);
    // though feelings on the ternary operator vary.
}

putchar prints a character. By printing the values 0 and 1, you're printing the null and start of heading (SOH) characters, both control characters. You'll need to convert the numbers 0 and 1 to something that's printable, either by calculating a printable value directly from the 0 or 1:

while (...) {
    // note: the C standard (§ 5.2.1-3 of C99) ensures '0'+1 is '1', which is printable
    putchar(c+'0');
}

or using c to decide what to print.

while (...) {
    if (c) {
        ...
    } else {
        ...
    }
    // or:
    //putchar(c ? ... : ...);
    // though feelings on the ternary operator vary.
}
灼疼热情 2024-10-27 02:20:41

除了每个人都说 c 是不可打印字符之外,无论如何,您都不会为 EOF 打印出 0 ,因为您不是在这种情况下将进入 while 循环。循环后您需要一个额外的 putchar

In addition to what everyone said about c being a nonprintable character, you would never print out a 0 for EOF anyway, since you're not going to go into the while loop in that case. You would need an extra putchar after the loop.

吹梦到西洲 2024-10-27 02:20:41

这就是在您的程序中为 int 保留空间(将该空间称为 c)而发生的情况

int c;

,而不必担心其内容。

while(c = getchar( ) != EOF)

括号中的内容可以写成 c = (getchar( ) != EOF) 因为赋值运算符的优先级低于不等运算符。

  • getchar() 等待按键并返回按下的按键的值
  • 该值与 EOF 进行检查
  • 由于不同,不等运算符的结果是 1
  • 并且该值1 被放入名为 c 的空间中。

然后,在 while 循环内,

{
   putchar(c);
}

您将打印值为 1 的字符。正如您所注意到的,在您的计算机上,值为 1 的字符在显示时没有漂亮的格式:)


如果您确实想打印值 < code>0 或 1 具有漂亮的格式,请尝试此

c = 0; /* or 1 */
putchar('0' + c);

如果您想将 0 到 9 之间的值打印为字符,请尝试此

c = 5; /* or 7, or 0, ... */
putchar('0' + c);

This is what happens in your program

int c;

reserve space for an int (call that space c) and don't worry about its contents.

while(c = getchar( ) != EOF)

The thing in parenthesis can be written as c = (getchar( ) != EOF) because the assignment operator has lower precedence than the inequality operator.

  • getchar() waits for a keypress and returns the value of the key pressed
  • That value is checked against EOF
  • As it's different, the result of the inequality operator is 1
  • and the value 1 gets put in the space with name c.

Then, inside the while loop

{
   putchar(c);
}

you're printing the character with value 1. As you've noticed, on your computer, the character with value 1 does not have a beautiful format when displayed :)


If you really want to print the values 0 or 1 with beautiful formats, try this

c = 0; /* or 1 */
putchar('0' + c);

If you want to print a value between 0 and 9 as a character, try this

c = 5; /* or 7, or 0, ... */
putchar('0' + c);
迷乱花海 2024-10-27 02:20:41

您正在使用 Unicode 控制台。所有不可打印的字符(例如值为 0 和 1 的字节)都会转换为 2x2 矩阵,显示其 Unicode 值。 (此外,对于未安装字体的所有可打印字符。)

You are using a Unicode console. All non-printable characters (like the bytes with value 0 and 1) are converted to the 2x2-matrix displaying its Unicode value. (Also, for all printable characters for which you have no font installed.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文