A C Introductory Course

Professor Rob Pooley

This is a self contained introduction to the C programming language. It is intended for students with some programming experience, but assumes very little.

It is also the first part of a course on C++, the remainder of which is available online.

The material is copyright Rob Pooley, but is made available free for personal use, but anyone intending to use it for commercial purposes should first contact the author for terms for its use.

Any hyperlinks to this material should start here or at the enclosing level of the C++ course.

The idea is to work through the notes and exercises by following links from one to the next. For those who want to go straight to a topic instead, a contents page is also provided below.

Note that in the examples the occurence of a less than sign (or opening angle bracket) has had to be replaced with the corresponding HTML character symbol. If you copy a program from the notes you will find it usually does not compile because of this. You would need to edit it to replace the HTML sequence with the less than character. To help avoid this, all examples are also provided as simple text files, which should compile and run unchanged.

Normally, new students start here.

Each page in the notes has an associated question page and, usually, a page with answers. You can use these to re-inforce the material in the notes.

Some of the exercises are intended to be milestones, by which you can judge your understanding. These are linked to the relevant page, but also appear directly in the index below, printed in red if you have a colour capable browser. These should be completed before you decide to move on.


Contents

What is C?

The C recipe

Variables
Types
Identifiers
Literals

Expressions

Arithmetic expressions
Operators
Boolean expressions
Comparison operators
Boolean operators
Bitwise operators
Order of evaluation
Precedence
Example
Converting or casting values

Statements

Assignment
Conditional statements
An example program
Compound statement
While loop
For loops
An example of a for loop
Further example of loops
First programming milestone


switch statements

Input and output

Output of items in C
Input of items in C
Format specifiers in C

User-Defined Types

Enumerated types
Making a type definition
Arrays
Multi-dimensional arrays
Second programming milestone


Structures or Record Types
Union types
Making a type definition for user defined types
An example using structs in a C program


Third programming milestone


Functions

Function declarations
Arguments to functions
Generating the value in a function
Calling a function
Function prototypes
Recursion
Calculator for parentheses using recursion

Storage classes

Automatic variables
Statics in functions
Global statics
External variables
Function declarations and scope
Forward declarations

Referring to locations

Reference parameters
Fourth programming milestone


Pointers
Arrays vs pointers
Pointer arithmetic
A note on strings and char*

Dynamic memory allocation in C

void * pointers
Accessing fields via pointers to structs and unions
Building a linked list in C
Building a binary tree in C


Fifth programming milestone