为什么我不能用scanf输入字符

发布于 2025-01-14 19:08:06 字数 196 浏览 3 评论 0原文

为什么这不起作用?我是 C 新手... scanf 函数与其他数据类型一起工作得很好,只是 char 没有给我输入字符的选项

char grade;
printf("Enter your grade: ");
scanf("%c", &grade);
printf("Your grade is %c", grade);

why is this not working? im new to C...
the scanf function works just fine with other data types, its just the char thats not giving me the option to input a character

char grade;
printf("Enter your grade: ");
scanf("%c", &grade);
printf("Your grade is %c", grade);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花细雨 2025-01-21 19:08:06

看来在输入字符之前,原始程序中已经输入了其他对象。

在这种情况下,您需要编写

scanf(" %c", &grade);

注意转换说明符%c之前的空白。它允许跳过空白字符。

It seems before entering the character there are inputs of other objects in your original program.

In this case you need to write

scanf(" %c", &grade);

Pay attention to the blank before the conversion specifier %c. It allows to skip white space characters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文