字符在c中未按预期工作 请考虑下面的简单程序: int main(void) { //exercise 1 float num2; printf("please enter a number \n"); scanf_s("%f", &num2); printf("the number multiple by 3 is %3.3f\n", num2 * 3); //exercise 2 char ch1, ch2, ch3, ch4; printf("enter a word with four char\n"); ch1 = getchar(); ch2 = getchar(); ch3 = getchar(); ch4 = getchar(); printf("the chars in reverse order are\n"); putchar(ch4); putchar(ch3); putchar(ch2); putchar(ch1); putchar('\n'); }
输出为:字符在c中未按预期工作 请考虑下面的简单程序: int main(void) { //exercise 1 float num2; printf("please enter a number \n"); scanf_s("%f", &num2); printf("the number multiple by 3 is %3.3f\n", num2 * 3); //exercise 2 char ch1, ch2, ch3, ch4; printf("enter a word with four char\n"); ch1 = getchar(); ch2 = getchar(); ch3 = getchar(); ch4 = getchar(); printf("the chars in reverse order are\n"); putchar(ch4); putchar(ch3); putchar(ch2); putchar(ch1); putchar('\n'); },c,scanf,fgets,C,Scanf,Fgets,输出为: please enter a number 2 the number multiple by 3 is 6.000 enter a word with four char ffff the chars in reverse order are fff 如果我将练习2的代码块移到1上面,控制台上会打印3个字符: int main(void) { //exercise 2 char ch1, ch2, ch3, ch4; printf("enter a word
please enter a number
2
the number multiple by 3 is 6.000
enter a word with four char
ffff
the chars in reverse order are
fff
如果我将练习2的代码块移到1上面,控制台上会打印3个字符:
int main(void)
{
//exercise 2
char ch1, ch2, ch3, ch4;
printf("enter a word with four char\n");
ch1 = getchar();
ch2 = getchar();
ch3 = getchar();
ch4 = getchar();
printf("the chars in reverse order are\n");
putchar(ch4);
putchar(ch3);
putchar(ch2);
putchar(ch1);
putchar('\n');
//exercise 1
float num2;
printf("please enter a number \n");
scanf_s("%f", &num2);
printf("the number multiple by 3 is %3.3f\n", num2 * 3);
}
结果如预期:
enter a word with four char
ffff
the chars in reverse order are
ffff
please enter a number
2
the number multiple by 3 is 6.000
我想知道,当我改变代码块的顺序时,它为什么会工作,以及如何解决它,谢谢
想知道当我改变代码块的顺序时它为什么会起作用,以及如何解决它
这是因为scanf_s(“%f”和&num2)代码>在输入缓冲区中保留换行符。因此,您的第一个getchar()
将该换行符解释为ch1
对于这种情况,前面的静默getchar
将执行以下操作:
getchar(); // will consume the remaining newline from stdin
ch1 = getchar();
ch2 = getchar();
ch3 = getchar();
ch4 = getchar();
想知道当我改变代码块的顺序时它为什么会起作用,以及如何解决它
这是因为scanf_s(“%f”和&num2)代码>在输入缓冲区中保留换行符。因此,您的第一个getchar()
将该换行符解释为ch1
对于这种情况,前面的静默getchar
将执行以下操作:
getchar(); // will consume the remaining newline from stdin
ch1 = getchar();
ch2 = getchar();
ch3 = getchar();
ch4 = getchar();
输入第一个浮点数时有一个换行符,通过调用getchar
将其作为字符输入。解决此问题的另一种方法是使用fgets
将整行作为字符串,然后使用您想要的任何格式对其进行解析:
char line[512];
printf("please enter a number \n");
fgets(line, sizeof line, stdin); // the newline is consumed here
sscanf(line, "%f", &num2);
ch1 = getchar(); // working as expected
输入第一个浮点数时有一个换行符,通过调用getchar
将其作为字符输入。解决此问题的另一种方法是使用fgets
将整行作为字符串,然后使用您想要的任何格式对其进行解析:
char line[512];
printf("please enter a number \n");
fgets(line, sizeof line, stdin); // the newline is consumed here
sscanf(line, "%f", &num2);
ch1 = getchar(); // working as expected
非常常见的常见问题。输入缓冲区中还有新行字符。例如,请参阅链接的副本,部分“当*scanf()无法按预期工作时”。非常常见的常见问题解答。输入缓冲区中还有新行字符。例如,请参阅链接的副本,部分“When*scanf()未按预期工作”。使用fflush将不起作用,因为它不是为输入缓冲区定义的。请从答案中删除该部分,因为它不正确并导致调用未定义的行为。啊,对了,fflush
不是标准的,tks。同意并删除该部分。使用fflush将不起作用,因为它不是为输入缓冲区定义的。请从答案中删除该部分,因为它不正确并导致调用未定义的行为。啊,对了,fflush
不是标准的,tks。同意并删除该部分。。