Origin of C
The C Programming Language was initially developed by Denis Ritchie
using a Unix system in 1972. This was varied and modified until a standard was
defined by Brian Kernighan and Dennis Ritchie in 1978 in "The C
Programming Language".
By the early 80's many versions of C were available which were
inconsistent with each other in many aspects. This led to a standard being
defined by ANSI in 1983. It is this standard this set of notes primarily
addresses.
Why use C ?
Industry
Presence : Over the last decade C has become one of
the most widely used development languages in the software industry. Its
importance is not entirely derived from its use as a primary development
language but also because of its use as an interface language to some of the
newer “visual” languages and of course because of its relationship with C++.
Middle
Level : Being a Middle level language it combines
elements of high level languages with the functionality of assembly language. C
supports data types and operations on data types in much the same way as higher
level languages as well as allowing direct manipulation of bits, bytes, words
and addresses as is possible with low level languages.
Portability
: With the availability of compilers for almost all
operating systems and hardware platforms it is easy to write code on one system
which can be easily ported to another as long as a few simple guidelines are
followed.
Flexibility
: Supporting its position as the mainstream
development language C can be interfaced readily to other programming
languages.
Malleable
: C, unlike some other languages, offers little
restriction to the programmer with regard to data types -- one type may be
coerced to another type as the situation dictates. However this feature can lead to sloppy coding unless
the programmer is fully aware of what rules are being bent and why.
Speed
: The availability of various optimising compilers
allow extremely efficient code to be generated automatically.
1.2 The “Hello World” Program
A C program consists of one or more
functions or code modules. These are essentially groups of instructions that
are to be executed as a unit in a given order and that can be referenced by a
unique name. Each C program must contain a main() function. This is the first function called when the program
starts to run. Note that while "main" is not a C keyword and hence
not reserved it should be used only in this context.
A C program is traditionally arranged in
the following order but not strictly as a rule.
Function prototypes and global data declarations
|
The main() function
|
Function definitions
|
Consider first a simple C program which
simply prints a line of text to the computer screen. This is traditionally the
first C program you will see and is commonly called the “Hello World” program
for obvious reasons.
#include <stdio.h>
void main()
{
/* This is how comments are implemented in
C
to comment out a block of text
*/
// or like this for a single line comment
printf( "Hello World\n" ) ;
}
As you can see this program consists of
just one function the mandatory main
function. The parentheses, ( ), after the word main indicate a function while
the curly braces, { }, are used to denote a block of code -- in this case the
sequence of instructions that make up the function.
Comments are contained within a /* ... */
pair in the case of a block comment or a double forward slash, //, may be used
to comment out the remains of a single line of test.
The line
printf("Hello World\n " ) ;
is the only C statement in the program and must
be terminated by a semi-colon.
The statement
calls a function called printf which causes its
argument, the string of text within the quotation marks, to be printed to the
screen. The characters \n are not printed as these characters are interpreted
as special characters by the printf function in this case printing out a
newline on the screen. These characters are called escape sequences in C and cause special actions to occur and are
preceded always by the backslash character, \ .
All
C compiler include a library of standard C functions such as printf which allow
the programmer to carry out routine tasks such as I/O, maths operations, etc.
but which are not part of the C language, the compiled C code merely being
provided with the compiler in a standard form.
Header files must be included which contain
prototypes for the standard library
functions and declarations for the various variables or constants needed. These
are normally denoted by a .h extension and are processed automatically by a
program called the Preprocessor prior
to the actual compilation of the C program.
The line
#include <stdio.h>
instructs the preprocessor to include the
file stdio.h into the program before compilation so that the definitions for
the standard input/output functions including printf will be present for the
compiler. The angle braces denote that the compiler should look in the default
“INCLUDE” directory for this file. A pair of double quotes indicate that the
compiler should search in the specified path e.g.
#include “d:\myfile.h”
NB : C is case sensitive i.e. printf() and
Printf() would be regarded as two different functions.
The C Programming Environment
Program development is nowadays carried out
in specifically designed software systems or workbenches with editing,
compilation, linking, debugging and execution facilities built in. In this
course we will be making use of a Microsoft system but the features found in
this are to be found in one form or another in almost all modern systems.
The first phase of development involves the
creation and editing of a file containing the appropriate C instructions which
will be stored using a file extension of .c
normally to invoke the C compiler, e.g. fname.c.
The next step is to take the C program and
to compile it into object code or machine language code. The C compiler
includes the aforementioned preprocessor which is called automatically before
the code translation takes place. This preprocessor acts on special commands or
directives from the programmer to manipulate the text of the C code before
compilation commences. These directives might involve including other source
files in the file to be compiled, replacing special symbols with specific
replacement text, etc. Once this is done the C code is translated into object
code and stored in a file with the extension .obj, e.g. fname.obj.
The final phase in building the executable
program is called linking. After the compilation stage the C code has been
translated into machine recognisable code but is in a somewhat unconnected
state. The program invariably contains references to standard library functions
or functions contained in other libraries or modules which must be connected to
the C program at link time. This simply involves linking the machine code for
these functions with the program’s object code to complete the build process
and produce an executable file with an extension .exe e.g. fname.exe.
The executable program can be loaded and
run from within the programming environment itself or may be run from the host
environment directly. If it executes as expected that is the end of the task.
However if this does not happen it may require the use of the debugger to
isolate any logical problems. The debugger allows us to step through the code
instruction by instruction or up to predefined break-points and to look at the
values of variables in the code in order to establish where errors are
introduced.