gpt4 book ai didi

char 在 c 中没有按预期工作

转载 作者:太空宇宙 更新时间:2023-11-04 00:45:41 25 4
gpt4 key购买 nike

<分区>

你好,请考虑下面的简单程序:

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');
}

输出是:

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

打印到控制台的 3 个字符,如果我将练习 2 的代码块移动到 1 之上:

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

我想知道为什么当我改变代码块的顺序时它会起作用,我该如何解决,谢谢。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com