【发布时间】:2021-12-18 20:40:26
【问题描述】:
程序不会在 scanf("%c", &ch) 行上停止。为什么会这样,有人能解释一下吗
#include<stdlib.h>
#include<stdio.h>
struct list {
char val;
struct list * next;
};
typedef struct list item;
void main()
{
char ch;
int num;
printf("Enter [1] if you want to use linked list or [2] for realloc\n");
scanf("%d", &num);
if(num == 2)
{
scanf("%c", &ch);
printf("%c", ch);
}
}
【问题讨论】:
-
基本上,虽然
scanf的几乎所有其他格式说明符都会去除前导空格,但%c是个怪人。你必须非常小心地对待它。
标签: c