相关疑难解决方法(0)

为什么0 <-0x80000000?

我有一个简单的程序:

#include <stdio.h>

#define INT32_MIN        (-0x80000000)

int main(void) 
{
    long long bal = 0;

    if(bal < INT32_MIN )
    {
        printf("Failed!!!");
    }
    else
    {
        printf("Success!!!");
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

条件if(bal < INT32_MIN )总是如此.这怎么可能?

如果我将宏更改为:

#define INT32_MIN        (-2147483648L)
Run Code Online (Sandbox Code Playgroud)

有谁可以指出这个问题?

c signed numeric-conversion numeric-limits

251
推荐指数
6
解决办法
2万
查看次数

为什么最负的int值会导致有关模糊函数重载的错误?

我正在学习C++中的函数重载,并遇到了这个问题:

void display(int a)
{
    cout << "int" << endl;
}

void display(unsigned a)
{
    cout << "unsigned" << endl;
}

int main()
{
    int i = -2147483648;
    cout << i << endl; //will display -2147483648
    display(-2147483648);
}
Run Code Online (Sandbox Code Playgroud)

根据我的理解,该int范围内给出的任何值(在我的情况下int是4个字节)都将调用,display(int)并且该范围之外的任何值都将是不明确的(因为编译器无法决定调用哪个函数).int除了最小值之外,它对整个值范围有效,即-2147483648编译因错误而失败

超载的召唤display(long int)是模棱两可的

但是将相同的值与a int打印并给出值2147483648.我对这种行为感到困惑.

为什么只有在传递最负数时才会观察到这种行为?(该行为是相同的,如果一个short使用具有-32768-事实上,在负数和正数具有相同的二进制表示任何情况下)

使用的编译器:g ++(GCC)4.8.5

c++ integer overloading negative-number ambiguous-call

88
推荐指数
2
解决办法
4553
查看次数

为什么在32位机器中 - ( - 2147483648)= - 2147483648?

我认为问题是自我解释的,我想它可能与溢出有关,但我仍然不太明白.引擎盖下发生了什么?

为什么-(-2147483648) = -2147483648(至少在用C编译时)?

c 32-bit twos-complement

62
推荐指数
3
解决办法
1万
查看次数

在C中将双精度转换为整数时处理溢出

今天,我注意到当我将一个大于最大可能整数的double转换为整数时,我得到-2147483648.类似地,当我转换一个小于最小可能整数的double时,我也得到-2147483648.

是否为所有平台定义了此行为?
在/溢出下检测此问题的最佳方法是什么?在演示之前将if语句放入min和max int是最佳解决方案吗?

c c++ floating-point casting

25
推荐指数
4
解决办法
1万
查看次数

警告:此十进制常量仅在ISO C90中无符号

一段代码:

long rangeVar = 0;
rangeVar = atol(p_value);

if (rangeVar >= -2147483648 && rangeVar <= 2147483647)
Run Code Online (Sandbox Code Playgroud)

在编译时,我得到:

警告:此十进制常量仅在ISO C90中无符号

提前致谢

c compiler-construction iso c89 long-integer

20
推荐指数
2
解决办法
3万
查看次数

为什么MSVC选择长期为-2147483648的类型?

我的片段:

auto i = -2147483648;
int j = 3;
std::swap(i, j); // Compile error about mismatched types here. 
Run Code Online (Sandbox Code Playgroud)

编译器声明文字i是a long long.这是为什么?-2147483648适用于intMSVC x64.

我的编译器是MSVC,目标是64位.

c++ visual-c++

12
推荐指数
1
解决办法
643
查看次数

为什么它在-2147483648和(int)-2147483648之间有所不同

当我在使用GCC of MinGW编译的Windows7 x64下运行以下代码时,结果似乎是下溢的:

cout<<-2147483648 ;    //Output: 2147483648
Run Code Online (Sandbox Code Playgroud)

但是当我将它分配给整数变量时,或者只是简单地将其转换为int类型:

cout<<(int)-2147483648 ; //Output: -2147483648 
Run Code Online (Sandbox Code Playgroud)

那么,我的代码的先前版本出了什么问题?我不是int类型吗?或整数的下界究竟是什么?非常感谢.

c++ integer overflow

8
推荐指数
1
解决办法
4182
查看次数

-9'223'372'036'854'775'808LL 未签名

由于 C++20 二进制补码表示是标准允许的唯一表示,保证范围从 -2 N-1到 +2 N-1 -1。因此,对于 64 位有符号整数类型,范围从-9'223'372'036'854'775'8089'223'372'036'854'775'807。但是,此代码不能在 Visual Studio 上编译(也不能在 gcc 上编译)

int main()
{
    long long x{-9'223'372'036'854'775'808LL};
    // error C4146: unary minus operator applied to unsigned type, result still unsigned
    // error C2397: conversion from 'unsigned __int64' to '__int64' requires a narrowing conversion
}
Run Code Online (Sandbox Code Playgroud)

然而,如果我用long long x{-9'223'372'036'854'775'807LL - 1}compiles替换代码就好了并x保持正确的值。我没有得到什么?

c++ integer literals long-long c++20

5
推荐指数
1
解决办法
274
查看次数

C++整型常量的类型

根据MSDN(整数类型 - VC2008):

没有后缀的十进制常量的类型是int,long int或unsigned long int.可以表示常量值的这三种类型中的第一种是分配给常量的类型.

Visual C++ 2008上运行以下代码:

void verify_type(int a){printf("int [%i/%#x]\n", a, a);}
void verify_type(unsigned int a){printf("uint [%u/%#x]\n", a, a);}
void verify_type(long a){printf("long [%li/%#lx]\n", a, a);}
void verify_type(unsigned long a){printf("ulong [%lu/%#lx]\n", a, a);}
void verify_type(long long a){printf("long long [%lli/%#llx]\n", a, a);}
void verify_type(unsigned long long a){printf("unsigned long long [%llu/%#llx]\n", a, a);}

int _tmain(int argc, _TCHAR* argv[])
{
    printf("sizeof(int) %i\n", sizeof(int));
    printf("sizeof(long) %i\n", sizeof(long));
    printf("sizeof(long long) %i\n\n", sizeof(long long));

    verify_type(-2147483647);
    verify_type(-2147483648);

    getchar();
    return 0; …
Run Code Online (Sandbox Code Playgroud)

c++ types long-integer

3
推荐指数
3
解决办法
663
查看次数