scanf的默认值

时间:2012-12-12 14:01:05

标签: c scanf

我想做这样的事情:

char name[20];
printf("Enter your name: ");
scanf("Micheal%s", &name);

我希望Michael可以在控制台上编辑,如果用户按 Enter 而不编辑任何内容,则将name设置为Micheal。有没有简单的方法呢?

2 个答案:

答案 0 :(得分:4)

不,没有。

“控制台”和“鼠标”不是C指定存在的东西。

您需要查看库,例如ncurses来执行此类操作。

答案 1 :(得分:0)

不可编辑,但可能不太糟糕:

char name[20] = "Michael";

printf("Enter your name [default: %s]: ", name);
fflush (stdout); /* Makes the printf output appear even without a newline. */
scanf("%s", &name);

有关使用%s扫描到小缓冲区而不测试scanf返回值的所有警告都适用。