Wednesday, May 12, 2010

My Hello World

As I have mentioned in my previous post, I have not liked the way most C or C++ books begin. They start with a program that prints hello world and attempts to create enough enthusiasm among the readers to lure them to continue reading the fat book. This approach puts undue stress on experiencing the output without really speaking anything about computers or the C/C++ programming language. Moreover, the first program that the beginners write is so difficult and tricky that few will understand its true genius even after completing the entire book.

If I teach C, I want to start off differently. I want to include the least number of constructs in the first program. My first program will be an infinite while loop. It will look like this.

void main()
{
    while(1);
}

Now that is a beauty. I would tell that all C/C++ programs had one (and only one) main function. The main is the entry point into the program. The CPU will start executing the program from this point. The first word of the program represents what kind of output this program will produce. It is known as a return value. “void” means nothing. So we are declaring that this program will not produce any output. Some implementations of the C language may require that the main function produce some output. For such implementations we would replace the “void” with “int” (for integer value) and add one more statement just after the while(1); - return 0;

The round brackets after main have a special purpose. We will talk about those later. There are curly brackets after this. They define the scope. Think of a closed box with its name stamped on its cover and the kind of value (integer / void) that comes out of it, stamped before the name. Then, the name will be “main” the kind of value is the int or void written before main. The periphery of the box then, are the curly brackets.

Whatever is inside the curly brackets will be executed by the CPU, one statement at a time. In the simplistic demo program, we have a single statement that says while(1); The word while is different from the word main. "while” is a keyword. There are only a few number of keywords in C/C++ and they have a specific meaning. The “while” keyword is a looping construct. It asks the CPU to execute whatever comes after “while” for as many times as required until the condition inside the () becomes false. Of course, it may never become false at all in which case the CPU will keep executing the instructions ad infinitum.

True and False are tricky in conditional checks in C. Anything that evaluates to 0 is considered false and all non-zero values are considered true. Hence, the condition inside the round bracket will always evaluate to true (since it is non-zero) and the loop will continue forever. But there is nothing inside the loop. The loop boundary could have been defined by the same scope defining {} as for “main”. Instead, in their place, we have a semicolon (;). Hence no statements are to be executed inside the loop. All the CPU does is keep checking the condition, over and over again. Really, the computer is a dumb box.

Your first program does not produce any output. It causes the computer to hang! The condition inside the while loop () can be a more complex one. It could be a comparison such as 5>2, which would always evaluate to true or another such as 1!=1, which stands for one not equal to one which always evaluate to false. The C language supports many operators and we will explain then as and when we use them.

No comments: