我的类赋值要求我提示用户在一个输入行中输入四个变量char float int char.
这是整个代码:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
int main(void){
char h = 'a';
char b, c, d, e;
int m, n, o;
float y, z, x;
short shrt = SHRT_MAX;
double inf = HUGE_VAL;
printf("Program: Data Exercises\n");
printf("%c\n", h);
printf("%d\n", h);
printf("%d\n", shrt);
printf("%f\n", inf);
printf("Enter char int char float: ");
scanf("%c %d %c %f", &b, &m, &c, &y);
printf("You entered: '%c' %d '%c' %.3f \n", b, m, c, y);
Run Code Online (Sandbox Code Playgroud)
这部分代码是我遇到问题的地方.
printf("Enter char float int char: ");
scanf("%c %f %d %c", &d, &z, &n, &e);
printf("You entered: '%c' %f %d '%c' \n", d, z, n, e);
Run Code Online (Sandbox Code Playgroud)
如果我隔离上面的部分,这部分是有效的.
printf("Enter an integer value: ");
scanf("%d", &o);
printf("You entered: %15.15d \n", o);
printf("Enter a float value: ");
scanf("%f", &x);
printf("You entered: %15.2f \n", x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
由于没有足够高的rep,因此我无法发布图像,因此在运行程序时,我将提供指向控制台屏幕截图的链接.

如果有人能向我解释为什么程序无法正常工作,我将非常感激.提前致谢.
您在此行中有错误:
scanf("%c %d %c %f", &b, &m, &c, &y);
Run Code Online (Sandbox Code Playgroud)
您需要先添加一个空格%c.
试试这一行
scanf(" %c %d %c %f", &b, &m, &c, &y); // add one space %c
scanf(" %c %f %d %c", &d, &z, &n, &e);
Run Code Online (Sandbox Code Playgroud)
这是因为在输入数字并按ENTER后,新行将保留在缓冲区中,并由下一个处理scanf.