Series: Subject: Topic:
Question: 577 of 587

# How can you determine the maximum value that a numeric variable can hold?

For integral types, on a machine that uses twos complement arithmetic (which is just about any machine youre likely to use), a signed type can hold numbers from 2(number of bits 1) to +2(number of bits 1) 1. An unsigned type can hold values from 0 to +2(number of bits) 1. For instance, a 16-bit signed integer can hold numbers from 2^15 (32768) to +2^15 1 (32767).
Zubair Ahmed

Answered On : Apr 4th, 2007

Hi,

a simple code snipet show how to determin the maximum value that an integer can  have

unsigned int x = 0;
--x;

now check the  value of x. This would contain maximum value that an interger can have.
now you can determine the values containing by the signed int.

Zubair A.

1 User has rated as useful.

shareentaj

Answered On : Dec 27th, 2008

Consider the 3 bits

For signed int these 3 bits contains both positive and negative values

you can find max and min values as follows

-2 ^ ( n-1 ) to ( 2 ^ ( n-1 ) - 1 ) ie,  n is no. of bits

-4, -3, -2, -1, 0, 1, 2, 3

For unsigned int you can find the max and min value as follow

0 to 2 ^ ( n ) - 1

ie,  0, 1,2,3,4,5,6,7

In Turbo C int data type contain 2 byte ( ie 16 bits ) apply the above formule to find out the max and min value.

like this char occupy 1 byte ( 8 bits )

you can find out the max and min value for char also.

Regards,
Shareen Taj.A

kbjarnason

Answered On : Jul 2nd, 2010

The correct way is to include <limits.h> and use the defined macros such as INT_MIN and INT_MAX, which define the minimum and maximum values which the type can hold.

In terms of designing your code, it helps to know that C imposes "minimum maximums" - eg a signed char must be able to hold _at least_ the values -127 to 127; a signed int must be able to hold _at least_ the values -32767 to 32767 and so forth.

Be wary of assuming that because a type is N bits wide, it can store 2^N-1 possible values; there is absolutely no guarantee this is true.

1 User has rated as useful.

rocking.piyush

Answered On : Feb 22nd, 2011

```Codemain()
{
int x = 1;
printf("%dn", sizeof(x));
while (x > 0)
x++;
printf("Maximum value = %dn", (-1 * x) - 1);
system("pause");
}

main()
{
unsigned int x = 1;
printf("%dn", sizeof(x));
while (x > 0)
x++;
printf("Maximum value = %un", (-1 * x) - 1);
system("pause");
}

```

VENU BABU

Answered On : Oct 7th, 2011

first include limits.h
in that so many variables to find max and min value that can hold by any data type
for ex: INT_MAX,INT_MIN for integer and so on.......

Jimmy Jack

Answered On : Apr 25th, 2012

```Code#include
#include

template T get_min_value(void)
{
return (T) (1 << ((sizeof(T) << 3) - 1));
}

template T get_max_value(void)
{
return (~(T) 0 - get_min_value());
}

template T get_umin_value(void)
{
return (T) 0;
}

template T get_umax_value(void)
{
return (~(T) 0);
}

int main()
{
std::cout << "minmum signed integer - " << get_min_value() << std::endl;
std::cout << "maxmum signed integer - " << get_max_value() << std::endl;
std::cout << "minmum unsigned integer - " << get_umin_value() << std::endl;
std::cout << "maxmum unsigned integer - " << get_umax_value() << std::endl;

std::cout << "minmum signed short - " << get_min_value() << std::endl;
std::cout << "maxmum signed short - " << get_max_value() << std::endl;
std::cout << "minmum unsigned short - " << get_umin_value() << std::endl;
std::cout << "maxmum unsigned short - " << get_umax_value() << std::endl;
}

```