I have declared a character array of size 4. When I take a string input even of size 5, the program prints the correct string. Why does this happen? With 8 characters too, it prints the right string but with 9 characters it gives an error.
#include <iostream>
using namespace std;
int main()
{
char s[4];
scanf("%s",s);
printf("%s",s);
}
Error when I try to take input as a 9 character long string:
abcdefghi
*** stack smashing detected ***: /home/shivam/Study/standardproblems/stacks/temp terminated
abcdefghiAborted (core dumped)