【发布时间】:2021-01-11 15:41:30
【问题描述】:
我在编译代码时收到警告。这些警告包括:format %lu expects argument of type 'long unsigned int' but argument 3 has type long long unsigned int & cast from printer to integer of different size。
我似乎不知道出了什么问题。非常感谢任何有关如何解决此问题的建议。
void printBits(size_t const size, void const * const ptr);
int main()
{
// variables are not in order on purpose for the first step in running the code.
float c;
char a;
double d;
int b;
d = 561232019; /* 8 bytes */
c = 154.6; /* 4 bytes */
b = -83273; /* 4 bytes */
a = 42; /* 1 byte */
printf("\n--------------------------------------------\n");
printf("LABEL - ADDRESS(hex) ADDRESS (dec) [S - E] - BINARY\n");
printf("A - ");
printf("%p - ",&a);
printf("%lu - %lu ",(long)&a, (long)&a + sizeof(a)-1); // the two errors occur at
//this line as well as the other identical print statements for each variable used. This print
//statement is identical to when using b, c, d and all have the same warnings. I just used this part
//of code to find out how to fix this error so I could fix all the other identical print statements.
printf("%d - ",a);
printBits(sizeof(a), &a);
printf("\n--------------------------------------------\n");
【问题讨论】:
-
请注意,
sizeof a在所有符合标准的平台上都是 1,所以sizeof(a)-1 == 0。 C99 6.5.3.4 sizeof 运算符 - 3:当应用于具有 char、unsigned char 或 signed char 类型(或其限定版本)的操作数时,结果为 1....
标签: c printf stdio integer-promotion