'Passing not null terminated string to printf results in unexpected value
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?
Solution 1:[1]
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.
Solution 2:[2]
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";
Solution 3:[3]
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.
Solution 4:[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.
Solution 5:[5]
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";
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Sergey Kalinichenko |
| Solution 3 | |
| Solution 4 | Sourav Ghosh |
| Solution 5 | Weather Vane |
