Extra characters added to end of string in c

后端 未结 3 728
一整个雨季
一整个雨季 2020-12-11 21:41

So I am trying to print out a string in C and I am consistently getting extra characters at the end of the string when I print it out. The Code:

char binaryN         


        
相关标签:
3条回答
  • 2020-12-11 22:19

    Let the compiler determine the amount of elements needed

    char binaryNumber[] = "1111000011110000";
    // same as
    // char binaryNumber[17] = "1111000011110000";
    
    0 讨论(0)
  • 2020-12-11 22:24

    It should be

    char binaryNumber[17] = "1111000011110000";
    

    This is because strings in C are null terminated. So you will be reading garbage if you don't give an extra character space for the implicit \0 which will be added

    0 讨论(0)
  • 2020-12-11 22:31

    You have 16 characters in your array. and there is no place to hold a \0 character.

    %s prints a string until it encounters a \0

    So what you are seeing is some garbage characters being printed out.Please make your string is \0 terminated

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