Skip to content
Physics and Astronomy
Home Our Teaching Resources C programming Summary of key points
Back to top

Summary of key points

The text of each key point is a hyperlink to its place in the appropriate lecture notes. Alternatively, click on the section title to read the full page.

Preface

People v computers

Algorithms and data

Exercise: Mr Turtle

Introduction to C

Comments!

Pre-processor (#) Directives

The main() function

Statements

The printf() function and character strings


The return statement

Making our program easier to understand

Arithmetic and variables

Printing numbers to the screen

Variables and expressions

Arithmetic expressions

Standard mathematical functions

Integer variables and expressions

Complex numbers

Reading variables from the keyboard with scanf()

Conditional statements

The if() statement

Relational operators

{ ... } blocks

The switch() statement

Loops

The while() loop

Debugging loops

The for() loop

"In place" arithmetic operators

The break and continue statements

Functions

What is a function?

Why are functions useful?

1. Code that gets used more than once

2. Hiding complexity

Criteria for a good function

Writing our own functions

We have written one function, and used others, already

Local variables: a vitally important principle

Function prototypes

Memory, input and output

Mistakes with memory addresses

Reading in variables with scanf()

Checking the arguments to scanf()

Writing to, and reading from, files

Arrays, etc.

When to use arrays

Declaring and using a one-dimensional array



Passing the address of an array to a function

Arrays of arrays, or multi-dimensional arrays

Dealing with text

Single characters

Character strings

Non-fixed strings, or arrays of characters.

Three things to look out for

Utility functions for characters and strings

String utility functions: <string.h>

Other useful features

Two-dimensional character arrays are arrays of (writeable) strings

Bugs and debugging

Find the bugs before they bite you where it hurts

Put in checks

Finding bugs

Memory and pointers

When we needed to "pass the array to another function" we encountered the simple, but key innovation is that whereas previously we had used memory addresses that are constants,  we can now also use memory addresses that are the values of variables.  We can illustrate a trivial example as follows:

Pointers

When pointers attack: random pointer values

Using pointers to allocate space

Advanced: realloc()

Appendix: allocation

How to use pointers effectively

Structures

Structures do for variables what functions do for code

Defining a new composite variable (structure) type

When to use structures and what to put in them

Pointers to structures

Thinking at a higher level

Keeping track of memory

Allocating structures

Linked lists

Creating a linked list

Traversing a list

Removing and freeing a member of a list

Recursion

C wrap-up

Unformatted input and output

Command-line arguments

Named constants and enumerations

Sharing variables between functions

Static variables inside functions

qsort() can sort arrays of anything

Multiple files and the pre-processor

Header files: "mydefs.h"

Calling functions in other source  files

Sharing and not sharing variables between files

Sharing variables between functions in different source files

Header file protection

Log in

                                                                                                                                                                                                                                                                       

Validate   Link-check © Copyright & disclaimer Privacy & cookies Share
Back to top