用c语言完成将点分十进制的IPv4地址以二进制格式输出
时间: 2023-05-26 07:05:54 浏览: 107
IP地址转换(c语言)
```c
#include <stdio.h>
#include <stdlib.h>
int main() {
char ip[16];
printf("Please enter an IP address: ");
scanf("%s", ip);
// Convert IP address from string to integers
int a, b, c, d;
sscanf(ip, "%d.%d.%d.%d", &a, &b, &c, &d);
// Convert each number to binary and output
printf("%d.%d.%d.%d in binary is ", a, b, c, d);
int i;
for(i = 7; i >= 0; i--) {
printf("%d", (a >> i) & 1);
}
printf(".");
for(i = 7; i >= 0; i--) {
printf("%d", (b >> i) & 1);
}
printf(".");
for(i = 7; i >= 0; i--) {
printf("%d", (c >> i) & 1);
}
printf(".");
for(i = 7; i >= 0; i--) {
printf("%d", (d >> i) & 1);
}
printf("\n");
return 0;
}
```
阅读全文