INT_MAX and INT_MIN in C/C++ and Applications

CC++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to understand INT_MAX and INT_MIN in C/C++.

INT_MIN and INT_MAX are macros that are defined to set the minimum and maximum value for a variable/element.

Example

 Live Demo

#include<bits/stdc++.h>
int main(){
   printf("%d\n", INT_MAX);
   printf("%d", INT_MIN);
   return 0;
}

Output

2147483647
-2147483648

Application

Calculating MIN value in an array

Example

 Live Demo

#include <bits/stdc++.h>
//calculating minimum element in an array
int compute_min(int arr[], int n){
   int MIN = INT_MAX;
   for (int i = 0; i < n; i++)
   MIN = std::min(MIN, arr[i]);
   std::cout << MIN;
}
int main(){
   int arr[] = { 2019403813, 2147389580, 2145837140, 2108938594, 2112076334 };
   int n = sizeof(arr) / sizeof(arr[0]);
   compute_min(arr, n);
   return 0;
}

Output

2019403813
raja
Published on 01-Apr-2020 09:57:24
Advertisements