printf()打印错误的值

Tho*_*mas 1 c printf arithmetic-expressions

这是我的完整代码,每当我运行它时,它都会打印随机的负值,不确定是否出了问题。使用Ubuntu来运行和“ gcc -Wall -Wextra test.c”

#include <stdio.h>
int main () {

unsigned int x = 10;
unsigned int y = 16;
unsigned int p = x + y;

printf("%d\n", &p);

return 0;
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*ood 8

您正在传递的地址p。您需要传递值。

printf("%d\n", p);
Run Code Online (Sandbox Code Playgroud)

有了它,您的代码就会打印出的地址p,无论发生什么情况。

另外,由于正在使用unsigned int,您可能要使用%d%u格式化程序。