Having difficulty printing strings

后端 未结 5 516
走了就别回头了
走了就别回头了 2021-01-22 06:35

When I run the program, the second printf() prints string2 with whatever was scanned into string1 attached to the end.

e.g.

5条回答
  •  无人及你
    2021-01-22 07:18

    A string is a null terminated char array in C.

    Change

    char string2[4]={'1','2','a','b'};
    

    to

    char string2[5]={'1','2','a','b', '\0'};
    

    (which is the same as char string2[] = "12ab";)

提交回复
热议问题