C 由于某种原因,命令被卡住了
我试图从用户那里输入基本的ip地址,但是我的命令在scanf中被卡住了,之后什么也没有执行C 由于某种原因,命令被卡住了,c,ip,bits,C,Ip,Bits,我试图从用户那里输入基本的ip地址,但是我的命令在scanf中被卡住了,之后什么也没有执行 int ip1,ip2,ip3,ip4; scanf("%d.%d.%d.%d",&ip1,&ip2,&ip3,&ip4); printf("Here"); 所以,基本上这里永远不会被打印,scanf命令永远不会被执行 #include <stdio.h> #include<math.h> int main(void) { cha
int ip1,ip2,ip3,ip4;
scanf("%d.%d.%d.%d",&ip1,&ip2,&ip3,&ip4);
printf("Here");
所以,基本上这里永远不会被打印,scanf命令永远不会被执行
#include <stdio.h>
#include<math.h>
int main(void) {
char input;
char rep = 'r';
char quit = 'q';
char first = '1';
char second = '2';
input = rep;
while( input != quit) {
printf("What type of conversion do you want? \n");
printf("Enter 1 for 32-bit number to dot-decimal conversion, 2 for the inverse of operation: ");
char val;
scanf(" %c", &val);
if( val == first) {
} else if( val == second) {
printf("\nEnter dot-decimal IP address:");
int ip1,ip2,ip3,ip4;
scanf(" %d.%d.%d.%d", &ip1,&ip2,&ip3,&ip4);
printf("Here");
unsigned int ip = 0,c,k,counter = 31;
for(c = 7; c >= 0; c--) {
k = ip1 >> c;
if(k & 1) {
int temp = 2,i;
for(i = 0; i < counter;i++) {
temp *= 2;
}
ip += temp;
counter--;
}
}
for(c = 7; c >= 0; c--) {
k = ip2 >> c;
if(k & 1) {
int temp = 2,i;
for(i = 0; i < counter;i++) {
temp *= 2;
}
ip += temp;
counter--;
}
}
for(c = 7; c >= 0; c--) {
k = ip3 >> c;
if(k & 1) {
int temp = 2,i;
for(i = 0; i < counter;i++) {
temp *= 2;
}
ip += temp;
counter--;
}
}
for(c = 7; c >= 0; c--) {
k = ip4 >> c;
if(k & 1) {
int temp = 2,i;
for(i = 0; i < counter;i++) {
temp *= 2;
}
ip += temp;
counter--;
}
}
printf("%u is the IP Address",ip);
}
printf("\n \n Enter r to repeat, q to quit:");
scanf(" %c",&input);
}
return 0;
}
这正是我所做的。当我尝试以十进制表示法获取IP地址时,它会卡住。我在更新完整代码后分析了您的代码,发现问题不在scanf的输入中,而是在获取数据后执行的for循环中 看看这个循环:
unsigned int ip = 0,c,k,counter = 31;
for(c = 7; c >= 0; c--) {
k = ip1 >> c;
if(k & 1) {
int temp = 2,i;
for(i = 0; i < counter;i++) {
temp *= 2;
}
ip += temp;
counter--;
}
}
尤其是在forc=7时;c>=0;考虑到c的类型是unsigned int。。。我看到这个循环是无限的,因为减量从0开始产生新的正值UINT_MAX参见limits.h.你是如何传递输入的?192.162.2.3只有一个案例无法重现@dave:你在输入后输入了回车键了吗?这对我来说很好。数字之间没有空格,对吗?@dave-这不是gcc的问题。我提供的链接使用gcc编译代码。问题在于你没有表现出来的东西。