Scroll to top

NAME

get_long - prompts user for a line of text from stdin and returns the equivalent long

SYNOPSIS

#include <cs50.h>
long get_long(const char *format, ...);

DESCRIPTION

Prompts user for a line of text from standard input and returns the equivalent long; if the text does not represent a long or would cause overflow, user is reprompted.

The prompt is formatted like printf(3).

RETURN VALUE

Returns the long equivalent to the line read from stdin in [LONG_MIN, LONG_MAX). If line can’t be read, returns LONG_MAX.

EXAMPLE

 /**
  * Returns the difference of two longs read from stdin, or LONG_MAX if there was an error.
  */
  long subtract_longs(void)
  {
      // read long from stdin
      long i = get_long("Enter a long: ");

      // make sure we read one successfully
      if (i == LONG_MAX)
      {
          return LONG_MAX;
      }

      long j = get_long("What do you want to subtract from %ld? ", i);

      if (j == LONG_MAX)
      {
          return LONG_MAX;
      }

      return i - j;

  }

SEE ALSO

get_char(3), get_double(3), get_float(3), get_int(3), get_string(3), printf(3)