/* Character input interpreted as hexnumbers. */

#include <stdio.h>

/* 
   Reads four characters and interprets the four characters as 
   a four digit hexadecimal number. This value is returned as the 
   result of the function.
*/
int readhex(void)
{ 
  int num, digit, i;
  char c;

  num=0; i=0;
  while ( i < 4) {
    c=getchar();
    if (('0' <= c) && (c <= '9'))
      digit = c - '0';
    else
    if (('a' <= c) && (c <= 'f'))
      digit = (c - 'a') + 10;
    else /* Illegal hexdigits, no errorcontrol. */
      digit = 0;      
    num = num*16 + digit;
    i++;
  }

  return num;
}

int main()
{
  printf("Enter four hexadecimal digits followed by new line: ");
  printf("readhex = %d. \n", readhex());

}
