CONTENTS OF A C PROGRAM
Here's the world's simplest C program that actually performs an action:
#include <stdio.h>
int main()
{
printf("Hello, world!\n");
return 0;
}
This code illustrates many important elements about the C family of languages. The following section looks at the C program in detail.
Programs that do nothing more than display the words "Hello, world" are famous exercises for learning any language. It, or something similar to it, is usually the first task most programmers try to do with a new language they learn.
Use a Library
Libraries are essential to getting anything done in C. At the start of a C
program, you have to list the libraries that you want to use. The line
#include <stdio.h> tells the compiler that you're going to
use the standard IO library. Libraries in C and C++ are organized by means of
header files that are typically named by some sort of acronym for what they
do. In this case, stdio for stands for STandarD Input/Output. By
long standing convention, header file names end in .h -- therefore,
stdio.h .
A header file contains information on how the various functions in a library are named and the parameters they take. Header files are separate from the actual library code they describe, so the programmer must be careful to ensure that the header files match the library that is being used.
Program Conventions
By convention, C programs are written with a function named main
that's executed when the program runs. Also by convention, this function must
return an integer value used to indicate whether any errors are encountered.
C uses int to indicate an integer number. The opening curly
brace { starts the content of the main function,
and the closing curly brace } shows the end of the function. The
C family of languages uses pairs of curly braces to enclose logical blocks of
code statements, just as you saw with the JavaScript function in the previous
chapter.
Also by convention, each complete code statement is terminated by a
semicolon. The statement printf("Hello, world!\n"); is a call to
the printf function in the stdio library with a
string constant. The return 0; statement declares that the value
returned by the main function is zero, which is the convention for not having
any error.
C Variable Types
In contrast with typical scripting languages, C family programming languages expect variables to be declared with a type that indicates the kind of data the variable holds. Having this knowledge in advance lets the compiler create more efficient code. Unfortunately, while Java has exact definitions about variables, some important points about type declaration in C and C++ vary between platforms.
One reason for the difference is that C has been used for many different sized computers and never got standardized in areas related to computer hardware limitations. Table 4-1 summarizes the points that cause the most trouble:
| Type | Abbreviation | Size in C/C++ | Size in Java |
| character | char | 8 bit or 16 bit | always 16 bit |
| integer | int | 16 bit or 32 bit | always 32 bit |
Table 4-1: Some differences in variable types among C family languages.
Characters in Java always use 16 bits because Java attempts to provide for the character sets used in writing a large fraction of the languages in use in the world today. When C got started, practically all programming was done with the small character set used in English. Now programmers have to worry about I18N, so more bits are needed to represent more different characters.
C Arithmetic Expressions
If you remember your algebra, C family arithmetic expressions will look very familiar. C uses parentheses to enforce the order of evaluation as in the following:
a = ( b + c ) * ( d + e ) ;
In words, that statement says multiply the sum of b and
c and by the sum of d and e , storing
the result in a . C also assigns relative precedence to
arithmetic operators so that if an expression is ambiguous, such as a +
b * c , the multiplication is performed before the addition.
C also has some convenience notations that will not be familiar from algebra.
For example, you can attach the symbols ++ or -- to
a variable such as the following:
a = ++b ; // called pre-increment a = b++ ; // called post-increment
In the first statement, the computer adds 1 to the current b
variable; then puts that incremented value and in the a
variable. In the second statement, the current value of b is
taken and assigned to a ; then the computer adds 1 to
b . There is a similar notation, called pre- and post-decrement,
which, as its name indicates, subtracts values.
You'll frequently see the post-increment notation used in conjunction with
arrays to automatically increment the array index. For example, suppose you
want to fill an array called buffer with the successive values
returned from a function called readData , and that
readData is supposed to return a value of -1 to
signal that the last data has been read. The following code looks plausible;
the index n starts at zero and is incremented with every cycle:
int n = 0 ;
while( (ch = readData() != -1 ) {
buffer[ n++ ] = ch ;
}
That would work as long as the buffer array was created large enough to hold
the data. However, you have made no provision for detecting that
n has reached the end of the array. In C, what happens then is
that the data gets stuck in the middle of whatever happens to follow the
array in memory. This is called buffer overflow
. It's the cause of many mysterious bugs in C and
C++ programs, and is exploited by virus writers.
Getting a C/C++ programming environment set up and compiling even simple programs is way beyond the scope of this course. Two widely recommended books for learning the basics of C or C++ are Ivor Horton's Beginning C++ by Ivan Horton and Schaum's Outline of Programming with C++ by John R. Hubbard.
