0

I have this code:

#include <stdio.h>
#include <string.h>

void main(){
       printf("%p");
}

This is the output: 0x7ffdd9b973d8

I know %p stands for pointer and when using it as for example

#include <stdio.h>
#include <string.h>

void main(){
 int i = 0;
 printf("%p", i);
}

it returns the pointer address of i. But my question is what does it return when not adding any other argument in the printf function just printf("%p")

Suraj Jain
  • 4,463
  • 28
  • 39
Rattata 2me
  • 37
  • 2
  • 5
  • 9
    `printf("%p", i);` does not print address of `i`. You'd need `printf("%p", (void *) &i);` for that. – AnT stands with Russia Feb 08 '17 at 18:50
  • 1
    I think it's just a random address in memory. Well, not random as in generated, but not predictable directly. Basically garbage. – Cullub Feb 08 '17 at 18:51
  • 2
    @ant do you need the `(void *)`, though? – Cullub Feb 08 '17 at 18:52
  • In the first case, the compiler should have warned you the code is incorrect (missing argument). In the second case you are not printing *the pointer address of `i`*. You are printing `i` is if it were a pointer, but it isn't and again the compiler should have warned you. Why are you asking what the compiler has already told you? Your question is equivalent to asking how much milk the milkman will deliver, when you have not told him. – Weather Vane Feb 08 '17 at 18:55
  • 2
    @cullub: Pedantically, yes. A `void *` pointer is expected by `printf`. A `[signed/unsigned] char *` pointer can be used without a cast, since it is guaranteed to have the same representation as `void *`. But `int *` can have a different representation, which is why it has to be cast to `void *`. – AnT stands with Russia Feb 08 '17 at 18:58
  • 1
    @cullub [Yes](http://stackoverflow.com/questions/24867814/printfp-and-casting-to-void). Pointers may have different object representations. – Ilja Everilä Feb 08 '17 at 19:00
  • So, to sum this up it is the data stored previously in the register? Trash data from another process. – Rattata 2me Feb 09 '17 at 22:32

3 Answers3

4

The behavior of

printf("%p");

is undefined. When you specify a %p format in the format string, the corresponding argument of void * (or char *) type shall be present in the argument list.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • 1
    I think OP is asking for how that rule is enforced, if at all. – Mad Physicist Feb 08 '17 at 18:55
  • @Mad Physicist: I don't see it. The question appears to be about the printed garbage and where it comes from. – AnT stands with Russia Feb 08 '17 at 19:16
  • That makes sense, but your answer does not really answer where the garbage comes from. What does undefined mean in this context? OP is getting a concrete result after all. – Mad Physicist Feb 08 '17 at 19:19
  • 1
    @Mad Physicist: A practical experiment always produces a result that is *concrete* in some sense. I don't quote the standard definition of "undefined behavior" since everybody is probably sick and tired of hearing it in the umpteenth time. – AnT stands with Russia Feb 08 '17 at 19:26
  • @MadPhysicist see [what is undefined behaviour](http://stackoverflow.com/q/2397984/1505939). I'm thinking of getting a macro keyboard for the amount of times I link that link – M.M Feb 08 '17 at 20:59
4

Trash. printf uses a variable-length argument list. It uses the format string to determine how many arguments you actually passed. If you did not actually pass anything in, it will still read from basically arbitrary portions of memory as though you did. The result is undefined/trash.

Some compilers will be able to catch this situation with a warning because the printf family of functions is so popular. Some cases may crash your system if the function tries to read from memory you do not have access to. There is no way to tell how it will behave next time even if you have obtained a certain result.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
4

But my question is what does it return when not adding any other argument in the printf function just printf("%p");

Anything. Nothing. Random junk. Maybe it crashes.

There is no way to know without investigating a specific combination of compiler, CPU, platform, libraries, execution environment, and so on. There is no rule that requires it to operate any particular way.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278