Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extra characters added to end of string in c

Tags:

c

string

printf

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 binaryNumber[16] = "1111000011110000";
printf("binary integer: %s\n", binaryNumber);

Output:

binary integer: 1111000011110000▒▒▒▒

can you please help me figure out why this is happening. I think this is the root of some other problems in my code. I had this problem before when I was creating the string in a more complex way, and got extra characters in that case too, but they were different. So I made a string the most basic way possible (method shown here) and am still getting the problem

like image 658
bs7280 Avatar asked Dec 09 '22 05:12

bs7280


1 Answers

Let the compiler determine the amount of elements needed

char binaryNumber[] = "1111000011110000";
// same as
// char binaryNumber[17] = "1111000011110000";
like image 117
pmg Avatar answered Dec 16 '22 15:12

pmg