1

I was just wondering if you could clear something up for me.

Let's have some example code to explain my question:

#include <stdio.h>

int main(void)
{
    char test[100];
    printf("%s",test);
    return 0;
}

If I am not totally mistaken, this should output randomly either some character that was at this memory address before I declared it or nothing if it was empty like in a virtual environment. So, this is my understanding. The memory held before I put something in is understood as a char and written to the terminal. For instance ascii 'a' = 97 = 01100001. That's why it outputs 'a'. Could have been anything else. Or nothing. And then it stops.

But if I put 'a' in the first position and then print it like this:

test[0] = 'a'
printf("%s",test);

It will output 'a' and additionally to that some character or nothing and then stop.

This is how I understand arrays: An array is a pointer to the first address and the brackets are a dereferences of the address after adding the number times sizeof(type) to it.

So, in that case, the random 01100001 (Ascii 'a') found in the memory in the first example should be indistinguishable for printf from the deliberately placed 01100001 (Ascii 'a') in the second example. Yet, when I run printf, I don't get 100 random outputs. I get one. And I don't assume random fields are in general set to '\0'.

Which means, my understanding must be wrong somewhere. Please help me understand where I make my mistake.

TheCommoner282
  • 205
  • 1
  • 7
  • 5
    Looks like "undefined behavior" would be a good name for your findings. – Jongware Nov 20 '16 at 10:27
  • 2
    Possible duplicate of [When does printf("%s", char\*) stop printing?](http://stackoverflow.com/questions/2726301/when-does-printfs-char-stop-printing) – phuclv Nov 20 '16 at 10:29
  • "How does c's printf function know how to stop without a \0?" --> `test[100]` is not initialized. So it could have a `\0` in it. We do not know it is "without a \0". – chux - Reinstate Monica Nov 20 '16 at 19:20

1 Answers1

6

It doesn't, it's undefined behavior. Your program just accidentally prints the un"expected" value.

#include <stdio.h>

int main(void)
{
    char test[100];
    printf("%s",test);
    return 0;
}

You can't expect the code above to do anything predictable, it might print something, it could segfault, there is no way to predict what will actually happen because the behavior of such program is strictly undefined.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • So, you're saying it could happend that it does print out 100 different undefined values? – TheCommoner282 Nov 20 '16 at 10:29
  • 1
    @TheCommoner282 Yes, that would be a behavior. Since it's undefined, in principle it could erase your HD. But that of course is very highly unlikely and perhaps, impossible. – Iharob Al Asimi Nov 20 '16 at 10:31
  • Okay, just for clarification. You're saying undefined behaviour. I understand, it prints out a memory section consisting of 8 bits, that it reads as chars. So, it can only understand those as chars, can't it? Now it does it as long until it encounters a "\0" in the end, right? – TheCommoner282 Nov 20 '16 at 10:36
  • 1
    @TheCommoner282 Yes, that's how it works. Although saying it's 8bits is not strictly true, but that doesn't matter for the assertion. It will read bytes from the input parameter until it finds the `'\0'` which is required for a string to be a string in the c language. Also, it understands nothing! It just calls the appropriate system functions to print the value, whether it's valid or not doesn't matter to `printf()`, if there is a `'\0'` or not doesn't matter either, but if there isn't it's not possible to know what will happen. – Iharob Al Asimi Nov 20 '16 at 10:38
  • so, it was nothing but my lack in experience as a beginner with c and the fact that I used a virtual machine (cloud9 online IDE) that it stopped always one letter later? Hmm.. that is good to know... I already started to simply print something without a \0 out and then just disregard the last char for debugging... Thanks for your explanation. – TheCommoner282 Nov 20 '16 at 10:42
  • 1
    @TheCommoner282 You are trying to find an explanation to undefined behavior, just DON'T!!! It has no explanation, it's merely an accident, nothing more. I am glad you now have a clearer understanding. – Iharob Al Asimi Nov 20 '16 at 10:44
  • 2
    @TheCommoner282 One of the harder things to "get" about undefined behavior is that it is not necessarily random. Sometimes it is -- sometimes it's downright nondeterministic -- but sometimes it does the same thing time after time, leading you to think that it must mean something. But it doesn't. – Steve Summit Nov 20 '16 at 14:14