问题描述
我正在参加有关"C"的培训课程,遇到了问题.很难解释,所以我将发布代码.这是训练语法,所以不要问我为什么这样做了.当这两个段块都在main()中运行时,第二个块的行为就好像它单独存在于main()中一样.我已经尝试了几种IDE,认为这可能是一个错误.
I am taking a training course on "C" and running into a problem. It's hard to explain so I'll post the code. This is training syntax, so don't ask me why it's done the way it is. When both of these segment blocks are run in a main(), the second block does not behave as if it exists alone in the main(). I have tried several IDE thinking it might be a bug.
/* First Segment Block */
int c;
printf("Type a letter: ");
c = getchar();
printf("You typed '%c'\n",c);
/* OR - outputs the same, to demonstrate putchar */
printf("You typed '");
putchar(c);
printf("'\n\n");
/* Second Segment Block */
int a,b,d;
printf("Type two letters: ");
a = getchar();
b = getchar();
d = getchar();
printf("You typed '");
putchar(a);
printf("' and '");
putchar(b);
printf("' and '");
putchar(d);
printf("'\n");
在第二段代码中,我添加了第三个变量来测试我的理论.当您键入所请求的2个字母时,第一个getchar()选择一个新行,第二个getchar()选择一个第一个字母.第三个getchar()接收第二个字母.如果注释掉整个第一个段块,则它的行为正确,第一个getchar()拾取第一个字母,第二个getchar()拾取第二个字母,按预期显示输出.
In the second segment block, I added a 3rd variable to test my theory. When you type the requested 2 letters, the first getchar() picks up a new line and the second getchar() picks up the first letter. The 3rd getchar() picks up the second letter. If you comment out the entire first segment block, then it behaves correctly, picking up the first letter by the first getchar() and the second letter by the second getchar(), displaying the output as expected.
这是两者一起运行时的输出.
Here is the output when both run together.
Type a letter: k
You typed (k)
You typed 'k'
Type two letters: lk
You typed '
' and 'l' and 'k'
RUN SUCCESSFUL (total time: 9s)
单独运行它们时,输出如下.
When they are run individually, the output is below.
第一段输出.
Type a letter: k
You typed (k)
You typed 'k'
RUN SUCCESSFUL (total time: 5s)
第二段输出.
Type two letters: rf
You typed 'r' and 'f' and '
'
RUN SUCCESSFUL (total time: 5s)
第三个getchar()是换行符,这是预期的.
The 3rd getchar() is a newline and that is expected.
谁能解释为什么当两个段都在同一个main()中运行时,其行为与单独运行时的行为不同吗?
Can anyone explain why when both segments are run in the same main(), the behavior is different from when run seperate?
预先感谢您,Daniel N.(C语言初学者)
Thank you in advance,Daniel N. (C language beginner)
推荐答案
在第一个提示上,键入 之类的内容,因此输入流中包含字符'a', '\n'
.第一个getchar
调用读取a
并将换行符保留在输入流中.
On the first prompt, you type something like , so your input stream contains the characters 'a', '\n'
. The first getchar
call reads the a
and leaves the newline in the input stream.
作为第二个提示的响应,您键入 ,因此您的输入流现在包含'\n', 'b', 'c', '\n'
.
In response to the second prompt, you type , so your input stream now contains '\n', 'b', 'c', '\n'
.
您可能会发现这里发生了什么-下一个getchar
调用从输入流中读取换行符.
You can probably figure out what happens from here - the next getchar
call reads that newline character from the input stream.
有两种方法可以解决此问题.一种是测试您的输入,如果是换行符,则再试一次:
There are a couple of ways to deal with this. One is to test your input, and try again if it's a newline:
do
{
a = getchar();
} while ( a == '\n' ); // or while( isspace( a )), if you want to reject
// any whitespace character.
另一种是不使用getchar
;而是将scanf
与%c
转换说明符一起使用,并在格式字符串中使用空格:
Another is to not use getchar
; instead, use scanf
with the %c
conversion specifier and a blank space in the format string:
scanf( " %c", &c ); // you will need to change the types of your
... // variables from int to char for this.
scanf( " %c", &a );
scanf( " %c", &b );
scanf( " %c", &c );
格式字符串中的前导空格告诉scanf
忽略任何前导空格,因此您不会选择换行符.
The leading space in the format string tells scanf
to ignore any leading whitespace, so you won't pick up the newline character.
这篇关于无法弄清楚为什么getchar()在C中首次出现换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!