我的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

#define NUM1 5.557111111111111
#define NUM2 1937006915
#define NUM3 1668248096
#define NUM4 8555

#include <stdio.h>

typedef union {
char  * a;
double num;
int * i;
}onion;

int main(int argc, char ** argv)
{
onion myoni;
char array[] = "TESTING";
int array2[] = { NUM2, NUM1, NUM3 };
myoni.a = array;
printf("char: %s, %d\n",myoni,sizeof(myoni.a));
myoni.num = NUM1;
printf("double: %10.15f, %d\n", myoni.num, sizeof(myoni.num));
myoni.i = array2;
printf("int: %d %d %d, %d\n", myoni.i[0], myoni.i[1],myoni.i[2], sizeof(myoni.i));

    return 0;
}

最佳答案

而不是:

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

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

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

关于c - 较长的无符号整数的printf格式字符串警告,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9318333/

10-11 20:58