在C中使最小整数值为正

时间:2021-12-17 03:25:14

So I've got the following code

所以我有以下代码

printf("atoi(input) = %d\n", atoi(input));
long number = 0;
if(atol(input) < 0){
    printf("went into here\n");
    printf("atoi = %d\n", atoi(input)); 
    number = -1 * atoi(input);  
    printf("number = %d\n", number);    
}else{
    number = atoi(input);
}

But for some reason when my user inputs -2147483648 (and probably other really large decimals), the multiply by -1 doesn't work, and the number remains negative. Any ideas?

但由于某些原因,当我的用户输入-2147483648(可能还有其他非常大的小数)时,乘以-1不起作用,并且数字仍为负数。有任何想法吗?

3 个解决方案

#1


1  

Unfortunately memory allocated for any standard tipe is limited, so ranges of all all types have quite definite borders. Violated borders lead to computing errors.

不幸的是,为任何标准tipe分配的内存都是有限的,因此所有类型的范围都有相当明确的边界。违规边界会导致计算错误。

To know borders (like min and max possible values) for integer types (not only int, but also char, short, etc.) you can use limits.h. E.g.:

要知道整数类型的边界(如最小和最大可能值)(不仅是int,还有char,short等),您可以使用limits.h。例如。:

#include <stdio.h>
#include <limits.h>

int main(void)
{
    printf("INT_MIN = %d\n", INT_MIN);
    printf("INT_MAX = %d\n", INT_MAX);
    printf("LONG_MIN = %ld\n", LONG_MIN);
    printf("LONG_MAX = %ld\n", LONG_MAX);
    printf("LLONG_MIN = %lld\n", LLONG_MIN);
    printf("LLONG_MAX = %lld\n", LLONG_MAX);
    return 0;
}

Note:

注意:

1) values of limits depends on compiler and platform (e.g. for my system LONG_MIN is equal to INT_MIN, but perhaps, you will see different values);

1)限制值取决于编译器和平台(例如,对于我的系统LONG_MIN等于INT_MIN,但也许,你会看到不同的值);

2) limits.h is just text file that can be oppend and explored

2)limits.h只是可以选择和探索的文本文件

3) also learn about stdint.h, that provide types with fixed sizes (and allows programs to be portable without "perhaps" in my first note)

3)也学习stdint.h,它提供固定大小的类型(并且允许程序在我的第一个音符中没有“或许”可移植)

4) there are some techniques to detect integer overflow

4)有一些技术可以检测整数溢出

#2


1  

For both atoi() and atol(); if the converted value is out of range for the return type (int and long int respectively), then you get undefined behaviour (e.g. "format hard drive").

对于atoi()和atol();如果转换后的值超出了返回类型的范围(分别为int和long int),则会得到未定义的行为(例如“格式化硬盘”)。

To avoid undefined behaviour you must ensure that the value in the string is within a certain range before using either atoi() or atol().

为避免未定义的行为,在使用atoi()或atol()之前,必须确保字符串中的值在某个范围内。

Any technique that correctly ensures the value in the string is within a certain range (without using either atoi() or atol()) is also a technique that can be modified slightly and used to detect and handle the INT_MIN case (without using either atoi() or atol()).

任何正确确保字符串中的值都在一定范围内的技术(不使用atoi()或atol())也是一种可以稍微修改并用于检测和处理INT_MIN情况的技术(不使用任何一个atoi) ()或atol())。

#3


0  

You are running into the value limit for a signed long integer. You will need to try using another data type.

您正在遇到有符号长整数的值限制。您需要尝试使用其他数据类型。

#1


1  

Unfortunately memory allocated for any standard tipe is limited, so ranges of all all types have quite definite borders. Violated borders lead to computing errors.

不幸的是,为任何标准tipe分配的内存都是有限的,因此所有类型的范围都有相当明确的边界。违规边界会导致计算错误。

To know borders (like min and max possible values) for integer types (not only int, but also char, short, etc.) you can use limits.h. E.g.:

要知道整数类型的边界(如最小和最大可能值)(不仅是int,还有char,short等),您可以使用limits.h。例如。:

#include <stdio.h>
#include <limits.h>

int main(void)
{
    printf("INT_MIN = %d\n", INT_MIN);
    printf("INT_MAX = %d\n", INT_MAX);
    printf("LONG_MIN = %ld\n", LONG_MIN);
    printf("LONG_MAX = %ld\n", LONG_MAX);
    printf("LLONG_MIN = %lld\n", LLONG_MIN);
    printf("LLONG_MAX = %lld\n", LLONG_MAX);
    return 0;
}

Note:

注意:

1) values of limits depends on compiler and platform (e.g. for my system LONG_MIN is equal to INT_MIN, but perhaps, you will see different values);

1)限制值取决于编译器和平台(例如,对于我的系统LONG_MIN等于INT_MIN,但也许,你会看到不同的值);

2) limits.h is just text file that can be oppend and explored

2)limits.h只是可以选择和探索的文本文件

3) also learn about stdint.h, that provide types with fixed sizes (and allows programs to be portable without "perhaps" in my first note)

3)也学习stdint.h,它提供固定大小的类型(并且允许程序在我的第一个音符中没有“或许”可移植)

4) there are some techniques to detect integer overflow

4)有一些技术可以检测整数溢出

#2


1  

For both atoi() and atol(); if the converted value is out of range for the return type (int and long int respectively), then you get undefined behaviour (e.g. "format hard drive").

对于atoi()和atol();如果转换后的值超出了返回类型的范围(分别为int和long int),则会得到未定义的行为(例如“格式化硬盘”)。

To avoid undefined behaviour you must ensure that the value in the string is within a certain range before using either atoi() or atol().

为避免未定义的行为,在使用atoi()或atol()之前,必须确保字符串中的值在某个范围内。

Any technique that correctly ensures the value in the string is within a certain range (without using either atoi() or atol()) is also a technique that can be modified slightly and used to detect and handle the INT_MIN case (without using either atoi() or atol()).

任何正确确保字符串中的值都在一定范围内的技术(不使用atoi()或atol())也是一种可以稍微修改并用于检测和处理INT_MIN情况的技术(不使用任何一个atoi) ()或atol())。

#3


0  

You are running into the value limit for a signed long integer. You will need to try using another data type.

您正在遇到有符号长整数的值限制。您需要尝试使用其他数据类型。