bin.c (628B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <inttypes.h> 5 6 #define BUFFER_SIZE 64 7 8 static void 9 printbits(uintmax_t n) 10 { 11 int prec = 0; 12 for (size_t i = 8 * sizeof(uintmax_t); i-- != 0;) { 13 char c; 14 if ((n & (1UL << i)) != 0) { 15 prec = 1; 16 printf("1"); 17 } else if (prec != 0) { 18 printf("0"); 19 } 20 } 21 printf("\n"); 22 } 23 24 int 25 main(int argc, char **argv) 26 { 27 if (argc == 2) { 28 uintmax_t n = atoi(argv[1]); 29 printbits(n); 30 31 return 0; 32 } 33 34 char buffer[BUFFER_SIZE]; 35 while (fgets(buffer, sizeof(buffer), stdin)) { 36 strtok(buffer, "\n"); 37 uintmax_t n = atoi(buffer); 38 printbits(n); 39 } 40 41 return 0; 42 }