printf outputting garbage instead of specific characters

后端 未结 2 1898
清酒与你
清酒与你 2020-12-12 07:47

Well I\'ve weird problem with printf(). It\'s outputting garbage on screen. It\'s kind of connected with memory I guess. Have a look:

char strin         


        
相关标签:
2条回答
  • 2020-12-12 08:32

    finish your string2 with null character '\0'

    string2[19] = '\0';
    

    Or you can do it in this way:

    for (i; i < 19; i++) string2[i] = ' ';
    string2[i] = '\0'; // after the end of the loop i= 19 here
    
    0 讨论(0)
  • 2020-12-12 08:34

    Because C strings need to be NUL terminated. This means the last char of your string must be '\0'. This is how printf (and all other C string functions) know when a string is finished.

    0 讨论(0)
提交回复
热议问题