15

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?

2

6 Answers 6

37

"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";
Sign up to request clarification or add additional context in comments.

4 Comments

by ... caused undefined before... did you mean ...caused undefined behavior?
Yes I meant behavior, fingers thinking faster than the brain, Sourav Ghosh's edit is correct and appreciated.
Suggest using chr str1[] = "abcde"; to prevent this?
Or use the more usual char *str1 = "abcde";
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.

2 Comments

..or suffers from segfault of inaccessible memory.
It can even be seen by doing a printf("%d", sizeof(str1)); of the second block of strings... 6
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";

2 Comments

str2 also was not given space for a null character, so is it just a coincidence that nothing more is printed after str2? It could continue reading memory even further?
It was unexpected by at least one person :)
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.

1 Comment

Yeah, looks better, +1.
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.

3 Comments

Would be so much nicer this answer with just one space added in front of the first 3 code blocks! Such a shame I have to find 6 characters to add to be able to edit it :(
@PeterSW OK, this one's for you. Happy? ;-)
@SouravGhosh Much better :)
-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";

3 Comments

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 nuls.
@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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.