简体   繁体   English

不明白字符计数程序的输出

[英]Don't understand the output for the character counting program

I am just starting out learning c.我刚开始学习c。 Downloaded xcode to my mac and bought The c Programming Language by K&R.将 xcode 下载到我的 mac 并购买了 K&R 的 The c Programming Language。 I am now at the character counting section and cannot understand the output the program gives me.我现在在字符计数部分,无法理解程序给我的输出。 Program is...程序是...

#include <stdio.h>
main()
{
    long nc;
    nc = 0;
    while (getchar() !=EOF)
        ++nc;
    printf("%ld\n", nc);
}

I've learned from these pages that I need to hit ctrl+d twice to send an EOF character to the program and that works ok.我从这些页面中了解到,我需要按 ctrl+d 两次才能向程序发送 EOF 字符,并且可以正常工作。 However, the answer it gives me is 140,734,799,804,376 (the commas are mine) plus the number of characters in the string.但是,它给我的答案是 140,734,799,804,376(逗号是我的)加上字符串中的字符数。 Where does this huge number come from?这个庞大的数字从何而来? Why doesn't the program just return 4 for "help" instead of 140734799804380?为什么程序不只返回 4 来表示“帮助”而不是 140734799804380?

nc = 0 

and

++nc

Missing semicolon here, change to :此处缺少分号,更改为:

nc = 0;

++nc;

I tried the same code like this :我尝试了这样的相同代码:

./test.exe
hello<CTRL+D>5

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

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