[新手求助]关于c的问题

青春永不毕业 2015-05-15 05:33:36
include <stdio.h>
#include <string.h>
int main(void)
{
char name [20];
printf("你丫的叫啥?\n");
scanf("%s",&name);
printf("“%s”\n",name);
printf("“%20s”\n",name);
printf("“%-20s”\n",name);
printf("“%*s”\n",strlen(name)+2,name);
return(0);
}
请问上面这一串代码哪里出错了啊,新人自学不懂啊,错误警告是这样的:错误警报
2.c: In function ‘main’:
2.c:7:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[40]’ [-Wformat=]
scanf("%s",&name);
^
2.c:11:2: warning: field width specifier ‘*’ expects argument of type ‘int’, but argument 2 has type ‘size_t’ [-Wformat=]
printf("“%*s”\n",strlen(name)+2,name);
下面这一串为啥又是对的呢?
#include<stdio.h>
#include<string.h>
int main(void)
{
char name[20];
int width;
printf("Please input your name:");
scanf("%s",name);
printf("A:\"%s\"\n",name);
printf("B:\"%20s\"\n",name);
printf("C:\"%-20s\"\n",name);
width=strlen(name)+3;printf("D:\"%*s\"\n",width,name);//*对应width
return(0);
}
...全文
142 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
青春永不毕业 2015-05-18
  • 打赏
  • 举报
回复
引用 5 楼 q3733353520 的回复:
[quote=引用 3 楼 chuzhouzhi 的回复:] [quote=引用 1 楼 q3733353520 的回复:] 第二个参数会和第一个参数配对 %s 对应第二个参数应该是对应字符串的地址 对于 char name[20]; name 就是其地址,而&name为其地址的地址
谢谢啊,今天我一直试也逝去了,但请问为啥我char类型的不用&,但是float类型的又要&呢?在网上没看到相关的[/quote] 不是char类型不用& 而是 char name[20]中的name 表示的是char型数组,而name可以表示数组的首地址, 如果是
char name;就需要&了   而且对应的是%c
[/quote] 哦,好的,谢谢了,知道了
  • 打赏
  • 举报
回复
引用 3 楼 chuzhouzhi 的回复:
[quote=引用 1 楼 q3733353520 的回复:] 第二个参数会和第一个参数配对 %s 对应第二个参数应该是对应字符串的地址 对于 char name[20]; name 就是其地址,而&name为其地址的地址
谢谢啊,今天我一直试也逝去了,但请问为啥我char类型的不用&,但是float类型的又要&呢?在网上没看到相关的[/quote] 不是char类型不用& 而是 char name[20]中的name 表示的是char型数组,而name可以表示数组的首地址, 如果是
char name;就需要&了   而且对应的是%c
  • 打赏
  • 举报
回复
第二个参数会和第一个参数配对 %s 对应第二个参数应该是对应字符串的地址 对于 char name[20]; name 就是其地址,而&name为其地址的地址
青春永不毕业 2015-05-16
  • 打赏
  • 举报
回复
引用 2 楼 Idle_Cloud 的回复:
2.c:7:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[40]’ [-Wformat=] scanf("%s",&name); ^ 2.c:11:2: warning: field width specifier ‘*’ expects argument of type ‘int’, but argument 2 has type ‘size_t’ [-Wformat=] printf("“%*s”\n",strlen(name)+2,name); 这两个其实都是类型的问题,都是可以运行,没问题的。 只是需要char * , int, 然后参数却是char *[], size_t
谢谢,嗯嗯,出现了警告但是可以运行
青春永不毕业 2015-05-16
  • 打赏
  • 举报
回复
引用 1 楼 q3733353520 的回复:
第二个参数会和第一个参数配对 %s 对应第二个参数应该是对应字符串的地址 对于 char name[20]; name 就是其地址,而&name为其地址的地址
谢谢啊,今天我一直试也逝去了,但请问为啥我char类型的不用&,但是float类型的又要&呢?在网上没看到相关的
Carl_CCC 2015-05-16
  • 打赏
  • 举报
回复
2.c:7:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[40]’ [-Wformat=] scanf("%s",&name); ^ 2.c:11:2: warning: field width specifier ‘*’ expects argument of type ‘int’, but argument 2 has type ‘size_t’ [-Wformat=] printf("“%*s”\n",strlen(name)+2,name); 这两个其实都是类型的问题,都是可以运行,没问题的。 只是需要char * , int, 然后参数却是char *[], size_t

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧