GeekInterview.com
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).  
Asked by: Interview Candidate | Asked on: Mar 6th, 2005
Showing Answers 1 - 6 of 6 Answers
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.

Yes  1 User has rated as useful.
  
Login to rate this answer.
shareentaj

Answered On : Dec 27th, 2008

View all answers by shareentaj

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

Answered On : Jul 2nd, 2010

View all answers by kbjarnason

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.

Yes  1 User has rated as useful.
  
Login to rate this answer.
rocking.piyush

Answered On : Feb 22nd, 2011

View all answers by rocking.piyush

Code
  1. main()
  2. {
  3.     int x = 1;
  4.     printf("%dn", sizeof(x));
  5.     while (x > 0)
  6.         x++;
  7.     printf("Maximum value = %dn", (-1 * x) - 1);
  8.     system("pause");
  9. }
  10.  
  11. main()
  12. {
  13.     unsigned int x = 1;
  14.     printf("%dn", sizeof(x));
  15.     while (x > 0)
  16.         x++;
  17.     printf("Maximum value = %un", (-1 * x) - 1);
  18.     system("pause");
  19. }
  20.  
  21.  

  
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
  1. #include
  2. #include
  3.  
  4. template T get_min_value(void)
  5. {
  6.     return (T) (1 << ((sizeof(T) << 3) - 1));
  7. }
  8.  
  9. template T get_max_value(void)
  10. {
  11.     return (~(T) 0 - get_min_value());
  12. }
  13.  
  14. template T get_umin_value(void)
  15. {
  16.     return (T) 0;
  17. }
  18.  
  19. template T get_umax_value(void)
  20. {
  21.     return (~(T) 0);
  22. }
  23.  
  24. int main()
  25. {
  26.     std::cout << "minmum signed integer - " << get_min_value() << std::endl;
  27.     std::cout << "maxmum signed integer - " << get_max_value() << std::endl;
  28.     std::cout << "minmum unsigned integer - " << get_umin_value() << std::endl;
  29.     std::cout << "maxmum unsigned integer - " << get_umax_value() << std::endl;
  30.  
  31.     std::cout << "minmum signed short - " << get_min_value() << std::endl;
  32.     std::cout << "maxmum signed short - " << get_max_value() << std::endl;
  33.     std::cout << "minmum unsigned short - " << get_umin_value() << std::endl;
  34.     std::cout << "maxmum unsigned short - " << get_umax_value() << std::endl;
  35. }
  36.  
  37.  

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.