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.
Login to rate this answer.
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
Login to rate this answer.
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.
Login to rate this answer.
Code
main()
{
int x = 1;
while (x > 0)
x++;
printf("Maximum value = %dn", (-1 * x
) - 1);
system("pause");
}
main()
{
unsigned int x = 1;
while (x > 0)
x++;
printf("Maximum value = %un", (-1 * x
) - 1);
system("pause");
}
Login to rate this answer.
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.......
Login to rate this answer.
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;
}
Login to rate this answer.