简体   繁体   English

printf() 正在打印错误的值

[英]printf() is printing the wrong value

This is my full code, and its printing random negative values each time I run it not sure what is wrong.这是我的完整代码,每次运行它时都会打印随机负值,不确定出了什么问题。 using Ubuntu to run and "gcc -Wall -Wextra test.c"使用 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;
}

You are passing the address of p .您正在传递p的地址。 You need to pass the value.您需要传递值。

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

As you have it, your code is printing the address of p , whatever that happens to be.正如您所拥有的那样,您的代码正在打印p的地址,无论发生什么。

In addition, since you are using unsigned int , you probably want to use the %u formatter insted of %d .此外,由于您使用的是unsigned int ,因此您可能希望使用%d%u格式化程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM