Colabambino
Colabambino

Reputation: 504

Basic program, Interesting output? Explanation

I'm learning about pointers, and I'm running this program from the tutorial that gives some interesting output.

#include <stdio.h>

int main () {

    int var = 20; /*actual variable declaration */
    int *ip; /*pointer variable declaration*/

    ip = &var; /*store addres of var in ip variable*/

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

    /*address stored in ip variable*/
    printf("Address of stored in ip variable: %x\n", ip);
    /*access the value using pointer */
    printf("value of *ip variable: %x\n", *ip);

    return 0;

}

And to my understanding, the site says my output should be Two matching memory locations, and the value of 20 for the "Value of *ip variable" part. I have no issue with the memory locations, they both come up as the same just as one would assume however, the last part, its telling me the value of the *ip variable is 14?

Address of var variable: 6d73b8cc
Address of stored in ip variable: 6d73b8cc
value of *ip variable: 14

Whats going on here? Compiled with gcc, which gives one warning about format %x expects an unsigned integer but argument 2 has type of....

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

Correct me, please, if I am wrong, but could it be due to the format, which when expecting an unsigned integer could cause the number to be one other than 20? being an unsigned integer is within values 0 to 65,535 or 0 to 4,294,967,295, while an integer is within values -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647?

Upvotes: 3

Views: 333

Answers (5)

user3629249
user3629249

Reputation: 16550

printing addresses with printf() should be done with '%p' not '%x'.

If you compiled with all the warnings enabled (for gcc, at a minimum use: -Wall -Wextra -pedantic ) then the compiler would have told you, for line 10 and line 13:

warning: format: '%x' expects argument of type 'unsigned int', but argument two has type 'int*' [-Wformat=]`   

The last call to printf() is passing a type of 'int' but the '%x' is expecting a unsigned int.

suggest declaring 'ip' and 'var' as unsigned int rather than int

or change the last printf() to:

printf("value of *ip variable: %d\n", *ip);

you can read.learn about the format specifiers for printf() at:

<http://www.codingunit.com/printf-format-specifiers-format-conversions-and-formatted-output>

or reading the printf man page

Upvotes: 1

Peter
Peter

Reputation: 36637

The first two printf() statements have undefined behaviour, as %x requires the corresponding parameter to be of type unsigned, not a pointer. Hence the warnings. Look up the %p format if you want to print the value of pointers (and convert the pointers to (void *) to use that).

%x prints the value in hex, base 16. The value of 20 (decimal) in hex is 14 (1*16 + 4). Strictly speaking, you should convert an int to unsigned to use %x.

Upvotes: 2

mksteve
mksteve

Reputation: 13085

Some fixes :-

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

This is incorrect, a pointer should be printed as a pointer value

printf("Address of var variable: %p\n", &var); /* << ensure pointer value */

When you call printf, it mixes fixed text with parameters in order...

printf( "hello %d world %d today %d\n", 1,2,3 );  

Prints hello 1 world 2 today 3

The way a parameter is displayed is based on the format specifier.

%s /* displays as a string */
%d /* displays as an integer in normal decimal */
%x /* displays an integer in hexa-decimal. */
%f /* displays as a floating point */
%p /* displays a pointer address */

Decimal goes      0,1,2,..,8,9,10,11,...,14,15,16,17,...,20
Hexadecimal goes  0,1,2,..,8,9, A, B,..., E, F,10,11,...,14

Upvotes: 5

Danny_ds
Danny_ds

Reputation: 11406

You are printing the hexadecimal value of var or *ip, which is 0x14.

Use %d for decimal value.

Upvotes: 4

David Hoelzer
David Hoelzer

Reputation: 16379

What's happening is that you've printed the integer value 20 as a hexadecimal value, which rightly is displayed as 14. I suspect you were expecting 20 decimal. :)

Upvotes: 10

Related Questions