Having difficulty printing strings

后端 未结 5 519
走了就别回头了
走了就别回头了 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:20

    You need to terminate your array with NULL character as

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

    When you are doing the scanf(), string1 is stored in next memory so it is printing string2 with string1. It will print upto it gets \0 so its Undefined Behavior

提交回复
热议问题