我只是在学习C并制作一个基本的"hello,NAME"程序.我已经让它工作来读取用户的输入,但它输出为数字而不是它们输入的内容?
我究竟做错了什么?
#include <stdio.h>
int main()
{
char name[20];
printf("Hello. What's your name?\n");
scanf("%d", &name);
printf("Hi there, %d", name);
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Sad*_*que 17
您使用错误的格式说明符%d
- 您应该使用%s
.更好的是仍然使用fgets - scanf不是缓冲区安全的.
浏览文档不应该那么困难:
示例代码:
#include <stdio.h>
int main(void)
{
char name[20];
printf("Hello. What's your name?\n");
//scanf("%s", &name); - deprecated
fgets(name,20,stdin);
printf("Hi there, %s", name);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输入:
The Name is Stackoverflow
Run Code Online (Sandbox Code Playgroud)
输出:
Hello. What's your name?
Hi there, The Name is Stackov
Run Code Online (Sandbox Code Playgroud)
#include <stdio.h>
int main()
{
char name[20];
printf("Hello. What's your name?\n");
scanf("%s", name);
printf("Hi there, %s", name);
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
64965 次 |
最近记录: |