Why is my printf formatting now working with GCC but working on Windows

I'm starting to learn C. We just did a tutorial on pointers and I was not able to run the sample code on my Linux machine (Mint 17 64 bit), although it works fine on Windows 7 (32 bit). The code is as follows:

#include <stdio.h>

int main() {
        int var = 20; //actual variable declaration
        int *ip; //pointer declaration

        ip = &var; //store address of var in pointer

        printf("Address of var variable: %x\n", &var);

        //address stored in pointer variable
        printf("Address stored in ip variable: %x\n", ip);

        //access the value using the pointer
        printf("Value of *ip variable: %d\n", *ip);

        return 0;
}

The program works as expected in windows with ide code blocks, but on Linux trying to compile in the terminal using GCC, I get the following error:

pointers.c: In function ‘main’:
pointers.c:9:2: warning: format ‘%x’ expects argument of type ‘unsigned int’, but   argument 2 has type ‘int *’ [-Wformat=]
  printf("Address of var variable: %x\n", &var);
  ^
pointers.c:12:2: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int *’ [-Wformat=]
  printf("Address stored in ip variable: %x\n", ip);
  ^

I would like to know what is happening and how I can get the code to work on Linux.

Thank.

+4
source share
1 answer

%x unsinged int, undefined ( , , , , ). C99, 7.19.6.1 fprintf p.8, 9 ( )

o,u,x,X unsigned int (o), unsigned decimal (u) ( x x)

- , undefined.

, %p, %x %d, :

printf("%p\n", (void *) ip);
+6

All Articles