This question is about C. Say I have a string 'test'. By looking at the chars we have { 't', 'e', 's', 't', '\0' }
Say I wanted to add a ': ' after it so that it becomes
{ 't', 'e', 's', 't', ':', ' ' }
I use the following code to implement this.
Code:
char str[16] = "test";
int len;
len = strlen(str);
len = len +2;
str[len - 2] = ':';
str[len - 1] = ' ';
str[len] = '\0';
So that should do it because the string is null-terminated. Yet when I run a
Nothing gets printed. However, if I do the following instead
Code:
printf("%s\n", str);
It prints just fine. Why is that? I could always just do
Code:
write(STDOUT_FILENO, str, sizeof(str));
which works fine but I'd like to understand why printf without the newline prints nothing.