the Problem actually lies with the character array, you defined.
char strings[5][4]
the program allocates "strings" 20 "char" sized memory blocks.
lets suppose starting address is 0x00
so memory block from 0x00 to 0x20 (Mentioned in decimal system for easy understanding) is allocated to "strings"
this means strings[0] points to 0x00
this means strings[1] points to 0x04
this means strings[2] points to 0x08
this means strings[3] points to 0x12
this means strings[4] points to 0x16
now in the "scanf" is basically reading each character until endline and storing it in corresponding memory blocks
for (int i = 0; i < 5; ++i) {
scanf("%s", strings[i]);
}
so from your question, "this" is stored in 4 blocks of strings[0] and there is no space for endline character
"is" occupies 3 characters from strings[1] with third being endline character.
Now coming to printf function for arrays.it considers array as pointer and iterates until it finds endline statement.
for (int i = 0; i < 5; ++i) {
printf("%s\n", strings[i]);
}
For strings[0] , printf function takes pointer 0x00 and iteartes until it finds endline character(ie at 0x06)
so "thisis" is printed
and for string[1] , pointer is at 0x04 and so "is" is printed.
now if you give your input string of sizw more than 4 you can observe that the string is overflowed to next string
for (int i = 0; i < 1; ++i) {
scanf("%s", strings[i]);
}
for (int i = 0; i < 2; ++i) {
printf("%s\n", strings[i]);
}
**INPUT:**
thisab
in the above code, only strings[0] is assigned with value "thisab"
but it printf produces
**OUTPUT:**
thisab
ab
to avoid such problem , use Strings or define size of each row equal to (max_input_size+1)