使用 xcode 学习 C(通过 K&R)

发布于 2024-08-19 13:51:09 字数 564 浏览 2 评论 0原文

我正在通过《C 编程语言》(K&R) 学习 C。

由于我不太想在文本编辑器和运行 gcc 之间来回切换,因此我决定使用 xcode 作为 IDE。到目前为止,我已经能够毫无问题地遵循本书的示例直到第 1.5.2 节。

当给出有效的(?)程序时......

#include <stdio.h>

void main()
{
    long nc;

    nc = 0;
    while (getchar() != EOF)
        ++nc;
    printf("%ld\n", nc);
}

我没有收到最终输出告诉我输入中有多少个字符。我正在通过 xcode 控制台窗口输入我的输入。

经过一些调试,我的程序似乎陷入了 while 循环,并且从未遇到 EOF 标记。为了适应这种情况,我用 "\n" 替换 EOF 来替换换行符作为新条件,这也不执行任何操作,并为我提供了一个 int指针比较警告。

我在这里做错了什么?

我可以使用 xcode 关注 K&R 吗?

I'm learning C with The C Programming Language (K&R).

Since I don't particularly want to bob back and forth between a text editor and running gcc, I've decided to use xcode as an IDE. So far, I've been able to follow the book's examples without a problem up until section 1.5.2.

When given the valid (?) program...

#include <stdio.h>

void main()
{
    long nc;

    nc = 0;
    while (getchar() != EOF)
        ++nc;
    printf("%ld\n", nc);
}

...I receive no final output telling me how many characters were in my input. I am entering my input via the xcode console window.

Upon some debugging, it looks like my program gets stuck in the while loop, and never encounters the EOF token. To accommodate for this, I've instead substituted a newline as the new condition, by replacing EOF with "\n", which also does nothing and gives me a int to pointer comparison warning.

What am I doing wrong here?

Will I be able to follow K&R using xcode?

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

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

发布评论

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

评论(1

水晶透心 2024-08-26 13:51:09

输入 ^D (control-d) 发送 EOF

如果要在换行符处中断,则需要将 getchar 的返回值与 '\n' 进行比较,而不是 "\n" 。前者是换行符的实际 char 值;后者是指向具有该值的 char 的指针。如果这对您来说还没有意义,请不要担心,一旦您阅读了更多内容,就会明白了。

Type ^D (control-d) to send an EOF.

If you want to break on a newline, you need to compare the return value of getchar to '\n', not "\n". The former is the actual char value of a newline; the latter is a pointer to a char with that value. If that doesn't make sense to you yet, don't worry, it will once you've read more.

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