How to get rid of seemingly random 0's generated by the strtol function?
I am dealing with this while loop below: while (fgets(binaryArray, MAX_COLUMNS, file)) { int index = 0, index2 = 0; while (binaryArray[index] != '\0'){ if (binaryArray[index] != ' ') { binaryArray[index2++] = binaryArray[index]; } index++; } binaryArray[index2]='\0'; //printf("%s", binaryArray); printf("%ld\n", strtol(binaryArray, NULL, 2)); } =========================================== When the printf("%s", binaryArray); is uncommented (and the printf("%ld\n", strtol(binaryArray, NULL, 2)); is commented out), I get desired binary numbers (without white space between the bits): 10101 10001 00011 10111 However, when the printf("%s", binaryArray); is commented out and the printf("%ld\n", strtol(binaryArray, NULL, 2)); is uncommented, I get the desired decimal values, but with unwanted 0's in between each print: 0 21 0 17 0 3 0 23 0 0 Could you explain why this is happening, and how I could prevent it from happening? ========================================== Entire code: https://code.sololearn.com/cPjtXK5U7u0V input text file has this: 4 5 1 0 1 0 1 1 0 0 0 1 0 0 0 1 1 1 0 1 1 1