我编写了一个程序来逐个字符地读取输入内容并将其打印输出,这是我的代码:
#include <stdio.h>
main()
{
int c;
while((c = getchar()) != EOF)
{
printf("%s\n", "log1");
printf("%c\n", c);
printf("%s\n", "log2");
}
}
结果如下:
a(my input)
log1
a
log2
log1
log2
但它应具有以下结果:
a
log1
a
log2
这个程序有什么问题?
您输入a和换行符
a(my input) You are giving a and newline
//this is because of a
log1
a
log2
//this is because of newline
log1
log2
检查换行符,并避免打印换行符。
while((c = getchar()) != EOF)
{
if(c!='\n')
{
printf("%s\n", "log1");
printf("%c\n", c);
printf("%s\n", "log2");
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句