Monday 3 January 2011

The C Programming Language (K&R) 01x01—Hello World (p. 9)—Exercises 1-1, 1-2

The book “The C Programming Language” by Brian Kernighan and Dennis Ritchie is a seminal book on the C and C++ computer languages. The book introduced the C language to the world and is referred to as K&R. It’s not a reference manual but a introduction to the language where the writers explain by example. They present code to solve a problem and write about what the code is doing. As they write,

“The only way to learn a new programming language is by writing programs in it.”


I will work my way through the sample code and blog about. I’m using the 2nd edition published in 1988. The language and technology has changed since, but that will be part of the blog. The blog entries will all have the tag K+R. The web site won’t accept the ampersand as a character.

Why do it? There’s no one reason, but several. To learn the language. To show others I know something about the C language. 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. At some point I will try other compilers, but that’s for another day.

When I was studying computer programming in high school and university, we never wrote a Hello World program to show the code of a language. Now it’s standard.

K&R p. 9 Hello World.

Here in a nutshell is their take on what a C program is:

“A C program, whatever its size, consists of functions and variables. A function contains statements that specify the computing operations to be done, and variables store values used during the computation.”


Attempt #1

#include <stdio.h>

main()
{
printf("hello, world\n");
}

The code above won’t compile in VC++ 2010. I get this error message:

error C4430: missing type specifier - int assumed

I know why. The main function doesn’t specify a return type. I can change it to “void main()” or “int main().” Both will work; however, there is continuing debate about this issue. Some say the main function should always return some value and not void. Others say returning void is fine. I won’t discuss it here, but will use a return value and specify a return value in the function.

A critical point on the main function,

Normally you are at liberty to give functions whatever names you like, but “main” is special - your program begins executing at the beginning of main. This means that every program must have a main somewhere.


Attempt #2

// C library
#include <stdio.h>

// added int return type
int main()
{
  printf("hello, world\n");

  // added a return value
  return 0;
}

This attempt works.

Note: I’m using C++ standard for commenting, not C, since it’s easier to use.

C doesn’t have many keywords or many built-in functions or commands. It’s a sparse language. Functionality is added by including libraries with other C code. The statement #include <stdio.h> isn’t C but a compiler directive to load in a library of code called the Standard I/O library. Without that directive, the compiler won’t know what printf is.

The printf function takes its argument and sends it to the standard output (a monitor).

The ‘\n’ is an escape sequence for the newline character (ASCII 13). There are several escape sequences for unprintable characters or to handle quotes and slashes.

\n        newline
\t         tab
\\        back slash
\”        double quote

Attempt #3

In C++ the directive should be: #include <cstdio>

// C library
//#include <stdio.h>
// C++ library
#include <cstdio>

// added int return type
int main()
{
  printf("hello, world\n");

  // added a return value
  return 0;
}


Attempt #4

Add a pause by calling the system function in the standard library. In Windows, the console pops up and cloes too quickly.

// C++ library
#include <cstdio>

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

int main()
{
  printf("hello, world\n");

  // Pause to keep console window open.
  system("pause");

  return 0;
}

Here’s how it looks on my computer.


No comments:

Post a Comment