Most C programs consist of function definitions and data structures.
Here is a simple C program that defines a single function, called main
.
void main() { printf("Hello, world!\n"); }
All functions must have a return type. Since main
does not
return a value, it uses void
, the null type, as its return type.
Other types include integers (int
) and floating point numbers
(float
). This function declaration information must
precede each function definition.
Immediately following the function declaration is the function's name
(in this case, main
). Next, in parentheses, are any arguments
(or inputs) to the function. main
has none, but a empty set of
parentheses is still required.
After the function arguments is an open curly-brace {. This signifies the start of the actual function code. Curly-braces signify program blocks, or chunks of code.
Next comes a series of C statements. Statements demand that
some action be taken. Our demonstration program has a single
statement, a printf
(formatted print). This will print the
message "Hello, world!
" to the LCD display. The \n
indicates end-of-line.
The printf
statement ends with a semicolon (;). All
C statements must be ended by a semicolon. Beginning C programmers
commonly make the error of omitting the semicolon that is required at
the end of each statement.
The main
function is ended by the close curly-brace {.
Let's look at an another example to learn some more features of C. The following code defines the function square, which returns the mathematical square of a number.
int square(int n) { return(n * n); }
The function is declared as type int
, which means that it will return
an integer value. Next comes the function name square
, followed by
its argument list in parentheses. square
has one argument, n
,
which is an integer. Notice how declaring the type of the argument is done
similarly to declaring the type of the function.
When a function has arguments declared, those argument variables are valid within the "scope" of the function (i.e., they only have meaning within the function's own code). Other functions may use the same variable names independently.
The code for square
is contained within the set of curly braces. In
fact, it consists of a single statement: the return
statement. The
return
statement exits the function and returns the value of the C
expression that follows it (in this case "n * n
").
Expressions are evaluated according set of precendence rules depending on
the various operations within the expression. In this case, there is only
one operation (multiplication), signified by the "*
", so
precedence is not an issue.
Let's look at an example of a function that performs a function call to the
square
program.
float hypotenuse(int a, int b) { float h; h = sqrt((float)(square(a) + square(b))); return(h); }
This code demonstrates several more features of C. First, notice that the
floating point variable h
is defined at the beginning of the
hypotenuse
function. In general, whenever a new program block (indicated
by a set of curly braces) is begun, new local variables may be defined.
The value of h
is set to the result of a call to the sqrt
function. It turns out that sqrt
is a built-in function that takes a
floating point number as its argument.
We want to use the square
function we defined earlier, which returns
its result as an integer. But the sqrt
function requires a floating
point argument. We get around this type incompatibility by coercing
the integer sum (square(a) + square(b))
into a float by preceding it
with the desired type, in parentheses. Thus, the integer sum is made into
a floating point number and passed along to sqrt
.
The hypotenuse
function finishes by returning the value of h
.
This concludes the brief C tutorial.