while and getchar()

CS35: Programming and Problem Solving
Ray Ontko
Department of Computer Science
Earlham College

We first encountered looping or iteration or repetition in in the pounds.c program. In that example we printed a dollar amount and the equivalent british currency amount for a number of different dollar amounts; we repeated a process for each dollar amount.

Here we will explore the while statement of C and show how it can be used in a variety of contexts, with numbers and characters, to repeat a process a number of times.

The while statement consists of the while keyword followed by condition enclosed between ( and ) which is followed by a statement or block of statements enclosed between { and }. For example:

main()
{
  int n ;

  n = 0 ;
  while ( n < 10 )
  {
    printf( "%d\n" ) ;
    n = n + 1 ;
  }
}

would print the numbers 0 through 9, one per line.

Counting Numbers

The following program counts the number of numbers entered up to the first non-number, or until the end of the input. (In our environment, you can indicate the end of input by typing CTRL-D. Note that this is differnt from simply halting a program, which you can indicate by typing CTRL-C.)

Note that we make use of the return value of the scanf() function. The value returned by scanf is the number of values read which match the format specifier (in our case, "%f"). If There is no number read, we terminate our loop.

We also make use of the increment operator ("++") in the statement:

  count ++ ;
You may think of it as being equivalent to:
  count = count + 1 ;

Without further delay, here is our number-counting program.

/*
number_count.c

program to count numbers entered.
*/

#include 

main()
{
float f ;
int count ;

count = 0 ;
while ( scanf("%f" , &f ) == 1 )
    count ++ ;

printf( "%d numbers\n" , count ) ;
}   

Counting Characters

Another useful example of the while statement comes from the realm of programs which read text. In this example, we read text input and count the number of characters (letters, spaces, punctuation, ends-of-lines, etc.), and display the number of characters counted.

Note that this program makes use of the getchar() function, which reads a single character of input and returns that character as the value of the function. If there is an error reading the character, or if the end of input is reached, getchar() returns a special value, represented below by "EOF".

We also make use of the return value of the assignment statement. In C, an assignment statement has as its value, the value assigned to the variable to the left of the "=". In our example, below, the while continues to execute as long as we assign values to the variable "c" which are not equal to the special value EOF.

/*
char_count.c

A program to count characters of input.
*/

#include 

main()
{
int c ;
int count ;

while ( ( c = getchar() ) != EOF )
   count ++ ;

printf( "%d characters\n" , count ) ;
}

Counting Letters

One final example program. In this case we wish to count only the number of letters (i.e., alphabetic characters, 'A' through 'Z' or 'a' through 'z').

Note that when we wish to refer to a character as a literal (by its "printed name"), we enclose it in single quotes ('). In C, when we wish to refer to a single character, we enclose it in single quotes; when we wish to refer to a string of characters, we enclose them in double quotes. Further note that we can use the escape character ("\") to refer to certain non-printing characters, such as the end of line character ('\n').

/*
letter_count.c

A program to count letters in input.
*/

#include 

main()
{
int c ;
int count ;

while ( ( c = getchar() ) != EOF )
  if ( ( c >= 'A' ) && ( c <= 'Z' ) ||
       ( c >= 'a' ) && ( c <= 'z' ) ) 
    count ++ ;

printf( "%d letters\n" , count ) ;
}

Reading Input from a File

One final thought. You can tell unix to read input from a file in your directory instead of requiring you to type it each time you run your program. For example, suppose I have a poem in a file called "alice.txt". I can redirect my program to read input from the file alice.txt as follows:
$ ./letter_count < alice.txt
133 letters
Here, the "<" tells the system to read input for the program from the file "alice.txt" (instead of waiting for you to enter it followed by CTRL-D).

Copyright © 1999, Ray Ontko (rayo@ontko.com).