Sample C Program

Sample C Program - Source Code

Our standard sample count-to-ten program in C:

#include <stdio.h>

/*

    Sample C Program - Counts to Ten

    Copyright 2006 Andrew Eichstaedt
    Eichstaedt Development Group
    http://www.andrew-eichstaedt.com

*/

main() {

    int i;

    printf("Hello! I am a sample C program\n");
    printf("that counts to ten.\n\n");

    for (i = 1; i <= 10; i++) {
        printf("%d\n", i);
    }

    printf("\nThanks for running me.\n");

}

General Notes on C

C is a widely-used, traditional structured programming language that runs on an exceptionally wide variety of platforms. C is a comparatively low-level language, almost a high-level macro assembler. It provides close-to-the-metal control of memory management and other system-level functions. This makes it useful for development of applications requiring maximum possible speed and efficiency.

However, lacking higher-level features like object-orientation and automatic garbage collection, C places a greater burden on the programmer for management of sometimes tricky details. As Bjarne Stroustrup, designer of the language C++, is cited as saying, "C makes it easy to shoot yourself in the foot."

History of C

C was developed initially by Dennis Ritchie and others at AT&T Bell Labs between roughly 1969 and 1973 in conjuction with the development of the Unix kernel. After some additions to the language, Brian Kernighan and Dennis Ritchie published the first edition of The C Programming Language, known to programmers as "K&R" This book served as an unofficial specification for C. An official standard, ANSI C, was introduced in 1989, and is accepted by all C compilers in wide use. An ISO standard with additional language extensions was introduced in 1999 and adopted by ANSI in 2000, but is not nearly as well supported as classic ANSI C.

Compiling and Running the Example C Program

C is a compiled language. On a typical Unix system, source code for a C program will be in a file with the .c extension, and compilation will produce an excecutable file with a .out extension. For example:

$ cc -o sample-c-program.out sample-c-program.c

compiles a program that can be executed with:

$ ./sample-c-program.out

to produce the output:

Hello! I am a sample C program
that counts to ten.

0
1
2
3
4
5
6
7
8
9
10

Thanks for running me.

Sample C Program - Comments for Programmers

A C program typically begins with one or more #include lines like the first one in our example. These direct the preprocessor to process the headers before actual compilation. In this case, stdio.h is the header declaring standard input and output functions.

The next portion of code within /* ... */ is a comment.

A C program has a single main() function that is the initial entry point for the program. Blocks of code for functions or control statements are listed within curly brackets ( { and } ) for function definitions and control statements like if and for.

The next line, int i; is a declaration of the variable i as an integer. Type definitions in C are explicit, though in some cases may be overridden by type casts.

The two printf lines that follow simply print the strings specified, with \n being interpreted as a newline character.

The subsequent for statement loops from one to ten, incrementing the variable i with each step. The first parameter to the for statement, i = 1, sets the value of i to one. The next parameter, i <= 10, is a test. If the boolean expression provided evaluates to true, the code within the block executes. The final parameter is an expression, generally with side effects, that is executed with each iteration of the loop. Here, i++ has the effect of incrementing i by one.

The usage of the printf statement within the for block is a bit more sophisticated than the preceeding printf statements. In general, the first parameter to a printf statements is a format string. In the case of the earlier printf statements, the format string was printed literally (with the exception of the \n character, interpreted as a newline). Subsequent parameters to printf are variables or literals, which are substituted in the format string before printing. In this case, the character combination %d indicates a decimal number, into which the value for i is substituted.

Finally, the last printf statement prints the string specified.

     
- -