-5

I'm learning C programming now (I'm new to it), and I'm trying various things as well to understand how things work. So, I'm well aware that the following code isn't proper code; but I still tried it out to understand how it would be compiled:

#include <stdio.h>
#include <stdlib.h>

int {
    printf("I think C will give me %d %d %d %d %d.", 20);
    return 0;
}

And I'm getting this in the command line:

I think C will give me 20 8 108 108 2.
Process returned 0 (0x0)   execution time : 0.234 s
Press any key to continue.

Where from the numbers 8, 108, 108 and 2? How were they generated?

I'm just trying to understand how it works. Thanks.

Isaac Asante
  • 435
  • 1
  • 5
  • 21
  • 1
    This is undefined behaviour, that's why. – cs95 Aug 27 '17 at 16:16
  • 1
    You only supplied an argument for the first of the five `%d` format specifiers, so the behaviour is undefined. – Weather Vane Aug 27 '17 at 16:17
  • 3
    The behavior is undefined. No one knows how these numbers are "generated" since in practice it will depend on many implementation-specific circumstances. In other words, it is possible to come up with a "real-life" answer, but it is highly dependent on your setup. We don't see it from here. – AnT stands with Russia Aug 27 '17 at 16:17
  • 2
    Don’t worry about the downvotes; this is a pretty good question. The relevant bit in the duplicate’s answer is: *“The printf function will assume that you have supplied the argument and go looking for it on the stack. It will pick up whatever happens to be on there.”* – Ry- Aug 27 '17 at 16:20
  • 1
    Trying to understand where these exact numbers come from in your particular environment will actually be a valuable, non-trivial exercise. `printf` is a funny function: It can have any number of parameters > 0. How would *you* write such a function? Hint: [va_arg](https://linux.die.net/man/3/va_arg). What happens? Where are the arguments stored? How accessed? How is the stack managed? What does the stack look like when printf is called? After it is called? After it has ended? Why? Also note that it is undefined bahavior (only) from a language standpoint; any particular system is deterministic. – Peter - Reinstate Monica Aug 27 '17 at 16:36

1 Answers1

1

Because it's undefined behavior.

There is no random involved, it's just that printf() is trying to find the correspoing argument for each "%d" and it accesses memory that does not point to any argument, so it prints whatever it finds there.

But strictly, it's undefined behavior. The standard does not specify a behavior in case there are more format specifiers than arguments.

So passing a single int variable, when the function is expecting 5 will cause undefined behavior and in practice it's impossible to predict what is going to happen.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • 2
    That’s what [FGITW](https://meta.stackexchange.com/questions/9731/fastest-gun-in-the-west-problem) is. Don’t do it. Also, I’m sure you know this is a duplicate of many things given that you’re answering with the standard UB response, so vote to close instead. – Ry- Aug 27 '17 at 16:21
  • 1
    @Ryan Thank you for the link, I should have thought of it from the beginning, I will never do it again. – Iharob Al Asimi Aug 27 '17 at 16:25
  • 1
    @Iharob [Edit: Was, in a deleted comment] right saying that this answer is essentially correct. In particular it is important to assert that no randomness is involved. Computers are (ideally) determistic machines. If you know the preconditions you can absolutely predict the numbers printed by the program. That is why you need to manually generate randomness by, well, randomly pounding the keyboard for some applications like cryptographic key generation. Using printf without arguments is emphatically *not* a way to produce random numbers. – Peter - Reinstate Monica Aug 27 '17 at 16:27