Why do strings get printed twice when using printf in C? -


my program asks user provide string, copied array of characters. then, for loop, program copies elements of first array second array.

int main() {      int i;     char string1[4], string2[4];      // first string     printf("insert string: ");     scanf("%s", string1);      // copy values second array     (i = 0; < 4; i++) {         string2[i] = string1[i];     }      // print second string     printf("%s", string2);     return 0; } 

however, when print string using printf() function string gets printed twice.

let's input word

bars

the output

barsbars

why happening?

char string1[4], string2[4]; 

4-element char array not enough 4-character strings. need 1 more terminating '\0' character.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -