1 Question: Can '&' be used in 'printf' function? [duplicate]

question created at Sat, Jun 1, 2019 12:00 AM

This question already has an answer here:

Here is mentioned that we can use & in 'printf' function, in order to get the address of a variable, this is the code it uses:

 * C program to get memory address of a variable

#include <stdio.h>

int main()
    int num = 10;

    printf("Value of num   = %d\n", num);

    /* &num gets the address of num. */
    printf("Address of num = %d\n", &num);

    printf("Address of num in hexadecimal = %x", &num);

    return 0;

and the output:

Value of num = 10

Address of num = 6356748

Address of num in hexadecimal = 60ff0c

But when I compile the code(saved as a.c) with gcc -o a a.c, I get the following warning:

a.c: In function ‘main’:

a.c:14:12: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=] printf("Address of num = %d\n", &num); ^

a.c:16:12: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int *’ [-Wformat=] printf("Address of num in hexadecimal = %x", &num);

My question is:

  • Can & be used in printf function?
  • If Yes, why do I get the warning?
  1. %d means int. &num is a pointer, not an int. %p is one alternative.
    2019-06-01 04:43:47Z
  2. Unfortunately, the answer has been closed while I was writing my answer: It depends on the CPU type (and the operating mode) if this is possible or not. Using an m68k, a TriCore or an x86 running in 16-bit mode with a "far" memory layout, it is not possible to use "%d" together with a pointer.
    2019-06-01 05:05:45Z
  3. @MartinRosenau no it doesn't depend. The behaviour is undefined.
    2019-06-01 05:14:01Z
  4. @MartinRosenau you're welcome to add an answer to the duplicate. It does not make sense debating the same misconceptions every time in answers to each question.
    2019-06-01 05:15:41Z
  5. Undefined behaviour is a term from the C standard. You cannot answer these kinds of C questions correctly without understanding the term. Please read the definition.
    2019-06-01 05:42:17Z
1 Answers 1

Can & be used in printf function?

Yes. The unary "address of" prefix & operator can be used in many contexts requiring an expression.

If yes, why do I get the error?

It is not an error, but a useful warning. Your compiler is helping you.

On many computers and ABIs (including my Linux/Debian/x96-64) a pointer has 8 bytes, and an int only 4 bytes. Check by using sizeof.

You need to cast (do a type conversion) to avoid the warning.

printf("address of num=%d=%#x\n", (int)&num, (int)&x);

But the proper way to print an address is (with the (void*) just for readability, you can remove it if you want to):

printf("address of num is %p\n", (void*) &num);

Please read the documentation of printf before using it

And take the good habit of reading the documentation of any function that you are using in your code. With current web searching technologies, finding that is remarkably easy.

Ability to read and understand documentation is one of the most important skills in software development. Read also Norvig's Teach yourself programming in ten years, a mandatory food for thought, with Lippert's How to debug small programs blog. Read also What every C programmer should know about undefined behavior, it is relevant too.

2019-06-01 12:31:34Z
  1. I don't think that (void*) cast is needed, or even recommended.
    2019-06-01 06:14:12Z
  2. Many people on StackOverflow have a different opinion, and at least it makes the code readable; the generated binary won't change.
    2019-06-01 06:25:49Z
  3. Perhaps, but that's totally irrelevant. Whether it's needed or not is not a matter of opinion. And if it's not needed, it makes the code error-prone (and by definition, less readable, as the reader now needs to figure out why it's there).
    2019-06-01 06:26:33Z
  4. IMHO it is increasing code readability
    2019-06-01 06:27:00Z
source placed here