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?
If you want to print a non-null-terminated string with interior null, you cannot use a single printf. Use fwrite instead: fwrite(string, 1, length, stdout);
C strings are null-terminated by convention. That means that printf , or any other function receiving a C string, will read the pointed memory till it encounters a null character. In your case there's no null terminator in str , so printf will keep reading from whatever memory comes next.
Problem with strncpy(): If there is no null character among the first n character of src, the string placed in dest will not be null-terminated. So strncpy() does not guarantee that the destination string will be NULL terminated.
It always null-terminate. The strncat function appends not more than n characters (a null character and characters that follow it are not appended) from the array pointed to by s2 to the end of the string pointed to by s1 . The initial character of s2 overwrites the null character at the end of s1 .
If your non-null-terminated array-of-char contains null bytes BEFORE the length, printf will stop on those, and not continue. Show activity on this post.
printf ("%.*s", length, string) will NOT work. This means to print UP TO length bytes OR a null byte, whichever comes first. If your non-null-terminated array-of-char contains null bytes BEFORE the length, printf will stop on those, and not continue.
This means to print UP TO length bytes OR a null byte, whichever comes first. If your non-null-terminated array-of-char contains null bytes BEFORE the length, printf will stop on those, and not continue. Show activity on this post.
You can use an fwrite () to stdout! This way you will output the first chars (number defined in number_of_chars variable ) to a file, in this case to stdout (the standard output, your screen)! Show activity on this post. printf ("%.*s", length, string) will NOT work. This means to print UP TO length bytes OR a null byte, whichever comes first.
"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";
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With