Sunday 9 January 2011

The C Programming Language (K&R) 01x07—Count Characters (p. 19)

I’m making progress as I go through the book “The C Programming Language” by Brian Kernighan and Dennis Ritchie aka K&R.

The reasons I’m doing it? To learn the language. To show others I know something about the C. To create notes for future reference where, on a blog, I can easily find an answer on something I did previously. To practice my writing of documentation and in general.

I am using Visual C++ 2010 and creating the code as a console application.

I keep hearing about gcc as a compiler. I did some quick research. It is an open source compiler written in C & C++. It handles many platforms, many types of CPUs. GCC stands for GNU Compiler Collection and GNU is part of the open source project. While GCC was written first to compile C code, it has since been expanded to cover: C++, Objective C/C++, Java, Fortran, Go and Ada. It’s been a long time since I did any coding in Fortran. I thought it was left for dead.

K&R p. 19, Count Characters

The next part of the K&R book deals with counting characters from the standard input. Counting characters is an easy task since there is no testing done. As long as a character is passed to the program, increment the counter nc.

Version #1

// The standard library includes the system function.
#include <cstdlib>

// C++ standard I/O library
#include <cstdio>

/* count characters in input; 1st version */

int main()
{
     long nc;
     nc = 0;

     while (getchar() != EOF) {
          ++nc;
     }

     printf("%ld\n", nc);

     // keep console window open
     system("pause");

     // return some value
     return 0;
}

When I ran the program, I typed in 12345, enter, <ctrl>+Z, enter. The program returns a count of 6. That’s 5 for each digit and 1 for the enter or new line character.

Here’s the console window:


Version #2

When it comes to coding, there’s always different ways to achieve the same result. The book presents an alternative method of counting the characters using a for statement that has no body. Here’s their words on it,
 
The body of this for loop is empty, because all the work is done in the test and increment parts. But the grammatical rules of C require that a for statement have a body. The isolated semicolon, called a null statement, is there to satisfy that requirement. We put it on a separate line to make it visible.

If you run the program with the same input as the previous version, you should get the exact same output.

// The standard library includes the system function.
#include <cstdlib>

// C++ standard I/O library
#include <cstdio>

/* count characters in input; 2nd version */

int main()
{
     double nc;

     for (nc = 0; getchar() != EOF; ++nc)
           ; // null statement

     printf("%.0f\n", nc);

     return 0;
     // keep console window open
     system("pause");

     // return some value
     return 0;
}

In the second version, the counter variable went from being an integer to a double. The printf function was adjusted accordingly. “%1d” for integer. “%.0f” for floating point.

Variation.

It’s possible to declare a variable in a for statement such as:

for (double nc = 0; getchar() != EOF; ++nc)

The problem with this approach is when it comes time to print the counter value, the variable has lost scope and can’t be accessed outside the for loop. You will get this compile error:

error C2065: 'nc' : undeclared identifier

No comments:

Post a Comment