使用for循环和EOF的程序给出了不正确的字符计数。

huangapple go评论62阅读模式
英文:

Program using for loop and EOF gives incorrect character count

问题

以下是您要翻译的部分:

(FIXED)

MY ORIGINAL QUESTION (made few mistakes while framing the question) - 
```c
#include <stdio.h>

int main()
{   
    for (double nc = 0; getchar() != EOF; ++nc)
    printf("%0.f", nc);
    return 0;
}

如果我输入 "a"(不包括引号),输出结果是 01,
输入:"ab",输出结果:012
输入:回车键,输出结果:0

为什么会发生这种情况? 难道不应该 \n 给出输出 1 吗,因为在我的设备上 EOF 值是 -1,ASCII 换行字符的值是 10,我正在使用 VS Code 和 Mac M1,顺便说一句,在在线编译器中输出相同。

我写了一个错误版本的问题

这是来自 K&R 的原始代码:

#include <stdio.h>

int main()
{   
    double nc;
    for (nc = 0; getchar() != EOF; ++nc)
        ;
    printf("%0.f", nc);
    return 0;
}

我在分号上犯了一个错误,认为对于空的 for 循环,我们不需要分号,并且在循环内部声明了 double,这不符合 ANSI 标准,这混淆了问题的解释,对此我感到抱歉。


<details>
<summary>英文:</summary>

(FIXED)

MY ORIGINAL QUESTION (made few mistakes while framing the question) - 

#include <stdio.h>

int main()
{
for (double nc = 0; getchar() != EOF; ++nc)
printf("%0.f", nc);
return 0;
}

if i give input &quot;a&quot; (Exclude quotes), i get 01 as the output,
Input: &quot;ab&quot;, output: 012
Input: Return Key, Output: 0

Why is this happening? **Shouldn&#39;t \n give output 1**, as in my device the EOF value is -1 and ascii new line character value is 10, I am using VS code and Mac M1, FYI, same output even in online compiler.

I WROTE A WRONG VERSION OF WHAT I WANTED TO ASK

This is the original code from K&amp;R:

#include <stdio.h>

int main()
{
double nc;
for (nc = 0; getchar() != EOF; ++nc)
;
printf("%0.f", nc);
return 0;
}


**I made a mistake with the semi-colons and assumed that for empty ```for``` loop we don&#39;t need a semi-colons and i declared double inside the loop which was not ANSI standard, it messed up the question solvers interpretation, sorry for that.**

</details>


# 答案1
**得分**: 1

以下是您要翻译的内容:

你正在循环一个变量,从0开始,并且在每次循环迭代中始终递增它,直到遇到 `EOF`。

因此,你的输出将会是

**012345678910...**

如果你只想获取 EOL 之前的字符数,你可以这样做:

```c
#include <stdio.h>

int main()
{   
    double nc = 0;
    for (nc = 0; getchar() != EOF; ++nc);
    printf("%0.f", nc);
    return 0;
}

在这种情况下,空的 for 循环不会包含你的 printf,因此,在循环结束后只会打印一次结果。而在您的代码中,for 循环在每一步都调用 printf,打印出迄今为止的字符数。

英文:

You are looping a variable starting from 0 and you always increment it on each iteration of your loop until EOF.

Hence, your output will look like

012345678910...

If you just want to get the number of characters prior to EOL, then you can do this:

#include &lt;stdio.h&gt;

int main()
{   
    double nc = 0;
    for (nc = 0; getchar() != EOF; ++nc);
    printf(&quot;%0.f&quot;, nc);
    return 0;
}

The empty for cycle in this case will not contain your printf, hence, you will only print the result once, after the end of the loop. With your code the for cycle calls printf at each step, typing the number of characters so far.

答案2

得分: 1

在C语言中,for循环的执行顺序如下:

  1. 评估第一个头表达式
  2. 评估第二个头表达式;如果为真,跳转到第3步,否则跳转到第6步。
  3. 评估循环体
  4. 评估第三个头表达式
  5. 跳转到第2步
  6. 退出

如果你了解while循环的工作原理,这可能会对你有所帮助:

for (A; B; C)
    BODY

类似于

A

while (B) {
    BODY

    C
}

因此,在你的代码中,当你只按下回车键时,循环体会运行一次(对于'\n'),并打印出'0'。当你输入'a'时,循环体会运行两次(分别对于'a'和'\n'),因此输出为'01'。

注意:你的代码应该更像这样,使用现代C风格:

#include <stdio.h>

int main(void)
{   
    for (int nc = 0; getchar() != EOF; nc++)
        printf("%d", nc);

    return 0;
}
英文:

In C, the execution order of a for-loop is as follows:

  1. Evaluate the 1st header expression
  2. Evaluate the 2nd header expression; if true goto 3., else goto 6.
  3. Evaluate the body of the loop
  4. Evaluate the 3rd header expression
  5. goto 2.
  6. Exit

If you know how while-loops work, this might help you:

for (A; B; C)
    BODY

is similar to

A

while (B) {
    BODY

    C
}

So in the case of your code, when you only hit the enter key, the body of the loop runs once (for &#39;\n&#39;) and prints 0. When you enter a, the body of the loop runs twice (for both &#39;a&#39; and &#39;\n&#39;) and the output is thusly 01.

Note: Your code should probably look something more like this, using modern C style:

#include &lt;stdio.h&gt;

int main(void)
{   
    for (int nc = 0; getchar() != EOF; nc++)
        printf(&quot;%d&quot;, nc);

    return 0;
}

huangapple
  • 本文由 发表于 2023年5月26日 08:11:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76336907.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定