CSCD-255
2015-02-03


Expanding Input and Output Operations (Chs. 18 and 19)

Note:  We will not be using the material on getch and putch.  These are not part of standard C but were added in some C implementations that want to access the console (the screen and keyboard).  Their definitions occur in the conio.h include file — console I/O and are not available on the Linux computers on campus.

Input and output can be done one character at a time without using scanf and printf:  the functions are int getchar() and int putchar(int).  The putchar function returns the character written as an
unsigned char cast to an int or EOF on error.  The getchar()  function actually reads from the keyboard buffer and not directly from the keyboard.  Consequently the information is not available until the user has ended the line.  You have seen this in the in-class example for stripping the pending \n character.

void EOLn()        // Move past the End-Of-Line
{
   while (getchar() != '\n')
      ;            // The null statement --- does nothing
}

The getchar() function consumes and returns the next character in the keyboard buffer or EOF at the end of file or on error.

The "Echo" program does exactly that — sending back to the user exactly the line that was just entered.
Echo.txt   Echo.c   Echo.exe

int main()
{
   int ch;

   printf("Terminate the program by forcing an EOF condition (^Z)\n");
   do
   {
      ch = getchar();
      putchar(ch);
   } while (ch != EOF);

   return 0;
}

At the other end, C has built-in functions to access an entire line of text.  These necessarily deal with character arrays.  the gets function consumes the entire line of text through the \n character and replaces the \n character with the NULL character to terminate the string.
Echo2.txt   Echo2.c   Echo2.exe

int main()
{
   char buffer[128];

   printf("Terminate the program by entering QUIT\n");
   do
   {
      gets(buffer);
      puts(buffer);
   } while (strcmp("QUIT", buffer) != 0);   // See below

   return 0;
}

Notice that the  function does not indicate how large the character array is.  It is the programmer's responsibility to insure that the array can hold whatever is entered.


String Operations (Ch. 19)

C has a number of build-in functions to facility dealing with characters and strings in character arrays.  Access to functions involving single characters are available through  #include <ctype.h>.  Instead of checking whether c is in the range of 'a' through 'z' --- if (c >= 'a' && c <= 'z') --- one can use the "islower" function — if (islower(c)).  C provides a number of character testing function.  Our text identifies isupper(). islower(), and isdigit().  One can also check for alphabetic [isalpha()], alphanumeric [isalnum()], and several other categories.

There are also two functions to convert between upper case and lower case alphabetic characters:  toupper() and tolower().  The value returned is the converted letter (if that is possible) or the letter passed if conversion was not possible — such as a nonalphabetic character.  Thus the test we have seen several times [ if (answ == 'y' || answ == 'Y') ] becomes simply if (toupper(answ) == 'Y').

Access the functions dealing with character arrays are available through #include <string.h>

Specimen program:  user dialog resulting in one line through  gets, strcpy, and strcat.
Dialog.txt   Dialog.c   Dialog.exe