14

This C program gives a weird result:

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

int main(int argc, char *argv[])
{
   char str1[5] = "abcde";
   char str2[5] = " haha";

   printf("%s\n", str1);
   return 0;
}

when I run this code I get:

abcde haha

I only want to print the first string as can be seen from the code.
Why does it print both of them?

Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
Lundu Harianja
  • 443
  • 1
  • 9
  • 17
  • 13
    Because you limited the length of `str1` to 5, no string terminator was stored as part of the array. Therefore it overflowed on printing into the next string. This is Undefined Behaviour, and may not always happen like this. – Weather Vane Jun 23 '15 at 18:43
  • 5
    See [No compiler error when fixed size char array is initialized without enough room for null terminator](http://stackoverflow.com/q/20694796/1708801) – Shafik Yaghmour Jun 23 '15 at 18:46

6 Answers6

36

"abcde" is actually 6 bytes long because of the null terminating character in C strings. When you do this:

char str1[5] = "abcde";

You aren't storing the null terminating character so it is not a proper string.

When you do this:

char str1[5] = "abcde";
char str2[5] = " haha";
printf("%s\n", str1);

It just happens to be that the second string is stored right after the first, although this is not required. By calling printf on a string that isn't null terminated you have already caused undefined behavior.

Update:

As stated in the comments by clcto this can be avoided by not explicitly specifying the size of the array and letting the compiler determine it based off of the string:

char str1[] = "abcde";

or use a pointer instead if that works for your use case, although they are not the same:

const char *str1 = "abcde";
missimer
  • 4,022
  • 1
  • 19
  • 33
20

Both strings str1 and str2 are not null terminated. Therefore the statement

 printf("%s\n", str1);  

will invoke undefined behavior.
printf prints the characters in a string one by one until it encounters a '\0' which is not present in your string. In this case printf continues past the end of the string until it finds a null character somewhere in the memory. In your case it seems that printf past the end of string "abcde" and continues to print the characters from second string " haha" which is by chance located just after first string in the memory.

Better to change the block

   char str1[5] = "abcde";
   char str2[5] = " haha";  

to

 char str1[] = "abcde";
 char str2[] = " haha";  

to avoid this problem.

haccks
  • 104,019
  • 25
  • 176
  • 264
11

Technically, this behavior is not unexpected, it is undefined: your code is passing a pointer to a C string that lacks null terminator to printf, which is undefined behavior.

In your case, though, it happens that the compiler places two strings back-to-back in memory, so printf runs into null terminator after printing str2, which explains the result that you get.

If you would like to print only the first string, add space for null terminator, like this:

char str1[6] = "abcde";

Better yet, let the compiler compute the correct size for you:

char str1[] = "abcde";
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
6

You have invoked undefined behaviour. Here:

   char str1[5] = "abcde";

str1 has space for the above five letters but no null terminator. Then the way you try to pass not null terminated string to printf for printing, invokes undefined behaviour.

In general in most of the cases it is not good idea to pass not null terminated strings to standard functions which expect (C) strings.

Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
4

In C such declarations

char str1[5] = "abcde";

are allowed. In fact there are 6 initializers because the string literal includes the terminating zero. However in the left side there is declared a character array that has only 5 elements. So it does not include the terminating zero.

It looks like

char str1[5] = { 'a', 'b', 'c', 'd', 'e', '\0' };

If you would compile this declaration in C++ then the compiler issues an error.

It would be better to declare the array without specifying explicitly its size.

char str1[] = "abcde";

In this case it would have the size equal to the number of characters in the string literal including the terminating zero that is equal to 6. And you can write

printf("%s\n", str1);

Otherwise the function continues to print characters beyond the array until it meets the zero character.

Nevertheless it is not an error. Simply you should correctly specify the format specifier in the call of printf:

printf("%5.5s\n", str1);

and you will get the expected result.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
-1

The %s specifier searches for a null termination. Therefore you need to add '\0' to the end of your string

char str1[6] = "abcde\0";
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
Manolete
  • 3,431
  • 7
  • 54
  • 92
  • 3
    This is incorrect. The compiler will add the terminating `nul` providing there is room for it. If you declare `char str1[] = "abcde\0";` then `sizeof str1` will be 7, not 6, since it will contain **two** `nul`s. – Weather Vane Jun 23 '15 at 19:10
  • @WeatherVane Well, it is not incorrect, at all!. If you "want" to give the size of the array in the declaration then you *have* to manually add the null termination. Now if you leave the compiler to decided the size of the string for you, then the compiler will add the null termination for you. Therefore you either do `char str1[] = "abcde";` or `char str1[6] = "abcde\0";`. In both cases `sizeof(str1) = 6` – Manolete Jun 24 '15 at 08:50
  • 1
    No, you do *not* need to add `'\0'` to the end of your string. `char str1[6] = "abcde";` is enough. If there is room for it, the compiler will add it. If not, you can't add it manually. – Weather Vane Jun 24 '15 at 10:43