c语言中使用十进制、二进制、八进制和十六进制输出0到65535的整数。
1、
#include <stdio.h> int count_bits(unsigned x) { int bits = 0; while(x) { if(x & 1U) bits++; x >>= 1; } return bits; } int int_bits(void) { return count_bits(~0U); } int print_nbits(unsigned x, unsigned n) { int i = int_bits(); i = (n < i) ? n - 1 : i - 1; for(; i >= 0; i--) { putchar((x >> i & 1U) ? '1' : '0'); } } int main(void) { unsigned i; for(i = 0; i <= 65535U; i++) { printf("%5u ", i); print_nbits(i, 16); printf(" %06o %04X ",i, i); } return 0; }