1 #include2 void binary(int d){ 3 int i=0,j,n,b[8]={ 0}; 4 while(d>0){ 5 n=d%2; 6 d=d/2; 7 b[i++]=n; //不停的除2,余数保存在b[8] 数组中 8 } 9 for(j=7;j>=0;j--)10 printf("%d",b[j]); //将余数倒序输出,即为所求。11 printf(" ");12 }13 14 void main()15 {16 int d1,d2,d3,d4;17 d1=d2=d3=d4=0;18 printf("input decimal ip address:\n");19 scanf("%d.%d.%d.%d",&d1,&d2,&d3,&d4);20 printf("%3d.%3d.%3d.%3d\n",d1,d2,d3,d4);21 printf("binary ip address:\n");22 binary(d1);23 binary(d2);24 binary(d3);25 binary(d4);26 }