C 语言中的 While 循环帮助
我是 C 编程新手,我有 Java 背景。我想知道为什么在下面的代码中,在 while 循环中我必须输入输入十次,然后显示所有十个输入。我正在尝试输入一些内容,然后立即显示它。然后继续输入我的其他输入。
#include <stdio.h>
#include <stdlib.h>
#include "Helper.h"
main(){
print(PROGRAM_INFO); //prints program name and author
print(PROMPT);
char input [100]; //array to hold input from user
int isActive = 1; //1 continue shell, 0 terminate shell
int count = 0;
while (isActive == 1 && count < 10){
print(PROMPT);
++count;
scanf("%s", input);
print(input);
}
}
I'm new to C programming, I come from a Java background. I was wondering why in the following code, in the while loop I have to type my input ten times and then all ten inputs are displayed. I'm trying to type something once and have it displayed right after. Then continue typing my other inputs.
#include <stdio.h>
#include <stdlib.h>
#include "Helper.h"
main(){
print(PROGRAM_INFO); //prints program name and author
print(PROMPT);
char input [100]; //array to hold input from user
int isActive = 1; //1 continue shell, 0 terminate shell
int count = 0;
while (isActive == 1 && count < 10){
print(PROMPT);
++count;
scanf("%s", input);
print(input);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您的
print
是复制/粘贴错误,对吗?它应该是printf
。而且你真的不应该直接以 printf 的格式打印用户字符串。
想象一下用户输入
"%d%f%s\a%c"
...使用
格式中的 '\n' 时,您不需要
fflush(stdout);
因为 stdout 默认情况下是行缓冲的,而 '\n' 会自行执行一个操作。此外,如果用户输入
"%d%f%s\a%c"
这就是您打印的内容。最好的办法是
将 '\n' 添加到输出中,并且格式字符串没有问题。
Your
print
is a copy/paste error, right? It should beprintf
.And you really shouldn't print the user string directly in the format of printf.
Imagine the user types
"%d%f%s\a%c"
...The best thing to do is
With the '\n' in the format, you don't need to
fflush(stdout);
because stdout is line buffered by default and the '\n' does one on its own.Besides, if the user types
"%d%f%s\a%c"
that's what you get printed.The best thing to do is
puts adds the '\n' to the output and has no problems with format strings.
尝试在每次
print(input)
后刷新fflush(stdout)
Try flushing
fflush(stdout)
after eachprint(input)
尝试在打印中添加“\n”字符。流只是没有被刷新。
Try putting a '\n' character in the print. The stream just isn't being flushed.
在最终打印语句之后,您可能可以调用 fflush(stdout);将标准输出刷新到屏幕。
after your final print statement you could probably call fflush(stdout); to flush stdout to the screen.
您需要刷新 STDOUT。
fflush(STDOUT)
应该做到这一点,在打印之后添加。You need to flush STDOUT.
fflush(STDOUT)
should do it, added after the print.默认情况下,输入/输出是缓冲的,即输入和输出字节在显示到流之前存储在字节数组中。
系统中的BUFSIZ一般是1024的倍数。虽然printf是行缓冲的,但是缓冲
当遇到换行符时会自动刷新。
fflush(stdout) 导致缓冲数据刷新到输出流,在本例中是标准输出。
您可以使用 setvbuf() 函数控制缓冲区处理
by default input/output is buffered, i.e the input and output bytes are stored in a byte array before being displayed to the stream.
BUFSIZ in systems is generally a multiple of 1024.Although printf is line buffered, the buffer
is flushed automatically when a newline is encountered.
fflush(stdout) causes the buffered data to be flushed to output stream which in this case is stdout.
you can control buffer handling using setvbuf() function