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
Let the compiler determine the amount of elements needed
char binaryNumber[] = "1111000011110000";
// same as
// char binaryNumber[17] = "1111000011110000";
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
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