top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Finding the maximum or minimum numeric value of a variable in C/C++

+12 votes
645 views

The easiest way to find out what min or max value a number that a particular type can hold is to use the values defined in the ANSI standard header file limits.h. This file contains many useful constants defining the values that can be held by various types, including these:

Value           Description
CHAR_BIT    -   Number of bits in a char
CHAR_MAX    -   Maximum decimal integer value of a char
CHAR_MIN    -   Minimum decimal integer value of a char
MB_LEN_MAX  -   Maximum number of bytes in a multibyte character
INT_MAX     -   Maximum decimal value of an int
INT_MIN     -   Minimum decimal value of an int
LONG_MAX    -   Maximum decimal value of a long
LONG_MIN    -   Minimum decimal value of a long
SCHAR_MAX   -   Maximum decimal integer value of a signed char
SCHAR_MIN   -   Minimum decimal integer value of a signed char
SHRT_MAX    -   Maximum decimal value of a short
SHRT_MIN    -   Minimum decimal value of a short
UCHAR_MAX   -   Maximum decimal integer value of unsigned char
UINT_MAX    -   Maximum decimal value of an unsigned integer
ULONG_MAX   -   Maximum decimal value of an unsigned long int
USHRT_MAX   -   Maximum decimal value of an unsigned short int

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

posted Dec 27, 2013 by Luv Kumar

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

This is based on Sakamoto's algorithm for find-out the day of the given date if we follow the Georgian calender.

Explanation

Source

year -= month < 3 is a nice trick. It creates a "virtual year" that starts on March 1 and ends on February 28 (or 29), putting the extra day (if any) at the end of the year; or rather, at the end of the previous year. So for example, virtual year 2011 began on Mar 1 and will end on February 29, while virtual year 2012 will begin on March 1 and end on the following February 28.

By putting the added day for leap years at the end of the virtual year, the rest of the expression is massively simplified.

Let's look at the sum:

(year + year/4 - year/100 + year/400 + t[month-1] + date) % 7

There are 365 days in a normal year. That is 52 weeks plus 1 day. So the day of the week shifts by one day per year, in general. That is what the y term is contributing; it adds one to the day for each year.

But every four years is a leap year. Those contribute an extra day every four years. Thanks to the use of virtual years, we can just add y/4 to the sum to count how many leap days happen in y years. (Note that this formula assumes integer division rounds down.)

But that is not quite right, because every 100 years is not a leap year. So we have to subtract off y/100.

Except that every 400 years is a leap year again. So we have to add y/400.

Finally we just add the day of the month d and an offset from a table that depends on the month (because the month boundaries within the year are fairly arbitrary). Then take the whole thing mod 7 since that is how long a week is.

Sample Code

#include<stdio.h>

int day_of_week(int date, int month, int year)
{
  static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
  int dow;

  year -= month < 3;
  dow = ( year + year/4 - year/100 + year/400 + t[month-1] + date) % 7;

  printf("\nDate: %d/%d/%d\n\n",date,month,year);

  switch (dow)
  {
    case 0:
        printf("Day = Sunday");
        break;
    case 1:
        printf("Day = Monday");
        break;
    case 2:
        printf("Day = Tuesday");
        break;
    case 3:
        printf("Day = Wednesday");
        break;
    case 4:
        printf("Day = Thursday");
        break;
    case 5:
        printf("Day = Friday");
        break;
    case 6:
        printf("Day = Saturday");
        break;
    default:
        printf("Wrong data");
  }

  return 0;
}

void main()
{
  int d,m,y;

  printf("Enter the year ");
  scanf("%d",&y);

  printf("Enter the month ");
  scanf("%d",&m);

  printf("Enter the date ");
  scanf("%d",&d);

  day_of_week(d,m,y);
}

Output

Enter the year 2014
Enter the month 5
Enter the date 26

Date: 26/5/2014

Day = Monday
READ MORE
...