Juergan and All:
> Could you send an example file? It's a bit hard to guess what's wrong
> only from your comments :)
I have attached a sample file. On this particular file section 1.2
doesn't print at all. Furthermore it's touch and go whether the headers
print either.
DAVID
--
Subscribe to the Linux C Mailing Lists Today!
mailto:[EMAIL PROTECTED]
http://users.senet.com.au/~lloy0076/linux_c_programming/index.html
#This file was created by <lloy0076> Thu Oct 14 14:36:50 1999
#LyX 1.0 (C) 1995-1999 Matthias Ettrich and the LyX Team
\lyxformat 2.15
\textclass linuxdoc
\language default
\inputencoding default
\fontscheme default
\graphics default
\paperfontsize 10
\spacing single
\papersize a4paper
\paperpackage a4
\use_geometry 0
\use_amsmath 0
\paperorientation portrait
\secnumdepth 3
\tocdepth 3
\paragraph_separation skip
\defskip medskip
\quotes_language english
\quotes_times 2
\papercolumns 1
\papersides 1
\paperpagestyle default
\layout Title
\added_space_top vfill \added_space_bottom 0.3cm
Developing a
\begin_inset Quotes eld
\end_inset
Hello World!
\begin_inset Quotes erd
\end_inset
Application in C with Linux
\layout Author
David Lloyd
\layout Date
14 September 1999
\layout Abstract
ABSTRACT
\layout Abstract
This document aims to introduce readers to the most basic of all programs
- the classical
\begin_inset Quotes eld
\end_inset
Hello World!
\begin_inset Quotes erd
\end_inset
program.
In doing so it will introduce readers to a number of important programming
constructs, concepts and data types.
\layout Standard
\begin_inset LatexCommand \tableofcontents{}
\end_inset
\layout Section
Developing a
\begin_inset Quotes eld
\end_inset
Hello World!
\begin_inset Quotes erd
\end_inset
Application in C with Linux
\layout Subsection
This document's objectives
\layout Standard
This document aims to introduce you to the C programming language, a small
part of its history, some of its structure and short-comings and, of course,
your very first program.
Here is the outcome of that program:
\layout Standard
<localhost root># ./hello
\layout Standard
Hello World!
\layout Standard
<localhost root>#
\layout Standard
Whilst this program is not at all exciting it will introduce you to a number
of important concepts and topics in C.
\layout Subsection
What you will learn
\layout Standard
By the end of this tutorial you will be able to:
\layout Itemize
understand the basis of all C programs
\layout Itemize
understand the concept of standard libraries and C headers
\layout Itemize
explain what a function is and understand its structure/syntax
\layout Itemize
write to standard output using printf
\layout Itemize
exit gracefully from the main function
\layout Itemize
create and compile the program using tools supplied with virtually all linux
distributions
\layout Section
Requirements
\layout Standard
This section shows the hardware and software requirements necessary to develop
your first
\begin_inset Quotes eld
\end_inset
Hello World!
\begin_inset Quotes erd
\end_inset
application.
In addition, it also specifies what the author assumes you already know.
\layout Subsection
Hardware and software requirements
\layout Standard
You will need the following to be able to produce the program outlined in
this document:
\layout Itemize
linux
\layout Itemize
gcc or egcs
\layout Itemize
gmake
\layout Itemize
vi, pico, joe or emacs (any text editor will do)
\layout Standard
The author cannot think of a hard-drive installation of linux which would
lack these tools.
Most users can assume that they all exist on their system.
\layout Subsection
Assumed knowledge
\layout Standard
The following knowledge is assumed:
\layout Itemize
basic knowledge of the linux operating system
\layout Itemize
basic computer knowledge, such as what a computer is, what a program is
\layout Itemize
an understanding of linux file permissions (read, write and execute)
\layout Itemize
how to use the text editor of your choice
\layout Standard
As you have managed to obtain this document, the author feels that these
are reasonable assumptions.
\layout Subsection
What do I do if....
\layout Standard
Please go to this address: http://www.linux.org.
This is the American Linux User Groups' home pages.
There are sections for new users of linux through to experienced users
of linux.
Read the many and varied FAQs.
Consider joining a local linux user group's mailing list to ask more detailed
questions.
\layout Section
An Overview of C
\layout Standard
This section gives a brief overview of the C programming language.
First it gives a short history of the language, a brief description of
the language and a brief discussion of C's shortcomings.
\layout Subsection
History of C
\layout Standard
In 1965, the MIT had sponsored a project codenamed Project MAC.
Co-funded by General Electric and Bell Labs, two talented programmers Thompson
and Ritchie began to produce an operating system codenamed Multics.
As time went by, though, Bell Labs found the project too expensive and
withdrew their support for Multics.
Thompson and Ritchie, who by then had found their still-developing operating
system already an excellent programming environment, led an informal group
of Bell Labs employees in making a new operating system called Unix
\begin_float footnote
\layout Standard
Unix is, of course, a play on the word Multics
\end_float
.
\layout Standard
At the time two languages existed which might have been able to code the
new operating system - PL/I and BCPL.
Hampered by insufficient hardware - Ritchie's original computer only had
8kB of RAM - PL/I was simply out of the question.
Thompson had some experience with BCPL, however he felt it lacked a number
of important features necessary for the new operating system and, above
all, he thought it too slow.
\layout Standard
Hence, the programming language B
\begin_float footnote
\layout Standard
Some say that B is simply a contraction of BCPL; others say that it is a
contraction of a programming language Thompson had developed many years
before and named after his wife Bonnie - hence the B.
\end_float
came into existence.
This language could squeeze into 8kB of RAM, run on a very slow PDP-11
and served its purpose.
Nonetheless, many programmers felt the need for improvements.
These were: enhanced pointer
\begin_float footnote
\layout Standard
Each memory location has a very specific address; put simply a pointer is
a variable (storage space) which contains the address of a particular portion
of memory
\end_float
handling, stricter type
\begin_float footnote
\layout Standard
A data type, here contracted just to type, means the form of data being
dealt with by the computer.
For example, an integer is a data type that stores only whole number.
A float is a data type that stores numbers in scientific x.xx * 10^y notation,
hence can contain both decimals and very large numbers.
\end_float
capabilities and faster output code.
\layout Standard
During the years 1971 Ritchie and others worked on this newly improved language.
In 1978 the book
\emph on
The C Programming Language
\emph default
by Kernigan and Ritchie was released and a new era of C programming had
begun.
In 1989, the American National Standards Institute ANSI released a definition
of the C programming language.
ANSI C is still the accepted standard for C worldwide.
\layout Subsection
A brief description of C
\layout Standard
This section gives a brief overview of C.
The first section will briefly outline the uses to which you can put C.
The second subsection will introduce you to a number of the standardised
packages which come with virtually every distribution of C.
The last subsection will outline the concepts you will learn by studying
\begin_inset Quotes eld
\end_inset
Hello World!
\begin_inset Quotes erd
\end_inset
in C.
\layout Subsubsection
The Uses of C
\layout Standard
C is a general purpose programming language.
However it is not what is termed a high-level programming language.
A high level language abstracts the user from the inner workings of the
computer, ensures strict type checking and standardises many common functions.
C does not offer a large range of inbuilt functions and types of data,
nor does it perform strict data type checking.
Type checking occurs when a compiler checks to see that you have passed
the correct type of data (eg a number) to a particular part of a program.
Errors can easily occur if you pass a character to part of a program expecting
a number for example.
\layout Standard
On the other hand, C is not a low-level language.
It's certainly much more abstract than machine or assembly language, yet
a well written and compiled C program can execute as quickly as a reasonably
written piece of assembly code.
C allows you to access the operating system at a lower level than many
other modern languages.
This gives C a lot of power but it also means you have to be more careful
when writing a C program.
\layout Standard
C tends to assume that you know what you are doing, hence it is relatively
easy to make an unexpected segmentation fault and core dump using C than
a heavily typed language such as Pascal.
\layout Standard
Because of this flexibility C can be put to almost any use imaginable.
It makes an excellent language to program device drivers and linux kernel
modules.
Both of these applications require you to have a degree of access to the
operating system and speed that other higher level languages simply don't
ofer.
It is also a language well suited to event driven programming.
The GIMP and its toolkit, Gtk+, are both written in C; although there have
been ports to Ada, Pascal and even C++, C is still the language of choice
for many Gtk+ programmers.
\layout Standard
Finally, although these libraries are not built-in to the C definition itself,
C programmers world wide have accepted a number of standard libraries for
use when programming C.
You will find out about some of them in the next subsection.
\layout Subsubsection
Standard libraries
\layout Standard
Standard libraries are not part of the C programming language itself.
They are useful libraries which contain functions (actions) which most
C programs need to do regularly.
ANSI does comment on the standard C libraries.
Here is a very incomplete list of standard libraries and a brief description
of the type of functions they contain:
\layout Description
stdio.h This is the standard input/output library.
It contains functions necessary to manipulate console input and output,
streams, file input and output and a number of other low-level i/o functions.
Most C programs require this library.
\layout Description
stdlib.h This library is the standard library and essentially contains functions
that don't sensibly fit into stdio.h.
It contains a number of type-conversion functions, memory allocation functions
and other useful functions
\layout Description
math.h As its name suggests this standard library contains a number of mathematic
al functions, such as pow (raise to the power), cos (cosine).
\layout Description
malloc.h Named after its most infamous function, malloc (memory allocate),
this library contains functions used to allocate, deallocate and alter
chunks of memory.
As a byside, the allocation/deallocation of memory is what causes many
C programs to crash!
\layout Description
string.h C is not known for its natural string handling capabilities.
This library provides the bare amount of funcitons to manipulate and use
strings of characters.
\layout Subsubsection
Concepts you will learn by studying
\begin_inset Quotes eld
\end_inset
Hello World!
\begin_inset Quotes erd
\end_inset
in C
\layout Standard
This tutorial introduces the following concepts:
\layout Itemize
basic program structure
\layout Itemize
the preprocessor and #include directive
\layout Itemize
functions and arguments
\layout Itemize
the main function
\layout Itemize
the standard input/output libraray (stdio.h)
\layout Itemize
printf
\layout Itemize
return
\layout Itemize
compilation of "Hello World!" in C with the use of a basic Makefile and
gcc under a linux operating system
\begin_float footnote
\layout Standard
As this particular program conforms to the ANSI C standard, it should compile
and run under many different platforms, linux being one
\end_float
\layout Subsection
Shortcomings of C
\layout Standard
C has a number of shortcomings.
None of these shortcomings are fatal to your use of C and many of them
are quirks of the language itself.
Nonetheless, it's worth being aware of them.
Here are some of them listed in no particular order.
The author will leave it as an exercise for you to work out how they might
make C more difficult, less robust or simply a pain to program with:
\layout Itemize
C does not perform strong type checking
\layout Itemize
C's handling of arrays, pointers and especially arrays of characters is
at first counter-intuitive
\layout Itemize
C does not check to see if you're using memory you shouldn't be; you are
responsible for not using more memory than you've asked for and deallocating
unused memory
\layout Itemize
It is not easy to reuse modules using C - it is possible but C is not an
Object Oriented Programming Language
\layout Itemize
The * and & operators (dereference and address-of) are, to put it mildly,
fantastically confusing; mistakes with one or both of these even catch
out seasoned programmers
\layout Itemize
No group has come up with a standard way to write an event-driven windows
program; this isn't a shortcoming of C per se however it is one reason
(the author believes) that Java has become so popular
\layout Standard
Nonetheless, the flexibility and speed of C have made it one of the world's
most successful programming languages.
There are many C programmers in the world and most programmers will help
other programmers if they research and try to fix their own problems first.
C is a rich, mature language that for all of its shortcomings is still
very powerful.
\layout Section
The
\begin_inset Quotes eld
\end_inset
Hello World!
\begin_inset Quotes erd
\end_inset
Application - QuickStart
\layout Standard
This section will show you without much explanation how to write and make
the
\begin_inset Quotes eld
\end_inset
Hello World!
\begin_inset Quotes erd
\end_inset
application.
As such, don't expect to understand everything that is written.
Simply do as the instructions say and you should be able to get a working
program.
This section assumes that you are using linux.
\layout Subsection
A quick and dirty start...
\layout Standard
For a very quick and dirty start, simply type in the following program using
your text editor (such as vi or emacs):
\layout Quote
#include <stdio.h>
\layout Quote
int main(int argc, char *argv[]) {
\begin_deeper
\layout Quote
printf(
\begin_inset Quotes eld
\end_inset
Hello World!
\backslash
n
\begin_inset Quotes erd
\end_inset
);
\end_deeper
\layout Quote
return 0;
\layout Quote
}
\layout Standard
Save this as HelloWorld.c.
Then execute this command:
\layout Quote
<~>$ gcc -o HelloWorld HelloWorld.c
\layout Standard
After this you should be able to do this:
\layout Quote
<~>$ ./HelloWorld
\layout Quote
Hello World!
\layout Quote
<~>$
\layout Standard
Please note that case is important in C and the linux operating system.
The next section will explain
\begin_inset Quotes eld
\end_inset
HelloWorld!
\begin_inset Quotes erd
\end_inset
in more detail.
\layout Section
\begin_inset Quotes eld
\end_inset
HelloWorld!
\begin_inset Quotes erd
\end_inset
- The Detail
\layout Standard
This section will explain the programming concepts shown in our
\begin_inset Quotes eld
\end_inset
HelloWorld!
\begin_inset Quotes erd
\end_inset
application.
We wil divide it into three broad sections: theory, the C programming structure
s and syntax, and the GNU C Compiler GCC.
To make this section easier you might like to print the HelloWorld.c application
you developed above.
For that matter, it's not large so you could also copy it by hand!
\layout Subsection
Theory
\layout Standard
A program is a set of step-by-step instructions which explicitly tell the
computer exactly what to do.
To help us program computers various people or organisations have invented
computer programming languages such as C and Pascal.
Programming languages have to broad divisions;: low-level and high-level
programming languages.
Most programming languages lie somewhere between these divisions.
C is closer to a low-level language than Pascal.
\layout Standard
Computer only understand machine language.
We can represent this on screen in two formats:
\layout Itemize
The binary format, eg 100101011111011010110111101101
\layout Itemize
The assembly format, eg add ax, bx
\layout Standard
As you can see the binary format is very difficult for humans to understand.
Although the assembly format is a little easier you will find later that
it too is too low-level for ease of use.
We can now give a very broad definition of low level language:
\layout Description
low-level A low level programming language provides little or no abstraction
from the inner workings of the computer.
\layout Standard
People have invented various higher level programming languages to help
them write programs more efficiently and with less errors
\begin_float footnote
\layout Standard
Imagine trying to find the misplaced 1 in a machne language program consisting
of 1000's of ones and zeros!
\end_float
.
Different high-level languages offer differing features and abilities:
Fortran is quite strong in mathematics, COBOL is more suited to a business
application and C, although general purpose, excels in writing operating
system type software.
Here is a reasonable definition of a high level.
\layout Description
high-level A high level programming language provides a certain degree of
abstraction from the inner workings of the computer.
\layout Standard
Nonetheless, the computer still can only understand machine language.
In order to run a program written in a higher level language you need to
translate your program into machine language.
Although you could do this by hand, it is best done by using either an
interpreter or a compiler.
\layout Standard
An interpreter reads every line of your program in sequence, translates
it on the fly into machine language and executes it.
If the first line is repeated a number of times, for example, it is read
a number of times and translated that number of times.
There are two primary advantages of an interpreter: they are often interactive
so you can see your results immediately and they involve less work to get
working once you have written your program.
There are two primary disadvantages of an interpreter: they're slower than
a compiled program and you need a copy of the interpreter to run them.
Two examples of interpreted languages are BASIC and perl.
\layout Standard
Our definition of an interpreter could be:
\layout Description
interpreter A tool which reads a program in sequence and translates its
instructions into machine language as it goes
\layout Standard
On the other hand a compiler reads the program once, translating it into
machine which you then execute.
A compiler produces what is known as an executable file.
Your operating system will be able to read this file into memory and execute
it without the need for an interpreter to assist it.
The primary advantage of a compiler is speed.
A compiled program almost always executes more quickly than an interpreted
program.
The main disadvantage is complexity; compiling doesn't always allow one
to see one's results immediately and there are a number of intermediate
stages through which one must go to produce a comp8led program.
Pascal, C and Ada are all compiled programs.
\layout Standard
Our definition of a compiler could be:
\layout Description
compiler A tool which reads a program in sequence and translates it into
a form which the operating system can execute directly
\layout Standard
\the_end