输出乱码问题,最后输出了我不想要的字符

img

c 里面的 string 是用 \0 做结尾
你定义了 char 20 个,可接收最多 19 char 留一个给 \0
但如只输入 test 4 个 char 实际只用了 char 20 里的 5 个
而 6-20 就是内存里的不明值
因为你没有任何额外处
而你又固定把 char 20 都打印出来
所以有一堆6-20不明值也打印出来


reakpoint 2, main () at ten.c:20
20        for(i=0; i<5; i++) {
(gdb) p s
$2 = {"test001\ntest002\ntest", "003\ntest004\ntest005\n", "test003\000\246\343\377\377\377\177\000\000\215TUU", "test004\000\377\177\000\000@TUUUU\000", "test005\000`QUUUU\000\000\260\344\377\377"}
(gdb) 

输出那里修改为:

for(i=0;i<5;i++)
    {
        for(j=0;s[i][j]!='\0';j++) //j<20
        {
            s[i][j]=toupper(s[i][j]);
            putchar(s[i][j]);
        }
    }