gpt4 book ai didi

c - 即使满足所有循环和条件语句,我的 C 代码也会继续运行并且不会返回任何结果

转载 作者:行者123 更新时间:2023-11-30 20:29:06 27 4
gpt4 key购买 nike

我也尝试过OnlineGBD调试工具和Visual Studio,但它们都没有多大帮助。

如果我输入“1 1 1 1 8”,程序仍然会运行并且不会返回所需的输出。

我研究了一下,网上有人说问题出在变量的三重声明上,但是我更改了每个函数的变量名称,问题仍然存在。

#include <stdio.h>

int IPAndSubnetMaskInput(int IPAndSubnetMaskFirstOctate, int IPAndSubnetMaskSecondOctate, int IPAndSubnetMaskThirdOctate, int IPAndSubnetMaskFourthOctate, int IPAndSubnetMaskSubnetInput) {

printf("Please input an IPv4 address followed by the subnetmask (must either be 8, 16, 24, or 32) in the following format: 192 168 1 1 32: \n"); //Statement to ask for IP and submask input and puts the inputs into a variable to calculate Broadcast IP
int IPv4AndSubmask = scanf("%d %d %d %d %d", &IPAndSubnetMaskFirstOctate, &IPAndSubnetMaskSecondOctate, &IPAndSubnetMaskThirdOctate, &IPAndSubnetMaskFourthOctate, &IPAndSubnetMaskSubnetInput); //Input function

while (!(IPAndSubnetMaskSubnetInput == 8 || IPAndSubnetMaskSubnetInput == 16 || IPAndSubnetMaskSubnetInput == 24 || IPAndSubnetMaskSubnetInput == 32)) { //Initializing loop to evaluate whether subnet is correct or not

printf("Your submask is wrong. Please enter a value that's either 8, 16, 24, or 32: \n");
scanf("%d", &IPAndSubnetMaskSubnetInput);

if (IPAndSubnetMaskSubnetInput == 8 || IPAndSubnetMaskSubnetInput == 16 || IPAndSubnetMaskSubnetInput == 24 || IPAndSubnetMaskSubnetInput == 32)
{ break; }
}
return IPv4AndSubmask;//function returns the value of IP octates and the subnet mask for the program to calculate
}


int broadcastCalculator(int broadcastFirstOctate, int broadcastSecondOctate, int broadcastThirdOctate, int broadcastFourthOctate, int broadcastSubnetInput) { //Declaration of first function for first Assignment point

IPAndSubnetMaskInput(broadcastFirstOctate, broadcastSecondOctate, broadcastThirdOctate, broadcastFourthOctate, broadcastSubnetInput);

while (0 == 0) {
if (broadcastSubnetInput == 8) { //Conditional statement for submask of 8
printf("The broadcast IP is:\t%hhu\t%hhu\t%hhu\t255\t\n", broadcastFirstOctate, broadcastSecondOctate, broadcastThirdOctate);//Program will print the Broadcast IP of firstOctate secondOctate thirdOctate 255
break;
}
else if (broadcastSubnetInput == 16) {//Conditional statement for submask of 16
printf("The broadcast IP is:\t%hhu\t%hhu\t255\t255\t\n", broadcastFirstOctate, broadcastSecondOctate);//Program will print the Broadcast IP of firstOctate secondOctate 255 255
break;
}
else if (broadcastSubnetInput == 24) {//Conditional statement for submask of 24
printf("The broadcast IP is:\t%hhu\t255\t255\t255\t\n", broadcastFirstOctate);//Program will print the Broadcast IP of firstOctate 255 255 255
break;
}
else if (broadcastSubnetInput == 32) {//Conditional statement for submask of 32
printf("The broadcast IP is:\t255\t255\t255\t255");//Program will print the Broadcast IP of 255 255 255 255
break;
}

}
return 0;
}


int main()
{
int firstOctate, secondOctate, thirdOctate, fourthOctate, subnetInput;
broadcastCalculator(firstOctate, secondOctate, thirdOctate, fourthOctate, subnetInput);

return 0;
}

最佳答案

编译发布的代码时:

gcc    -ggdb -Wall -Wextra -Wconversion -pedantic -std=gnu11  -c "untitled2.c"  
untitled2.c: In function ‘main’:
untitled2.c:50:5: warning: ‘firstOctate’ is used uninitialized in this function [-Wuninitialized]
broadcastCalculator(firstOctate, secondOctate, thirdOctate, fourthOctate, subnetInput);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
untitled2.c:50:5: warning: ‘secondOctate’ is used uninitialized in this function [-Wuninitialized]
untitled2.c:50:5: warning: ‘thirdOctate’ is used uninitialized in this function [-Wuninitialized]
untitled2.c:50:5: warning: ‘fourthOctate’ is used uninitialized in this function [-Wuninitialized]
untitled2.c:50:5: warning: ‘subnetInput’ is used uninitialized in this function [-Wuninitialized]
Compilation finished successfully.

该语句:“编译成功完成。”仅意味着编译器为解决问题而生成了一些“解决方法”的代码。这并不意味着生成的代码是正确的。

每一条“使用未初始化”消息都在告诉您一些未定义的行为。

所以无论代码输出什么都是没有意义的。

建议修复代码,以便它可以干净地编译

关于c - 即使满足所有循环和条件语句,我的 C 代码也会继续运行并且不会返回任何结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59008049/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com