📜  C / C++和应用程序中的INT_MAX和INT_MIN

📅  最后修改于: 2021-05-25 20:13:55             🧑  作者: Mango

大多数时候,在竞争性编程中,需要分配变量,数据类型可以容纳的最大值或最小值,但是记住这么大而精确的数字却是一件困难的工作。因此,C++具有某些宏来表示这些数字,因此可以直接将它们分配给变量,而无需实际键入整数。
INT_MAX是一个宏,它指定整数变量不能存储超出此限制的任何值。
INT_MIN指定整数变量不能存储任何低于此限制的值。

Values of INT_MAX and INT_MIN may vary
from compiler to compiler. Following are
typical values in a compiler where integers
are stored using 32 bits.

Value of INT_MAX is +2147483647.
Value of INT_MIN is -2147483648.
CPP
// C++ program to print values of INT_MAX
// and INT_MIN
#include 
using namespace std;
int main()
{
    cout << INT_MAX << endl;
    cout << INT_MIN;
    return 0;
}


C
// C program to print values of INT_MAX
// and INT_MIN
// we have to include limits.h for results in C
#include 
#include 
int main()
{
    printf("%d\n", INT_MAX);
    printf("%d", INT_MIN);
}


CPP
// C++ code to check for Integer overflow while
// adding 2 numbers
#include 
 
// Function to check integer overflow
int check_overflow(int num1, int num2)
{
    // Checking if addition will cause overflow
    if (num1 > INT_MAX - num2)
        return -1;
 
    // No overflow occured
    else
        return num1 + num2;
}
 
// Driver code
int main()
{
    // The sum of these numbers will equal INT_MAX
    // If any of them is incremented by 1, overflow
    // will occur
    int num1 = 2147483627;
    int num2 = 20;
 
    // Result is -1 if overflow occurred
    // Stores the sum, otherwise
    int result = check_overflow(num1, num2);
 
    // Overflow occurred
    if (result == -1)
        std::cout << "Integer overflow occurred";
 
    // No overflow
    else
        std::cout << result;
}


CPP
// C++ code to compute MIN element
#include 
 
// Function to compute minimum element in array
int compute_min(int arr[], int n)
{
    // Assigning highest value
    int MIN = INT_MAX;
 
    // Traversing and updating MIN
    for (int i = 0; i < n; i++)
        MIN = std::min(MIN, arr[i]);
 
    // Printing MIN element
    std::cout << MIN;
}
 
// Driver code
int main()
{
    // array with MIN to compute
    int arr[] = { 2019403813, 2147389580, 2145837140,
                  2108938594, 2112076334 };
 
    // size of array
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function call to compute MIN
    compute_min(arr, n);
}


输出
2147483647
-2147483648

INT_MAX和INT_MIN的应用:

1.检查整数溢出:

CPP

// C++ code to check for Integer overflow while
// adding 2 numbers
#include 
 
// Function to check integer overflow
int check_overflow(int num1, int num2)
{
    // Checking if addition will cause overflow
    if (num1 > INT_MAX - num2)
        return -1;
 
    // No overflow occured
    else
        return num1 + num2;
}
 
// Driver code
int main()
{
    // The sum of these numbers will equal INT_MAX
    // If any of them is incremented by 1, overflow
    // will occur
    int num1 = 2147483627;
    int num2 = 20;
 
    // Result is -1 if overflow occurred
    // Stores the sum, otherwise
    int result = check_overflow(num1, num2);
 
    // Overflow occurred
    if (result == -1)
        std::cout << "Integer overflow occurred";
 
    // No overflow
    else
        std::cout << result;
}
输出
2147483647

同样,我们可以在使用INT_MIN减去2个数字的同时检查溢出。

2.在具有大元素的数组中计算MIN
我们通常为MIN分配一个高值,以计算数组中的最小值。但是,如果数组包含大元素,则必须为数组分配尽可能高的值。

下面是C++的实现:

CPP

// C++ code to compute MIN element
#include 
 
// Function to compute minimum element in array
int compute_min(int arr[], int n)
{
    // Assigning highest value
    int MIN = INT_MAX;
 
    // Traversing and updating MIN
    for (int i = 0; i < n; i++)
        MIN = std::min(MIN, arr[i]);
 
    // Printing MIN element
    std::cout << MIN;
}
 
// Driver code
int main()
{
    // array with MIN to compute
    int arr[] = { 2019403813, 2147389580, 2145837140,
                  2108938594, 2112076334 };
 
    // size of array
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function call to compute MIN
    compute_min(arr, n);
}
输出
2019403813

同样,可以使用INT_MIN在大量数组中找到MAX。

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。