Introduction to C Programming

Introduction

Chapter 1 section 1

By time-honoured convention the first C program anybody writes is known as the "hello world" program. It is shown below.

main()
{
	printf("hello, world\n");
}

When this program is compiled and run the effect is to display

hello, world

on the screen.


Basics of Programming

Chapter 1 section 2

In order to run the program you need to go through several stages. These are

If you make a mistake during step 1 then it is likely that the computer will not be able to translate the program so you will never reach step 3.

The text of a program is known as the source of the program. The translation of the program is known as compilation and is performed by a special program known as a compiler. The result of the translation or compilation is the executable or binary version of the program.

The program source is kept in a source file. You may use whatever method you prefer to enter and modify the text in the source file. Usually this is done using some sort of text editor program. The use of the word-processing software often found on PCs can cause problems as such software often incorporates extra codes for formatting and laying out text in the text file. Such extra codes will not be understood by the compiler and will cause the compilation to fail.

If you are going to do a lot of programming it is well worth the effort to learn an editor designed for use by programmers. vi and emacs are two such editors frequently found on Unix based computer systems and not uncommon on PCs.

If you are going to store the source in a file for future alteration and development then you will need to name the file in accordance with conventions understood by your compiler. Practically all C compilers expect program source to be provided in files with names ending in .c . C compilers also make use of files with names ending in .h .

The compiler is a large complex program that is responsible for translating the source program into a form that can be executed directly by the computer. Compilers are either supplied with the rest of the standard programs provided on a computer system or they may be purchased separately. There are even some public domain (i.e. free) compilers.

The process of compilation involves the compiler breaking down the source program in accordance with the published rules for the programming language being used and generating machine code that will cause the computer to execute the programmer's intentions. Machine code is a sequence of 1s and 0s that control the flow of data between the various internal parts of the computer. This is often called the binary or executable version of the program.

Once a program has been compiled or translated into machine code the code may be executed directly or may be saved in a file for future use. If it is saved in a file then the compiled program may be run at any time by telling the host operating system (Unix, MSDOS etc.,) the name of the file and the fact that it should copy the program from the file into the computer memory and then start the program running.

Once you have prepared the source file you are ready to to try to compile the program. Supposing that you were working on the hello world program then you might well put the source in a file called hw.c . What you actually type to compile the program depends on the particular compiler and operating system you are using.

Under Unix systems, it is almost always

cc hw.c

You may sometimes use gcc or acc . Assuming you don't get any error messages you will now find a file called a.out has appeared in the current directory. This contains the executable version of your program and it can now be executed by typing the command

a.out

If you are familiar with Unix you can change the name of the file to hw by typing

mv a.out hw

or you can type

cc -o hw hw.c

to compile the program. Unix C compilers often have many options, consult the manual for details.

If you are operating under MSDOS you will need to check your compiler manual and, possibly, consult a local expert to find out what command to type. It may be something like

CL HW.C

or

BCC HW.C

Most MSDOS C compilers will then generate an executable file called HW.EXE which can be executed by typing

HW.EXE

Unix C compilers do not write any messages unless there are errors in your program. If the compilation is successful the next thing you will see is the next operating system prompt. MSDOS compilers often generate intermediate messages concerning libraries and code phases, these are only of interest to more advanced programmers.

It is usual to talk about a C program being executed, this means that the compiled version of the C program is executed. Similarly when talking about how a particular piece of C code is executed we are actually referring to the execution of the machine code generated by compilation of the piece of C code in question.

The "hello world" Program

Chapter 1 section 3

You may be wondering about the need for all the peculiar symbols in the "hello world" program. They are all essential as can be demonstrated by leaving some of them out, the consequences of such errors are discussed in more detail later. First, however, let's take another look at the hello world program.

main()
{
	printf("hello, world\n");
}

This program, in fact, consists of a single piece or chunk of executable code known as a function . Later on we will see programs consisting of many functions. All C programs MUST include a function with the name main , execution of C programs always starts with the execution of the function main , if it is missing the program cannot run and most compilers will not be able to finish the translation process properly without a function called main .

It is important to note that the required function is called main not MAIN . In the C programming language the case of letters is always significant unlike many older programming languages.

In the simple hello world program main appears on line 1. This is not essential, program execution starts at main not at the first line of the source. However, it is conventional in C programs, to put main at, or near, the start of the program.

The round brackets, or parentheses as they are known in computer jargon, on line 1 are essential to tell the compiler that when we wrote main we were introducing or defining a function rather than something else. You might expect the compiler to work out this sort of thing for itself but it does make the compiler writer's task much easier if this extra clue is available to help in the translation of the program.

On lines 2 and 4 you will see curly brackets or braces. These serve to enclose the body of the function main . They must be present in matched pairs. It is nice, but not essential, to line them up in the fashion shown here.

Line 3 is the meat of this simple program. The word printf clearly has something to do with printing. Here, of course, it is actually causing something to be displayed to the screen, but the word print has been used in this context since the time before screens were generally used for computer output. The word printf was probably derived from print formatted but that doesn't matter very much, all you need to know is that this is the way to get something onto the screen.

In computer jargon printf is a library function. This means that the actual instructions for displaying on the screen are copied into your program from a library of already compiled useful functions. This process of putting together a program from a collection of already compiled bits and pieces is known as linking and is often done by a linker or loader program as distinct from a compiler. Most compilation systems, sensibly, hide this complication from the user but you will see references to linkage or linker errors from time to time. More advanced programmers can build their own private libraries of useful things.

Line 3 is a statement. A statement tells the computer to do something. In computer jargon we say that a statement is executed. This simple example consists of the use of a library function to do something. In the C programming language a simple use of a function (library or otherwise) is, technically, an expression, i.e. something that has a value. In computer jargon we say that an expression is evaluated. To convert an expression into a statement a semi-colon must follow the expression, this will be seen at the end of line 3. The actual value of the expression is of no interest in this case and no use is made of it.

A C function normally consists of a sequence of statements that are executed in the order in which they are written. Each statement must, of course, be correctly written before the sequence is acceptable to the compiler. The following version of the "hello world" program shows the use of multiple statements.

main()
{
	printf("hello, ");
	printf("world");
	printf("\n");
}
Note that each one of the three statements is a printf expression converted to a statement by a terminating semi-colon.

Officially the purpose of printf is to write things to the standard output or screen. The list of things to be printed out are enclosed within parentheses immediately after the word printf. They are known as the function parameters or arguments. We sometimes say that the parameters are passed to the function. Incidentally when talking about any function, a C programmer would refer to it, in writing, by its name with the parentheses. We shall adopt this convention from now on.

In the hello world program printf() has only got a single parameter, this is the sequence of characters

hello, world\n

This sequence is identified as a single parameter by enclosing it in double quotes. Such a sequence of characters enclosed in double quotes is known as a string. Strings are discussed in more detail in the next section.

Writing strings

Chapter 1 section 4

In the previous section we have used the library function printf() with a single string as parameter. This appeared as

printf("hello world\n");

The double quote character used to enclose strings should not be confused with a pair of single quote characters.

If you have followed what has been said so far you may well be wondering why the character \n was included in the string and why it didn't appear in the output. The answer is that this pair of characters is converted by the compiler into a single new-line character that is stored as part of the string. Actual new-line characters in the source of the program are treated as if they were space characters by the compiler, so the escape convention is necessary to get a new-line character into the string.

The program example used earlier to illustrate multiple statements also illustrates the significance of the \n character in the strings. If the \n were left off the end of the last line of program output then the effect would be that the next operating system prompt would appear on the same line as the output of the program.

You can include as many \n's as you like within a string enabling multi-line output to be produced with a single use of the printf() function. Here's an example.

main()
{
	printf("This is an\nexample of\nmulti-line output\n");
}
When the program was compiled and run it produced the following output.
This is an
example of
multi-line output

It would not be possible to include an actual new-line character in the string in the program source because this would "mess-up" the source and the compiler would not be able to translate the program properly.

However if the string is too long to fit comfortably on a single line of the source listing of program then it is possible to spread a string over several lines by escaping the actual new-line character at the end of a line by preceding it with a backslash. The string may then be continued on the next line as the following example shows.

main()
{
	printf("hello, \
world\n");
}
Note that there are no characters after the backslash on the third line and also note that the continuation string must start at the very start of the next line. The following nicely laid out version of the above program.
main()
{
	printf("hello, \
        world\n");
}
produced the output
hello,         world
the indenting spaces at the start of the string continuation being taken as part of the string.

An alternative, and probably nicer, approach is to make use of what is called string concatenation. This simply means that two strings which are only separated by spaces are regarded by the compiler as a single string. Actually the two strings can be separated by any combination of spaces, newlines, tab characters and comments. In the jargon of C programming all these things are collectively known as white space although perhaps we should now say "content challenged space". The use of string concatenation is shown by the following example.

main()
{
	printf("hello," /* space only */
		" world\n");
}

Programmers using MSDOS should note that versions of printf() supplied with MSDOS compilers normally convert \n to a carriage return and line feed (new-line) character pair, there is no need to try and include a carriage return character in a string. On Unix systems this is the responsibility of the display driving software.

There are several other characters that cannot conveniently be included in a string but which can be included using the \ notation. The complete list is

\a	"BELL" - i.e. a beep
\b	Backspace 
\f	Form Feed (new page) 
\n	New Line (line feed)
\r	Real carriage return
\t	Tab
\v	Vertical Tab
\\ 	Backslash
\'	Single Quote
\"	Double Quote
\?	Question Mark

It is not necessary to use the \ convention for all the above characters in strings but they will always be understood.

It is also possible to include arbitrary characters in strings by including the three digit octal representation of the character preceded by a \ symbol. For example the "hello world" program could be written in this form.

main()
{
	printf("\150ello, world\n");
}
In case you didn't know, 150 is the octal code for "h". It is not, generally, possible to use hexadecimal or decimal constants in this fashion. The use of \ in this way is known as "escaping" and the \ is known as an "escape" character, it turns off the normal meaning of the immediately following character.

The effect is actually to send the string of binary 1s and 0s equivalent to octal 150 to the output device which should respond in the manner its designers intended it to. You can use this convention to send all sorts of unusual special characters to all sorts of unusual output devices.


Program Layout

Chapter 1 section 5

The hello world program listed at the start of this chapter is laid out in accordance with normal C conventions and practice. The C programming language is "free format" which means you can lay out the text of programs in whatever form takes your fancy provided, of course, you don't run words together. The "hello world" program could have been written in this form

main(){printf("hello, world\n");}
or even this
main
(
)
{
printf
(
"hello, world\n"
)
;
}
Neither of the above is recommended.

The indentation of the word printf in the original version of the hello world program is achieved by the use of a single TAB character not a sequence of spaces. TAB characters may be generated by hitting the TAB key on normal keyboards, occasionally the TAB key carries a mysterious symbol consisting of 2 arrows. Multiple TAB characters should be used to indent C programs in preference to sequences of spaces. The use of TABs makes for smaller source files, less typing and tidier layout. All C compilers understand the use of TABs. Unfortunately some word processors and editors have strange ideas about standard TAB settings, for C source TABs should be set every 8 characters.

All but the briefest programs will benefit from descriptive comments included in the source. In the C programming language anything enclosed between the character pair /* and the character pair */ is treated as a comment. The compiler treats any comment, however long, as if it were a single space. Comments may appear in any reasonable place in the source. A commented version of the "hello world" program appears below.

/*	This is the first C program

	Author	: J.P.H.Burden
	Date	: 22 September 1992
	System	: IBM 6150 + AIX Version 2.1
*/
main()
{
	printf("hello, world\n");
}
Comments may stretch over several lines but may not be nested, i.e. you cannot include comments within comments. This can cause difficulties if you wish to "comment out" parts of a program during development. The following use of comments is perfectly correct.
main()
{
	printf("hello, world\n");
/*	
	some very clever code I haven't
	quite sorted out yet.
*/
        printf("the solution to the problem of \
life, the universe and everything is");
/*	
	more very clever code still to be
	developed
*/
}
But the following attempt to "comment out" further parts of the program.
main()
{
	printf("hello, world\n");
/*
	OK let's stop pretending we can write
	really clever programs
	/*	
		some very clever code I haven't
		quite sorted out yet.
	*/
		printf("the solution to the problem of \
	life, the universe and everything is");
	/*	
		more very clever code still to be
		developed
	*/
*/
}
Gave the following compiler error messages when using the SUN Sparc Station compiler.
"comm2.c", line 17: syntax error before or at: /
Compilation failed
The error message refers to the final "*/" in the program.


Programming Errors

Chapter 1 section 6

It is quite likely that your attempt to write a hello world program will be completely successful and will work first time. It will probably be the only C program you ever write that works first time. There are many different types of mistake you might make when writing C programs. These can result in messages from the compiler, or sometimes the linker, programs that bring the computer grinding to a halt or programs that simply produce the wrong answer to whatever problem they were trying to solve or programs that get stuck in a never-ending loop. As a beginner you are more likely to encounter compiler or linker error messages, particularly if you have already done some programming in another programming language. The rest of this section illustrates some possible errors and their consequences.

In the first example the programmer has forgotten that the C programming language is case sensitive.

MAIN()
{
	printf("hello, world\n");
}
This error gave rise to some rather mysterious error messages from the linker. Typical examples are shown below. The compiler on the IBM 6150 used to write these notes produced the following error output.
ld: Undefined -
	.main
	_main
	_end
The compiler on a SUN Sparc Station produced the following messages.
ld: Undefined symbol 
   _main 
Compilation failed
The Turbo C integrated environment produced the following.
Linker Error: Undefined symbol _main in function main
And finally Microsoft C version 5.1 produced the following error messages.
hw.c

Microsoft (R) Overlay Linker  Version 3.65   
Copyright (C) Microsoft Corp 1983-1988.  All rights reserved.

Object Modules [.OBJ]: HW.OBJ 
Run File [HW.EXE]: HW.EXE /NOI
List File [NUL.MAP]: NUL
Libraries [.LIB]: SLIBCEC.LIB 

LINK : error L2029: Unresolved externals:



_main in file(s):
 C:\MSC\LIB\SLIBCEC.LIB(dos\crt0.asm)

There was 1 error detected

All these errors reflect the fact that the linker cannot put the program together properly. On Unix based systems the linker is usually called "ld". Along with the user supplied main() function all C programs include something often called the run-time support package which is actually the code that the operating system kicks into life when starting up your program. The run-time support package then expects to call the user supplied function main(), if there is no user supplied main() then the linker cannot finish the installation of the run-time support package. In this case the user had supplied MAIN() rather than main(). "MAIN" is a perfectly valid C function name but it isn't "main".

The rather extensive set of messages from Microsoft C 5.1 were partly generated by the compiler, which translated the program without difficulty, and partly generated by the linker. The reference to "crt0" is, in fact, a reference to the C Run Time package which tries to call main() to start the program running.

In the second example the programmer, probably confusing C with another programming language, had used single quotes rather than double quotes to enclose the string passed to printf().

main()
{
	printf('hello, world\n');
}
On the IBM 6150 the compiler produced the following error messages. The reference to "hw.c" is to the name of the source file that was being compiled.
"hw.c", line 3: too many characters in character constant
"hw.c", line 3: warning: Character constant contains more than one byte
The SUN Sparc Station compiler gave the following error messages.
"hw.c", line 3: too many characters in character constant
Compilation failed
The Turbo Integrated environment gave the following messages. C:\ISPTESTS\HW.C was the name of the source file.
Error C:\ISPTESTS\HW.C 3:
Character constant too long in function main
Microsoft C version 5.1 gave the following messages.
hw.c
hw.c(3) : error C2015: too many chars in constant
In each case the error message referred clearly to the line number in error. The reference to character constants appears because the C language uses single quotes for a different purpose (character constants).

In the third example the programmer, again possibly confused by another programming language, had missed out the semi-colon on line 3.

main()
{
	printf("hello, world\n")
}
The IBM 6150 compiler produced the following message.
"hw.c", line 4: syntax error
The SUN Sparc Station produced the following messages.
"hw.c", line 4: syntax error at or near symbol }
Compilation failed
Turbo C produced the following messages.
Error C:\ISPTESTS\HW.C 4:
Statement missing ; in function main
The Microsoft C version 5.1 compiler produced the following messages.
hw.c
hw.c(4) : error C2143: syntax error : missing ';' before '}'
In all cases the error message referred to line 4 although the error was actually on line 3. This often happens when compilers detect errors in free-format languages such as C. Simply the compiler didn't realise anything was wrong until it encountered the } on line 4. The first error message is particularly unhelpful.

In the fourth example the programmer wrote print() rather than printf().

main()
{
	print("hello, world\n");
}
This, not surprisingly, produced the linker error messages shown in below. These are rather similar to the error messages shown earlier when the effects of writing MAIN() rather than main() were shown. The IBM 6150 compiler generated the following messages.
ld: Undefined -
	.print
	_print
The SUN Sparc Station compiler generated the following messages.
ld: Undefined symbol 
   _print 
Compilation failed
Turbo C generated the following messages.
Linking C:\ISPTESTS\HW.C 4:
Linker Error: Undefined symbol _print in module HW.C
The Microsoft C version 5.1 compiler produced the following messages.
hw.c

Microsoft (R) Overlay Linker  Version 3.65   
Copyright (C) Microsoft Corp 1983-1988.  All rights reserved.

Object Modules [.OBJ]: HW.OBJ 
Run File [HW.EXE]: HW.EXE /NOI
List File [NUL.MAP]: NUL
Libraries [.LIB]: SLIBCEC.LIB 

LINK : error L2029: Unresolved externals:



_print in file(s):
 HW.OBJ(hw.c)

There was 1 error detected

In the final example the programmer left out the parentheses immediately after main.

main
{
	printf("hello, world\n");
}
The IBM 6150 compiler produced the following messages.
"hw.c", line 2: syntax error
"hw.c", line 3: illegal character: 134 (octal)
"hw.c", line 3: cannot recover from earlier errors: goodbye!
The SUN Sparc Station compiler produced the following messages.
"hw.c", line 2: syntax error at or near symbol {
"hw.c", line 2: unknown size
Compilation failed
Turbo C produced the following messages.
Error C:\ISPTESTS\HW.C 2: Declaration syntax error
The Microsoft C version 5.1 compiler produced the following messages.
hw.c
hw.c(2) : error C2054: expected '(' to follow 'main'
All of these messages are remarkably unhelpful and confusing except that from Microsoft C, particularly that from the IBM 6150 compiler.

You may find it interesting to try the various erroneous versions of the "hello world" program on your particular system. Do you think your compiler generates more helpful error messages?

If you are using Turbo C you will see the following message, even when compiling a correct version of the hello world program

Warning C:\ISPTESTS\HW.C 4:
   Function should return a value in function main
A warning message means that there is something not quite right with the program but the compiler has made assumptions that the compiler writer thought reasonable. You should never ignore warnings. The ideal is to modify the program so that there are no warnings, however that would introduce extra complications into the hello world program and this particular message can be ignored for the moment.

The message means that the user supplied function main() should return a value to the run-time support package. There is no requirement to do so and most compilers recognise main() as a special case in this respect. Returning a value to the run-time support package should not be confused with returning a value, sometimes known as an exit code, to the host operating system.

C Standards and History

Chapter 1 section 7

The C programming language was developed in the years 1969 to 1973, although the first published description did not appear until the book The C Programming Language" written by Brian Kernighan and Dennis Ritchie was published in 1978. The early versions of the C language were strongly influenced by a language called BCPL which itself was a derviative of Algol.

The early development of C was closely linked to the development of the Unix operating system. Large portions of the code of the Unix operating system were eventually written in C and problems encountered in transferring Unix to various computers were reflected into the design of the C language. The modest hardware available to the Unix developers was also reflected in the language design, most notably the use of separate library functions for operations such as input and output. Until the early 1980s the language was almost exclusively associated with Unix.

The widespread introduction of microprocessor based computer systems in the early 1980s also saw a rapid growth in the use of C on such systems. C compilers were known to be small and many of the start-up operations that produced the early microprocessor based computer systems were staffed by ex-students who had encountered the language whilst at university.

As early as 1982 it became clear that the informal description of the language in Kernighan & Ritchie's book was not good enough. ANSI established a committee known as X3J11 in 1983. This committee produced a report defining the language at the end of 1989. The report was known as X3.159 but the standard was soon taken over by ISO with the designation ISO/IEC 9899-1990. This version of the language is known as ANSI-C to distinguish it from the earlier version of the language described in Kernighan and Ritchie's book. The earlier version of the language is known as K&R C. C++ and Objective-C are different languages developed from C. The GNU C compiler, often known by the command that invokes it, gcc , is public domain software available for both Unix and MSDOS based systems. It supports a version of the language close to the ANSI standard.

All the code presented in these notes, unless specifically indicated otherwise, confirms to the ANSI standard. Many compiler writers and vendors produce C compilers that will compile code conforming to the ANSI standard but they also provide a variety of extensions to suit the particular target environment. Such extensions are ususally extra library routines for functions such as PC screen handling and interfacing direct to MSDOS system functions. Sometimes the extensions include extra features introduced into the language, usually to cope with some of the problems of memory management on MSDOS based systems. In the Unix environment such extensions are less common, Unix systems are normally supplied with a compiler and the extensions appear as extra libraries for functions such as driving X-Windows displays or network communications.

All C programmers ought to be aware of what is and what isn't standard in their particular environment. The better compiler writers usually make this fairly clear in their manuals.

It is also often possible to obtain further special purpose application specific libraries for use with C compilers to provide facilities such as database handling, graphics, numerical analysis etc.


C and C++

Chapter 1 section 8

Although it is the intention of these notes to stick rigidly to the ANSI standard for C, it is quite possible you will be using a compiler, such as Turbo C, that is really a C++ compiler. This is possible because C++ is a strict superset of C which means that any C program should be acceptable to a C++ compiler. In fact some C++ compilers operate by first converting the C++ code into C. As well as the ideas associated with object-oriented programming, C++ introduces a few extra features that the C programmer should be aware of, these include an alternative syntax for comments, several extra keywords that cannot be used in normal programming and a very simple form of input and output known as stream IO.

The alternative comment syntax uses the symbol // to mark the start of a comment and the end of a line to indicate the end of a comment. This means that you cannot have multi-line comments in C++ and it also means that you cannot have a comment in the middle of a line of code. If you have ever programmed in assembler language you'll be familiar with this style of commenting. The problem of nested comments does not arise.

Of course, C++ compilers will still accept C style comments marked by symbols "/*" and "*/". You shouldn't mix the two styles in the same program.

Here is an example of the hello world program written using C++ commenting style.

//          A simple program to demonstrate
//          C++ style comments
//
//          The following line is essential
//          in the C++ version of the hello
//          world program
//
#include	<stdio.h>
main()
{
	printf("hello, world\n");  // just like C
}

The C++ stream output facility is illustrated by the following program. The basic syntax of C++ stream output is

cout << value-to-display

Note that the program, like the previous example, requires the use of "#include" and a header file name. The meaning of "#include" will be discussed in a later chapter.

#include	<iostream.h>
main()
{
	cout << "hello, world\n";
}

The "<<" symbol is read as "puts to" but may also be read as "shift to". This is really C's left shift operator. C++ allows hexadecimal chraracter representations in strings unlike ANSI C.


Character Codes

Chapter 1 section 9

Here is a list of ASCII character codes in octal and hexadecimal notation along with the equivalent C escape sequence where relevant. This table is not comprehensive.

Octal codeHexadecimal codeEscape SequenceControl SequenceStandard DesignationFunction
00000-Ctrl-@Nulldoes nothing
00101-Ctrl-ASOH-
00202-Ctrl-BSTX-
00303-Ctrl-CETXSometimes Break Program
00404-Ctrl-DEOTSometimes End of File
00505-Ctrl-EENQ-
00606-Ctrl-FACK-
00707\aCtrl-GBELAudible Signal
01008\bCtrl-HBSBackspace
01109\tCtrl-IHTTAB
0120a\nCtrl-JNLNewline or Linefeed
0130b\vCtrl-KVTVertical TAB
0140c\fCtrl-LFFNew Page or Clear Screen
0150d\rCtrl-MCRCarriage Return
0160e-Ctrl-NSO-
0170f-Ctrl-OSI-
02010-Ctrl-PDLE-
02111-Ctrl-QDC1Flow Control - On
02212-Ctrl-RDC2-
02313-Ctrl-SDC3Flow Control - Off
02414-Ctrl-TDC4-
02515-Ctrl-UNAKSometimes Line Delete
02616-Ctrl-VSYN-
02717-Ctrl-WETB-
03018-Ctrl-XCANSometimes Line Delete
03119-Ctrl-YEM-
0321a-Ctrl-ZSUBSometimes End of File
0331b-Ctrl-[ESCOften Escape for Screen Control
0341c-Ctrl-\FS-
0351d-Ctrl-]GS-
0361e-Ctrl-^RS-
0371f-Ctrl-_US-
04020--SPSpace - Hit the space bar

And the normal printing character codes with hexadecimal and octal equivalent values.

21 041 !  22 042 "  23 043 #  24 044 $  25 045 %  26 046 &  
27 047 '  28 050 (  29 051 )  2a 052 *  2b 053 +  2c 054 ,  
2d 055 -  2e 056 .  2f 057 /  30 060 0  31 061 1  32 062 2  
33 063 3  34 064 4  35 065 5  36 066 6  37 067 7  38 070 8  
39 071 9  3a 072 :  3b 073 ;  3c 074 <  3d 075 =  3e 076 >  
3f 077 ?  40 100 @  41 101 A  42 102 B  43 103 C  44 104 D  
45 105 E  46 106 F  47 107 G  48 110 H  49 111 I  4a 112 J  
4b 113 K  4c 114 L  4d 115 M  4e 116 N  4f 117 O  50 120 P  
51 121 Q  52 122 R  53 123 S  54 124 T  55 125 U  56 126 V  
57 127 W  58 130 X  59 131 Y  5a 132 Z  5b 133 [  5c 134 \  
5d 135 ]  5e 136 ^  5f 137 _  60 140 `  61 141 a  62 142 b  
63 143 c  64 144 d  65 145 e  66 146 f  67 147 g  68 150 h  
69 151 i  6a 152 j  6b 153 k  6c 154 l  6d 155 m  6e 156 n  
6f 157 o  70 160 p  71 161 q  72 162 r  73 163 s  74 164 t  
75 165 u  76 166 v  77 167 w  78 170 x  79 171 y  7a 172 z  
7b 173 {  7c 174 |  7d 175 }  7e 176 ~   


Exercises

Chapter 1 section 10

  1. Your first task, of course, is to enter, compile and run the hello world program on your computer system. Note the commands you used to compile the program, any messages produced and the names of any files generated.

  2. Now try some of the programs with errors. Note the error messages produced by your compiler. Do you think they are more infomrative than those described in the notes ?

  3. What is the effect of typing the hello world program using round brackets (parentheses) rather than curly brackets (braces) ?

  4. Write a C program that produces the following output

    	****************
            *  hello world *
    	****************
    

    using multiple printf() calls.

  5. Write a C program that prints hello world vertically on the screen.

  6. Write a C program that prints hello world on the screen and then "beeps".

  7. On many displays the character sequence ESC [ 2 J causes the display to go blank. Use this information to write a C program that clears the screen. (If you are working on a PC running MSDOS you'll need to ensure that ANSI.SYS is operational)

  8. Write a program that displays hello world in the centre of an otherwise blank screen.