C PROGRAMMING EXAMPLES
the program example called ADD.C that we were discussing at the
December meeting. We used the Knoppix 3.3 Linux "live" CD
operating system, wrote it with the Kwrite editor, compiled it with
the gcc compiler on the command line of a console and ran it from the
command line of a console.
int main(int argc, char *argv)
In the example, we used the function main()as
the starting point because all executable C programs must contain a
function called main() to
tell the compiler where to start. If there is no main()then
the program is incomplete and must be part of a program with main()
somewhere else - for example, library functions will not have a
main(). Programs without
a main() will still
compile but won’t run by themselves. We want ours to run.
feature of main() is the
occurs immediately in front of the function name. The “int”
stands for “integer” and tells the compiler that the
function main() will
return an integer to the calling program (the operating system in the
case of main()).
Linux compiler that we are using “int”
is a 32 bit value. Other “types” that are used in C are
character (8 bits), “long”
for 32 bit integer numbers (same as int
for this compiler), “float” for floating point numbers,
and “double” for double precision floating point numbers.
These are the basic types and are used by themselves or to
construct other types. They also have further qualifiers such as
“signed” and “unsigned” or “long”
and “short” so that you could have, say, “short
int” for a 16 bit integer or even "long
long " for a 64 bit integer. The actual interpretation
depends on the particular implementation of the C compiler you are
using. We will look closer at types in future.
talking about functions it should be noted that functions can only
return single values or even none at all (in the case where no value
is returned the type is void).
Some functions only need to return an answer such as some maths
function like squareroot, sqrt().
Other functions may return a single value indicating status such as
an error code. Where more than one value is needed in a result such
as a string or an array then the return value may be a pointer to the
string or array. This takes a bit of getting used to if you are
more familiar with languages that use functions for single value
returns just like C, but use subroutines that can return more than
one value such as Pascal, Fortran, etc. C does not have
worth mentioning that these other languages often “nest”
subroutines ie. they can include the code for subroutines within
subroutines. C does not nest functions within functions. There is
no need – you list all functions separately, but if required
you simply call any other functions from within functions instead,
which effectively does what nesting subroutines does.
features of the main()
statement are the arguments within the round brackets. All
functions must include brackets whether they have actual arguments or
not. ie. the brackets may be empty, but the brackets must still be
example the arguments are returned from the command line.
is the count of parameters returned from the command line that has
the type “int”
is the name of the array of strings that are returned from the
command line. The square brackets combined with the “char”
type indicate that each argv is a string of characters. The asterisk
is the pointer indicator. So we have “argv” as the name
of an array of strings of characters. This introduces pointers and
arrays that I hope will be covered in much more depth in future
meetings. Pointers and arrays can be very complex so don’t be
phased if you don’t follow what’s happening just yet.
It is pointers that make the C and C++ languages so powerful.
worth mentioning that Java looks a lot like C and C++, but without
pointers. Sun and James Gosling who wrote it for Sun advocate Java
over C and C++ because they say, without pointers, Java source or its
intermediate pseudo-code can be easily ported unaltered to different
platforms and different operating systems whereas C and C++ can’t.
Then alternatively C and C++ compiled programs run faster than
almost anything except very tight assembly code and can drive
hardware, usually as part of the operating system or as a driver
while Java can’t. Also, the Java compiler and pseucode
interpreter itself is written in C or C++, so who wins that one?
Well, the answer is “the one that best suits the application at
have a sequence of statements enclosed in curly brackets that form
the main() program.
statement is a print function, or “printf()”, that is one
of the most commonly used and complex functions in the C language.
You can look at its complexity by listing its description with the
look up the function it will show its “prototype” that
describes its type and the parameters it accepts in the round
brackets as well as their type and description.
another important item shown in any functions’ description and
that is the “header” that defines all of the constants,
variables, other functions, in fact, anything that you want to
predefine to avoid having to do so each time you use the function.
The header used by printf()
is stdio.h. It precedes
the function that uses it. Usually all #includes
are listed at the top of a program. In this case the header is a
standard one and is found in the include
directory so it is enclosed in angle brackets < >. If we write
our own header and include it in the working directory it will be
enclosed in double quotes " ".
case the format of the printf()
function’s output is contained in the quotes indicating that we
want to print a decimal number, “%d”,
followed by a newline, “\n”.
decimal value that we will print is the result of the next bit of
code. We are adding the two arguments from the command line.
However, the arguments are ASCII strings (even though they may look
like numbers). The arguments from the command line are indicated as
Because they are strings we convert them to numbers using the ASCII
to integer function “atoi()”
first then add them with “+”. A lot of work in one
line, but that is the sort of brevity (or obtruseness) you can
achieve with C.
function requires the header stdlib.h.
want to take things a bit slower you can substitute the printf()
line with the lines. (BASIC programmers may prefer this method)
a, b, c;
= a + b;
there is the exit()
function. This returns the value within the round brackets. If you
look up the manual using:
in the round brackets is returned to the operating system. In this
case a zero has been chosen to indicate normal program completion.
note that the include header reference for exit()
is stdlib.h that we have
already used for the atoi()
function so we don’t have to include it again.
now compile the program with:
to get an
executable file a.out that
you can run using, say,
in the answer
alternative for compiling the program that gives an executable output
file with another name other than a.out
add.c –o add
version of the program is run using:
in the answer
Here are a
few more things to consider.
there are no arguments in the command line other than the program’s
name? What if there is only one argument following the program’s
name? What if there are more arguments than 2? What if one or more
of the arguments are not integer numbers, but are alpha characters,
or floating point numbers, or are too long to be integers, or are a
mixture of alpha and number characters? You should be doing error
checking. There are a few more things to look at in future.
Here is an
example showing that C++ is really just an extension to the original
the well known “Hello World!” test program used by
everyone to see that most things are operational, but written in C to
start with and called hello.c.
compiling this with the C compiler gcc with the command line
and run it
compiling with the C++ compiler g++
And run it
with ./a.out They should both work.
Now let us
add some C++ code
<< “Hello C++ World” << std::endl;
attempt to compile that with gcc you will get a heap of errors. Try
compiling with g++ instead which should result in no errors, and then
run the result.
two methods in the class std.
Because the std class is used so often, it can be declared with
the “using namespace std;”
statement. The code becomes, by including the namespace statement
and omitting the class parts:
<< “Hello C++ World” << endl;
see from that you can mix C and C++ statements more or less without a
problem provided that you use the g++ compiler and use the correct C
and C++ syntax.
December issue of Australian Personal Computer (APC) have two
electronic books on C++ programming (and other languages as well).
The files are a bit hard to find on the DVD, so look carefully.