1 Pass address of a char
array in scanf()
and not the address of a char*
.
2 Insure you do not overwrite your destination buffer.
3 Right-size your buffer needs. It is apparent from other posts you want a binary textual representation of an int
. Let's assume your int
is 8 bytes (64 bits).
#include <stdio.h>
#include <stdlib.h>
int main(){
char bitstr[8*8 + 1]; // size to a bit representation of a big integer.
printf("Enter a bitstring or q for quit: ");
//Change format and pass bitscr, this results in the address of bitscr array.
scanf("%64s", bitstr);
return 0;
}
I prefer the fgets() & sscanf() method.
char buf[100]; // You can re-use this buffer for other inputs.
if (fgets(buf, sizeof(buf), stdin) == NULL) { ; /*handle error or EOF */ }
sscanf(buf, "%64s", bitstr);