Search code examples
cprintfarithmetic-expressions

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;
}

Solution

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