C 长无符号整数的printf格式字符串警告

C 长无符号整数的printf格式字符串警告,c,linux,C,Linux,我的c代码有问题,当我尝试打印相同的值时,g++编译器给出了一个关于长无符号int的错误。以下是我的代码和我得到的错误: as3.c: warning: format %s expects type char *, but argument 2 has type onion as3.c: warning: format %d expects type int, but argument 3 has type long unsigned int as3.c: warning: format %d

我的c代码有问题,当我尝试打印相同的值时,g++编译器给出了一个关于长无符号int的错误。以下是我的代码和我得到的错误:

as3.c: warning: format %s expects type char *, but argument 2 has type onion
as3.c: warning: format %d expects type int, but argument 3 has type long unsigned int
as3.c: warning: format %d expects type int, but argument 3 has type long unsigned int
as3.c: warning: format %d expects type int, but argument 5 has type long unsigned int
as3.c: warning: format %d expects type int, but argument 2 has type long unsigned int

对于第一个警告,不要使用洋葱使用洋葱

而不是:

printf("char: %s, %d\n",myoni,sizeof(myoni.a));
使用:

也就是说,为s转换说明符传递一个char*,并对sizeof运算符的结果使用%zu转换规范。

将值从sizeof强制转换为无符号long,类型为size\u t,并且在C99中,可以使用%zu打印类型size\u t的值,并在printf说明符中使用%lu

printf("%lu\n", (unsigned long)sizeof something);
printf("%lu\n", (unsigned long)sizeof (sometype));
/* C99 below */
printf("%zu\n", sizeof something);
printf("%zu\n", sizeof (sometype));
洋葱类型有一个char*类型的成员。我猜这就是你想印的

printf("%s\n", myoni.a);

第一个警告是由这一行引起的:

printf("char: %s, %d\n",myoni,sizeof(myoni.a));
%s需要char*类型,但您传递了onion类型的变量。您需要显式使用myoni的a成员,该成员具有所需的char*类型:

printf("char: %s, %d\n",myoni.a,sizeof(myoni.a));
第二个警告是由于使用了sizeof宏,该宏返回一个长的无符号int,如警告消息中所述。长无符号整数的格式说明符为%lu长无符号,而不是%d。i、 e:


我想是%lu,但无论如何,谢谢你。仍然无法修复第一个错误我在哪里使用洋葱而不是洋葱第一个,使用myoni.a。不过,这里的sizeof只能提供char*的大小。与sizeofmyoni.i相同,将返回int指针的大小。sizeof是运算符,而不是宏;它返回一个size_t类型的值,该值可能对应于,也可能不对应于不同实现中的长无符号int。
printf("%s\n", myoni.a);
printf("char: %s, %d\n",myoni,sizeof(myoni.a));
printf("char: %s, %d\n",myoni.a,sizeof(myoni.a));
printf("char: %s, %lu\n",myoni.a,sizeof(myoni.a));