我想做这样的事情:
char name[20];
printf("Enter your name: ");
scanf("Micheal%s", &name);
我希望Michael
可以在控制台上编辑,如果用户按 Enter 而不编辑任何内容,则将name设置为Micheal
。有没有简单的方法呢?
答案 0 :(得分:4)
答案 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返回值的所有警告都适用。