英文:
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 "a" (Exclude quotes), i get 01 as the output,
Input: "ab", output: 012
Input: Return Key, Output: 0
Why is this happening? **Shouldn'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&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'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 <stdio.h>
int main()
{
double nc = 0;
for (nc = 0; getchar() != EOF; ++nc);
printf("%0.f", 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循环的执行顺序如下:
- 评估第一个头表达式
- 评估第二个头表达式;如果为真,跳转到第3步,否则跳转到第6步。
- 评估循环体
- 评估第三个头表达式
- 跳转到第2步
- 退出
如果你了解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:
- Evaluate the 1st header expression
- Evaluate the 2nd header expression; if true goto 3., else goto 6.
- Evaluate the body of the loop
- Evaluate the 3rd header expression
- goto 2.
- 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 '\n'
) and prints 0
. When you enter a
, the body of the loop runs twice (for both 'a'
and '\n'
) and the output is thusly 01
.
Note: Your code should probably look something more like this, using modern C style:
#include <stdio.h>
int main(void)
{
for (int nc = 0; getchar() != EOF; nc++)
printf("%d", nc);
return 0;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论