ECE-1021 Lesson 1

Hello World!

(Last Mod: 27 November 2010 21:38:38 )

ECE-1021 Home



Objectives


Prerequisite Material


Co-requisite Material


The Infamous "Hello World!" Program

It is somewhat traditional that the first program a person writes in C does nothing more than print a simple phrase to the screen - the time honored phrase being, "Hello World!" 

By successfully doing this, the person has demonstrated the ability to write a minimal program including writing a function, gaining access to some of the standard C functions, calling at least one of those functions, and passing data to one or more functions. They then demonstrated that they could do so without error, could successfully complete the process of compiling the source code file to obtain the intermediate level object code files, and could then link those files together to create the final executable code file. They should not be too quick to dismiss this as a trivial accomplishment, not if they are new to C and especially not if they are new to computer programming in general.

While your first program will also follow in the footsteps of this tradition, you will not use the same function that most people use. The typical first program uses the printf() function which is a very powerful function. By beginning with it the new programmer can get up and going much quicker than if they had to work at a much lower level - but they also lose out on the opportunity to learn things that can only be learned by working at that lower level. By putting these things off until later, it is hoped that the person will be better prepared to understand the lower level concepts when they are reached. What frequently happens, however, is that a mental model has build up of how things work and it is usually well at odds with how they really work. When this happens, the new material becomes extremely confusing and a lot of effort is required to breakdown this subconscious mental model before the newer, correct one can take its place.

In an effort to prevent this from happening, we will take a slower starting pace - learning to crawl - and be sure that we understand what it is we are doing, and how to work at that level, before moving on.

The following is a code listing for our version of the hello.c program:

#include <stdio.h>  /* putc(), stdout */


int main(void)
{
    putc('H', stdout);
    putc('e', stdout);
    putc('l', stdout);
    putc('l', stdout);
    putc('o', stdout);
    putc(' ', stdout);
    putc('W', stdout);
    putc('o', stdout);
    putc('r', stdout);
    putc('l', stdout);
    putc('d', stdout);
    putc('!', stdout);
    putc('\n', stdout);

    return 0;
}
 

The above program consists of a function named "main". Throughout the modules, assignments, quizzes, and exams for this course quite a bit of effort is made to use a consistent nomenclature and syntax and the way that functions are indicated is by following them with a pair of parentheses. So the above sentence would generally written as saying that the above program consists of the function main().

Let's now walk through this program line by line and discuss some of the basic elements of a C program.

The first thing we encounter is a #include statement which is one of the preprocessor directives. This statement includes the entire contents of the indicated file as though it were physically included at the same point as the statement. The file included in this particular #include statement gives us access to the Standard Iinput/Output Library <stdio.c>. We will be using the two items indicated in the comment to the right of the #include statement, namely the pre-defined constant stdout and the function putc().

The main() function is declared so that it receives no arguments from the host system - as indicated by the "void" parameter list - and is expected to return a value of type int - as indicated by the "int" preceding the function name. The code for this function is everything in the compound statement that starts with the left-curly-brace immediately after the function header and the matching right-curly brace.

The putc() function allows us to write single characters to an output stream. In this program, all output is sent to the standard output which is the console display (the screen). The function takes two arguments - the first is passed-by-value and the second is passed-by-reference.

The final putc() function call passes the character code '\n' to the function. This is not two characters, it is an "escape sequence" that is translated by the preprocessor into a single character that happens to be one that we can't indicate directly - in this case it is the new line (also known as the Line Feed - or LF) character.


The putc() Function

#include <stdio.h>

int putc(int c, FILE *stream);

The putc() function is one of the standard function that can be accessed by including the header file for the Standard I/O library. The "function prototype" for this function is located above. A function prototype is simply a copy of the function header followed by a semicolon instead of the function body. It's purpose is to inform you of how to communicate with this function. In the case of putc(), it returns a value of type 'int' and we are expected to supply two arguments when we call it. The first is a value of type 'int' (a signed integer value with a specific number of bytes) and the second is a pointer (indicated by the *) to a value of type 'FILE'. A "pointer" is simply the memory location, or address, where the actual data is stored.

If we look up the description of this function, we find that the first value we pass, 'c', is the ASCII code of the character we want printed to a file. The second value that we pass, 'stream', is the address of where information about the file we want to print to can be found. The "C-speak" way of expressing this is to say that 'stream' is a pointer to a FILE object.

The return value of the function is either the value that was printed to the file or, if an error occurred, the function will return a special value defined symbolically as EOF.