next up previous
Next: Word Counting Up: Character Input and Output Previous: Character Counting

Line Counting

#include <stdio.h>

/* count lines in input */
main()
{
  int c, nl;

  nl = 0;
  while ((c = getchar()) != EOF)
    if (c == '\n')
      ++nl;
  printf("%d\n", nl);
}
$\bullet$
Concepts: Equality operator; character constants


Derek C. Richardson 2005-02-03