简体   繁体   中英

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"

#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 . 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.

In addition, since you are using unsigned int , you probably want to use the %u formatter insted of %d .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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