Program Structure

The best way to start learning a programming language is by writing a program, which is exactly what we did in the page "Getting started with Dev-C++ ". The code we wrote, once compiled, produced a program that simply printed a message on the screen. Here is the code once more:

// A first C Program.
#include <stdio.h>

void main()
{
  char str[1];
  printf("Greetings, Maestro!");
  printf("\n\nPress ENTER to continue...");
  gets(str);
}

This program prints the message "Greetings, Maestro!" on the screen. It is a very simple program, but it includes the structural elements found in all C programs. Let's take a look at each of those elements in turn, and see what they actually do.



Comments

// A first C Program.

This is a comment line, and is used to convey information about the program to someone reading the code. All lines beginning with two forward slashes (//) are comments, and have no effect whatsoever on the execution of the program. Comments are a convenient method of providing documentation within the program itself. The form of comment shown above is often used if the comment will only occupy one line in the program. Note that block comments can also be included in a program and should be enclosed using a combination of the forward slash and asterisk characters as shown below:

/*
This is a block comment.
It can occupy several lines.
*/

We can even create a rather elaborately framed comment, as shown below. You may feel that this kind of thing is totally unnecessary, and that the programmer has too much time on his or her hands. On the other hand, you could argue that it draws attention to an important comment that might otherwise be overlooked. How to write comments is to some extrent a matter of personal choice - institutional constraints and conventions notwithstanding, of course.

/***********************************
* This is a somewhat more          *
* contrived block comment.         *
* It boxes the text up nicely,     *
* but could conceivably be seen as *
* being somewhat superfluous.      *
***********************************/



Preprocessor directives

#include <stdio.h>

Lines beginning with the hash sign (#) are called preprocessor directives, and are used to provide information to the compiler. In this case they instruct the compiler to include the stdio library header file. Header files include declarations for functions that will be used by the program.



The main() function

void main()
{

  . . .

}

The first line of this construct declares the program's main() function, and indicates the point at which program execution actually begins. All C programs must have a main() function. Other functions outside of the main() function, whether they appear before or after it, are called from within main(). The keyword void appearing before the word main indicates that the program will not return any value to whatever process called it.

In some instances, it may be desirable to return a value to indicate the exit status of the program (e.g. success or failure). The parentheses following the word main are required because this is a function declaration. The parentheses in any function declaration may be used to enclose any parameters (or arguments) that may be passed to the function (if included for the main() function, they allow the program to be started from the command line with one or more command-line arguments). Even if a function does not require any parameters, the parentheses must always be included. The code that constitutes the body of the function is enclosed between the curly braces({}).



Program statements

char str[1];

This program statement declares a variable called str as an array of type char. Note that, unlike some other programming languages (including C++), C does not have a string type as such. A C program statement is a high level, English-like command that causes one or more low level machine code instructions to be executed by the computer's processor. Notice that statements are always terminated with a semicolon.

printf("Greetings, Maestro!");

This statement causes the text "Greetings, Maestro!" to be sent to the standard output stream (usually the screen). The printf function is declared in the stdio library header file, which is why it needed to be specifically referenced by the #include <stdio> pre-processor directive.

printf("\n\nPress ENTER to continue...");

This statement outputs the text "Press ENTER to continue..." to the screen. Note the use of the escape characters ("\n\n") which output two newline characters, each of which moves the cursor down the screen by one line and sets it at the leftmost position.

gets(str);

This statement calls the function gets(), which is also declared in the stdio header file. The getls() function gets a line of text from the user. The argument str indicates the name of the string variable where the string data will be stored (in this case we simply expect the user to hit the ENTER key).


The output of your first C program

The output of your first C program

In this simple program, there is one block of code. A block of code begins with the opening brace ({) and ends with the closing brace (}). Braces are used to enclose code blocks associated with loops, functions, conditional statements, and other programming constructs which we will be discussing later. Notice that the program statements inside the main block of code are indented. Statements can be nested inside other statements, and the convention is to indent each level of code to a column position that reflects how deeply it is nested, simply to make the program code more readable. The indentation is completely ignored by the compiler.