Why do I have different output then expected when I input a word that has the same length (or bigger) as default char-array length
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[])
{
char word[4];
printf("Write a string: ");
scanf("%s", word);
int lenght = strlen( word );
int j = lenght-1;
for (size_t i = 0; i < lenght/2; i++)
{
char temp = word[i];
word[i]=word[j];
word[j] = temp;
j--;
}
printf("%s", word); //reversed string
return 0;
}
e.g input is "kill"
expected output is "llik"
actuall output is lliki♦
but when I fill that array using scanf()
with word "kill" and then use printf("%s",word)
I see that there is no problems with having words longer than 4 in word[]