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

For integral types, on a machine that uses two’s complement arithmetic (which is just about any machine you’re 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).  

Showing Answers 1 - 6 of 6 Answers

Zubair Ahmed

  • 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.

shareentaj

  • 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



  Was this answer useful?  Yes

kbjarnason

  • 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.

Code
  1. span style="color: #ff0000;">"%dn""Maximum value = %dn", (-1 * x) - 1);

  2.     system("pause""%dn""Maximum value = %un", (-1 * x) - 1);

  3.     system("pause");

  4. }

  5.  

  6.  

  Was this answer useful?  Yes

VENU BABU

  • 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.......

  Was this answer useful?  Yes

Jimmy Jack

  • Apr 25th, 2012
 

Code
  1. #include

  2. #include

  3. "minmum signed integer - ""maxmum signed integer - ""minmum unsigned integer - ""maxmum unsigned integer - ""minmum signed short - ""maxmum signed short - ""minmum unsigned short - ""maxmum unsigned short - " << get_umax_value() << std::endl;

  4. }

  5.  

  6.  

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions