gpt4 book ai didi

c - C 代码中的运行时错误(奇怪的双重转换)

转载 作者:行者123 更新时间:2023-11-30 19:48:16 26 4
gpt4 key购买 nike

我的 C 代码中有一个奇怪的运行时错误。这里的Integers比较效果很好。但在小数比较中,我总是得到第二个数字大于第一个数字,这是错误的。一般来说,我对 C 和编程还很陌生,所以这对我来说是一个复杂的应用程序。

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int choose;
long long neLimit = -1000000000;
long long limit = 1000000000;
bool big(a,b) {
if ((a >= limit) || (b >= limit))
return true;
else if ((a <= neLimit) || (b <= neLimit))
return true;
return false;
}
void largerr(a,b) {
if (a > b)
printf("\nThe First Number is larger ..\n");
else if (a < b)
printf("\nThe Second Number is larger ..\n");
else
printf("\nThe Two Numbers are Equal .. \n");
}
int main() {
system("color e && title Numbers Comparison && echo off && cls");
start:{
printf("Choose a Type of Comparison :\n\t1. Integers\n\t2. Decimals \n\t\t I Choose Number : ");
scanf("%i", &choose);
switch(choose) {
case 1:
goto Integers;
break;
case 2:
goto Decimals;
break;
default:
system("echo Please Choose a Valid Option && pause>nul && cls");
goto start;
}
}
Integers: {
system("title Integers Comparison && cls");
long x , y;
printf("\nFirst Number : \t");
scanf("%li", &x);
printf("\nSecond Number : ");
scanf("%li", &y);
if (big(x,y)) {
printf("\nOut of Limit .. Too Big Numbers ..\n");
system("pause>nul && cls") ; goto Integers;
}
largerr(x,y);
printf("\nFirst Number : %li\nSecond Number : %li\n",x,y);
goto exif;
}
Decimals: {
system("title Decimals Comparison && cls");
double x , y;
printf("\nFirst Number : \t");
scanf("%le", &x);
printf("\nSecond Number : ");
scanf("%le", &y);
if (big(x,y)) {
printf("\nOut of Limit .. Too Big Numbers ..\n");
system("pause>nul && cls") ; goto Decimals;
}
largerr(x,y);
goto exif;
}
exif:{
system("pause>nul");
system("cls");
main();
}
}

最佳答案

函数需要参数类型声明。

当 OP 调用 big()largerr() 时,变量 x ydouble

Decimals: {
...
double x , y;
...
big(x,y)
...
largerr(x,y)

声明了这2个函数

bool big(a,b) {
...
void largerr(a,b) {

在这两个函数中,参数 a b 缺少任何类型信息。使用旧式 C 标准,该函数假设 a bint

结果是传递了 2 个 double,并且这些通常是 2*8 字节在函数处接收,并且预计通常是 2*4 字节的 int。因此,传递的数据的类型和大小不匹配,并且随之而来的是各种未定义行为 (UB)。

原型(prototype)设计会有所帮助,如下所示。

bool big(double a, double b)
<小时/>

在 + 方面,OP 的帖子已完成。

<小时/>

其他问题:
转到
调用main()
main() 没有返回
应使用 lager_double(double, double)lager_long(long, long)
过度使用标签。

关于c - C 代码中的运行时错误(奇怪的双重转换),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19747123/

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