2

In C, how can I write a program to find the maximum value of an integer variable? As far as I know the maximum value of an integer variable is 2147483647 but how can I represent this in a C program ?

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42

4 Answers4

9

As ı know the maximum value of a integer variable is 2147483647

That's a wrong assumption. The maximum value of an int can vary across systems. For example, on a 16-bit machine maximum value of int is not 2147483647.

You don't need to find it yourself. There are pre-defined macros which you can use from <limits.h>. For example, INT_MAX represents the maximum value an int can hold.

P.P
  • 117,907
  • 20
  • 175
  • 238
7

You cannot find this value programatically by means of computation. There is no way to detect that you're "at the end", and incrementing the largest integer has undefined behaviour.

That's why the largest and smallest values of integral types are made available to you by the standard library in the <limits.h> header. It is your responsibility to ensure that the results of operations fit into the desired types.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • Okey it is kind of a homework but ı cannot understand the homework but the answer is very explaining thanks for that. –  Oct 19 '16 at 09:56
  • @Kerrek You can find these programatically. The values returned should be specific to the environment and compiler used. See my example below. – Peter H Oct 19 '16 at 10:04
  • 1
    @PeterH: Well, that's my answer, too. Whether you consider that "finding" is nitpicking, I suppose. I used "finding" in the sense of "computing", rather than "looking up". Reworded a bit. – Kerrek SB Oct 19 '16 at 10:51
  • @KerrekSb Check out this post computing the maximum value of int, might be interesting for you https://stackoverflow.com/questions/31897880/how-to-programmatically-determine-maximum-and-minimum-limit-of-int-data-in-c – LauroSkr Nov 19 '18 at 19:17
  • @LauroSkr: Do you mean the question or the accepted answer? The code in the question has UB, and the answer, well, that basically just amount to *asserting* the value, right? I might as well just say `0x7FFFFFFF` and declare this the answer. It's not really a *computation*. How do you *know* it's the max? – Kerrek SB Nov 19 '18 at 23:38
  • Hi @KerrekSB Thank you for your answer. I would like to upvote it. Could you please show the source to support your statement "incrementing the largest integer has undefined behaviour"? – aafulei Jan 02 '20 at 08:07
  • @aafulei: How about C11 6.5p5? The result (which for `+` is "the sum") is not representable for its type. – Kerrek SB Jan 03 '20 at 01:46
  • @KerrekSB Thank you. That's what I am asking about. Answer upvoted. Quoting C11 (draft N1570) 6.5 (5) `If an exceptional condition occurs during the evaluation of an expression (that is, if the result is not mathematically defined or not in the range of representable values for its type), the behavior is undefined.` – aafulei Jan 03 '20 at 03:49
2

The maximum and minimum values are defined in limits. You can verify these using the following code.

C Version:

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


int main()
{
    printf("Minimum Int value:%d\n", INT_MIN);
    printf("Minimum Int value:%d\n", INT_MAX);
    return 0;
}

In C++

#include "stdafx.h"
#include <iostream>
#include <limits>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Minimum Int value:" << INT_MIN << endl;
    cout << "Maximum Int value:" << INT_MAX << endl;
    return 0;
}

An alternate (Non Visual Studio Version is below):

#include <iostream>
#include <string>
#include <limits.h>
using namespace std;
int main()
{
    std::cout << "Minimum Int value:" << INT_MIN << endl;
    std::cout << "Maximum Int value:" << INT_MAX << endl;
}

The constant names E.G INT_MIN and INT_MAX can be found in the website below if you are interesting in the maximum values and minimum values of other data types.
http://www.cplusplus.com/reference/climits/

Peter H
  • 871
  • 1
  • 10
  • 33
0

I know it is late, but if one has to calculate as an assignment, one can use sizeof() operator and this will give the number of bytes an integer holds.

2^((number of bytes*8) - 1) should give the max value of an integer.

Of course sizeof()too is compile time just like INT_MAX.

Kalana
  • 5,631
  • 7
  • 30
  • 51
Ajay
  • 1