My Basket
You have nothing in your shopping cart yet.
Title: C Programming Tutorials
Description: Every program is limited by the language which is used to write it. C is a programmer's language. Unlike BASIC or
Pascal, C was not written as a teaching aid, but as an implementation language. C is a computer language and a
programming tool which has grown popular because programmers like it! It is a tricky language but a masterful one.
Sceptics have said that it is a language in which everything which can go wrong does go wrong. True, it does not do
much hand holding, but also it does not hold anything back. If you have come to C in the hope of finding a powerful
language for writing everyday computer programs, then you will not be disappointed. C is ideally suited to modern
computers and modern programming.
Document Preview
Extracts from the notes are below, to see the PDF you'll receive please use the links above
C Programming Tutorial
Node:Top, Next:Preface, Previous:(dir), Up:(dir)
C Programming Tutorial (K&R version 4)
This is a C Programming Tutorial for people who have a little experience with an interpreted programming language,
such as Emacs Lisp or a GNU shell
...
02
Copyright © 1987,1999 Mark Burgess
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this
permission notice are preserved on all copies
...
htm[6/29/2009 12:42:58 PM]
C Programming Tutorial
Node:Preface, Next:Introduction, Previous:Top, Up:Top
Preface
Every program is limited by the language which is used to write it
...
Unlike BASIC or
Pascal, C was not written as a teaching aid, but as an implementation language
...
Sceptics have said that it is a language in which everything which can go wrong does go wrong
...
If you have come to C in the hope of finding a powerful
language for writing everyday computer programs, then you will not be disappointed
...
This book is a tutorial
...
It presumes that you have some previous aquaintance with programming -- you need to know what a
variable is and what a function is -- but you do not need much experience
...
When it comes down to it, most languages have
basically the same kinds of features: variables, ways of making loops, ways of making decisions, ways of accessing
files etc
...
You will most likely find all of those things and more, as you work though the chapters
...
In places these examples make use of
features before they have properly been explained
...
Mark Burgess
...
The book was originally
published by Dabs Press
...
This new edition is written in Texinfo, which is a documentation system that
uses a single source file to produce both on-line information and printed output
...
Node:Introduction, Next:Reserved words & example, Previous:Preface, Up:Top
Introduction
What is C? What is it for? Why is it special?
Levels:
Basic ideas:
The compiler:
file:///C|/Users/HKN/Desktop/C%20Programming%20Tutorial1
...
A computer falls definitely into the category of complex objects and it
can be thought of as working at many different levels
...
Low level is perhaps the easiest to understand: it describes a level of
detail which is buried down amongst the working parts of the machine: the low level is the level at which the
computer seems most primitive and machine-like
...
Imagine stepping back from the complexity of the machine level pieces and grouping together parts which work
together, then covering up all the details
...
) At a high level a computer becomes a group of black boxes which can then
be thought of as the basic components of the computer
...
The aim of any high level computer language is to provide an easy and
natural way of giving a programme of instructions to a computer (a computer program)
...
As you might expect, the action which results from a single
machine code instruction is very primitive and many thousands of them are required to make a program which does
anything substantial
...
C is one of a large number of high level languages which can be used for general purpose programming, that is,
anything from writing small programs for personal amusement to writing complex applications
...
Before C, high level languages were criticized by machine code programmers because they shielded the user
from the working details of the computer, with their black box approach, to such an extent that the languages become
inflexible: in other words, they did not not allow programmers to use all the facilities which the machine has to offer
...
file:///C|/Users/HKN/Desktop/C%20Programming%20Tutorial1
...
They are not merely monologues to the machine, they are a way to express ideas and a way to solve problems
...
This is
vitally important for writing lengthy programs because complex problems are only manageable with a clear
organization and program structure
...
These provide an excellent basis for controlling the
file:///C|/Users/HKN/Desktop/C%20Programming%20Tutorial1
...
Another unusual feature of C is the way it can express ideas concisely
...
C gives us the apparatus to build neat and compact programs
...
Its conciseness can be a mixed blessing: the aim is to try to seek a balance between
the often conflicting interests of readability of programs and their conciseness
...
C allows things which are disallowed in other languages: this is no defect, but a very powerful freedom which, when
used with caution, opens up possibilities enormously
...
The programmer carries an extra responsibility to write a careful and
thoughtful program
...
C tries to make the best of a computer by linking as closely as possible to the local environment
...
Above all it is flexible
...
The aim of this book is to convey some of the C philosophy in a practical way and to provide a comprehensive
introduction to the language by appealing to a number of examples and by sticking to a strict structuring scheme
...
htm[6/29/2009 12:42:58 PM]
C Programming Tutorial
hoped that this will give a flavour of the kind of programming which C encourages
...
What can go wrong
...
It differs in a
number of ways
...
This form of the
program is called the source program
...
Secondly, the completed source file is passed to a compiler--a program which generates a new file containing a
machine code translation of the source text
...
The executable
file is said to have been compiled from the source text
...
You use a screen editor to create the words of a program (program text) and run the final program
in its compiled form usually by simply typing the name of the executable file
...
A compiler usually operates in two or more phases (and each phase may have stages
within it)
...
As we shall see later, this approach provides a flexible
file:///C|/Users/HKN/Desktop/C%20Programming%20Tutorial1
...
A two-phase compiler works in the following way:
Phase 1 scans a source program, perhaps generating an intermediate code (quadruples or pcode) which helps to
simplify the grammar of the language for subsequent processing
...
A separate object file is built for each separate
source file
...
o files
...
This program appends standard library code to the object file so that the code is complete
and can "stand alone"
...
Even at this stage, the compiler can fail, if it finds that it has a reference to a function which
does not exist
...
To avoid the irritation of typing two or three separate commands (which are often cumbersome) you will normally find
a simple interface for executing compiler
...
out
...
c , write
file:///C|/Users/HKN/Desktop/C%20Programming%20Tutorial1
...
c
Node:Errors, Next:Use of Upper and Lower Case, Previous:The compiler, Up:Introduction
Errors
Errors are mistakes which we the programmers make
...
They are listed all in one go, with the line number, in the text file, at which the error occurred
and a message to say what was wrong
...
Upon compilation, you would see this error message:
eg
...
c:12: parse error before `y'
(If you compile the program in Emacs, you can jump directly to the error
...
However, a compiler will usually not stop at the first error it encounters but will
attempt to continue checking the syntax of a program right to the last line before aborting, and it is common to
submit a program for compilation only to receive a long and ungratifying list of errors from the compiler
...
The situation thus
looks much worse than it really is
...
As a rule, look for the first error, fix that, and then recompile
...
But at the beginning, just look for and fix the first error
...
You intend to send a letter to all drivers whose licenses will expire soon; instead, you send a letter
to all drivers whose licenses will expire sometime
...
This file will contain machine code which can
be executed according to the rules of the computer's local operating system
...
Node:Use of Upper and Lower Case, Next:Questions 1, Previous:Errors, Up:Introduction
Use of Upper and Lower Case
One of the reasons why the compiler can fail to produce the executable file for a program is you have mistyped
something, even through the careless use of upper and lower case characters
...
Unlike languages such as Pascal and some versions of BASIC, the C compiler distinguishes between small letters and
capital letters
...
If a letter is typed in the
file:///C|/Users/HKN/Desktop/C%20Programming%20Tutorial1
...
Declarations
Compiler languages require us to make a list of the names and types of all variables which are going to be used in a
program and provide information about where they are going to be used
...
It serves
two purposes: firstly, it provides the compiler with a definitive list of the variables, enabling it to cross check for
errors, and secondly, it informs the compiler how much space must be reserved for each variable when the program is
run
...
Consequently, the type of a variable is of great importance to the compiler
...
Node:Questions 1, Previous:Use of Upper and Lower Case, Up:Introduction
Questions
1
...
3
...
5
...
The basic instructions are built up using a reserved set of words, such as main , for , if,while ,
default , double, extern, for , and int , to name just a few
...
You cannot use default, for example, as
the name of a variable
...
See All the Reserved Words, for a complete list of the reserverd words
...
If you use a word which has already been adopted in a
library, there will be a conflict between your choice and the library
...
For example, the stdio library, which is part of the C library, provides
standard facilities for input to and output from a program
...
While the features provided by libraries are not strictly a part of the C language itself, they are
essential and you will never find a version of C without them
...
printf:
Example 1:
Output 1:
Questions 2:
file:///C|/Users/HKN/Desktop/C%20Programming%20Tutorial1
...
It provides
an superbly versatile way of printing text
...
some string
...
These can be inserted into a text string
by using a `control sequence' inside the quotes and listing the variables after the string which get inserted into the
string in place of the control sequence
...
The printf function is described in full detail in the relevant
chapter, but we'll need it in many places before that
...
If you are
reading this in Info, you can copy this to a file, compile and execute it
...
h>
/***********************************************************/
main ()
{
printf
printf
printf
printf
printf
printf
printf
printf
printf
printf
printf
printf
}
/* Poem */
("Astronomy is %dderful \n",1);
("And interesting %d \n",2);
("The ear%d volves around the sun \n",3);
("And makes a year %d you \n",4);
("The moon affects the sur %d heard \n",5);
("By law of phy%d great \n",6);
("It %d when the the stars so bright \n",7);
("Do nightly scintill%d \n",8);
("If watchful providence be%d \n",9);
("With good intentions fraught \n");
("Should not keep up her watch divine \n");
("We soon should come to %d \n",0);
Node:Output 1, Next:Questions 2, Previous:Example 1, Up:Reserved words & example
Output
Astronomy is 1derful \n"
And interesting 2
The ear3 volves around the sun
And makes a year 4 you
The moon affects the sur 5 heard
By law of phy6d great
It 7 when the the stars so bright
Do nightly scintill8
If watchful providence be9
With good intentions fraught
Should not keep up her watch divine
file:///C|/Users/HKN/Desktop/C%20Programming%20Tutorial1
...
2
...
4
...
Write a command to print out the number 22?
Write two commands to print out "The 3 Wise Men" two different ways
...
This is a layer of software which drives the hardware
and provides users with a comfortable environment in which to work
...
The operating system
is the route to all input and output, whether it be to a screen or to files on a disk
...
In C the link between these two is very efficient
...
Recently the tendency has been to try to eliminate typing completely by providing graphical user interfaces (GUIs) for
every purpose
...
For that one needs a command language
...
On microcomputers
command languages are usually very similar in concept, though more primitive, with only slightly different words for
essentially the same commands
...
When most compiler languages were developed, they were intended to be run on large mainframe computers which
operated on a multi-user, time-sharing principle and were incapable of interactive communication with the user
...
Input and output are not actually defined as a fixed, unchanging part of the C language
...
This file is called a standard C library
...
) The library is standard in the sense that C has developed a set of functions which all
computers and operating systems must implement, but which are specially adapted to your system
...
htm[6/29/2009 12:42:58 PM]
C Programming Tutorial
Files and Devices
The filing system is also a part of input/output
...
C does this implicitly
(it comes from Unix)
...
The corresponding route for output is called "stdout" or standard output file and is usually a
monitor screen
...
The keyboard and the monitor screen are not
really files, of course, they are `devices', (it is not possible to re-read what has been sent to the monitor", or write to
the keyboard
...
The advantage of treating devices like this is that it is not necessary to know how
a particular device works, only that it exists somewhere, connected to the computer, and can be written to or read
from
...
This is a
great simplification of input/output! The filenames of devices (often given the lofty title `pseudo device names')
depend upon your particular operating system
...
You might
have to open it explicitly as a file
...
Node:Filenames, Next:Command languages, Previous:Files devices, Up:Operating systems
Filenames
The compiler uses a special convention for the file names, so that we do not confuse their contents
...
c
...
o, as yet unlinked
...
EXE on Windows derived systems
...
a or liblibraryname
...
Header files are always called libname
...
The endings `dot something' (called file extensions) identify the contents of files for the compiler
...
The quad file and the object file are only working files and should be deleted by the compiler at the end of
compilation
...
c suffix is to tell the compiler that the file contains a C source program and similarly the other
letters indicate non-source files in a convenient way
...
c
Node:Command languages, Next:Questions 3, Previous:Filenames, Up:Operating systems
Command Languages and Consoles
In order to do anything with a compiler or an editor you need to know a little about the command language of the
operating system
...
e
...
ls -l
less filename
emacs filename
file:///C|/Users/HKN/Desktop/C%20Programming%20Tutorial1
...
Fortunately it is possible to get by with knowing just handful of the most common ones and having the
system manual around to leaf through when necessary
...
Every system will have its own way of
halting or terminating the operation of a program or the execution of a command
...
In GNU/Linux, CTRL-C is used
...
2
...
4
...
What is an operating system for?
What is a pseudo-device name?
If you had a C source program which you wanted to call `accounts' what name would you save it under?
What would be the name of the file produced by the compiler of the program in 3?
How would this program be run?
Node:Libraries, Next:Programming style, Previous:Operating systems, Up:Top
Libraries
Plug-in C expansions
...
The core of the C language is small and simple
...
This is what makes C so portable
...
You can also make your own, but to do so you need to know
how your operating system builds libraries
...
Libraries are files of ready-compiled code which we can merge with a C program at compilation time
...
For example, there are
libraries of mathematical functions, string handling functions and input/output functions and graphics libraries
...
For example, to merge with the math library libm
...
c -lm
when you compile the program
...
If we wanted to add in the socket library libsocket
...
c -lm -lsocket
and so on
...
When library functions are used in programs, the appropriate
library code is included by the compiler, making the resulting object code often much longer
...
htm[6/29/2009 12:42:58 PM]
C Programming Tutorial
conjunction with the libraries
...
You cannot then use the names of functions or macros which have already been
defined in libraries or header files to mean anything other than what the library specifies
...
h
...
The math
...
a
...
h
at the top of a program file
...
h"
includes a personal header file which is in the current directory
...
h>
includes a file which lies in a standard directory like /usr/include
...
Some functions can be used without having to include library files or special libraries explicitly since every program is
always merged with the standard C library, which is called libc
...
h>
main ()
{
printf ("C standard I/O file is included\n");
printf ("Hello world!");
}
A program wishing to use a mathematical function such as cos would need to include a mathematics library header
file
...
h>
#include