This course will give an introduction to the programming language C. It runs in
the autumn term and ... C++: - Steve Oualline, Practical C++ Programming, 1995.
MSc in Mathematics and Finance, Imperial College London
Dr Robert N¨ urnberg
Computing in C++ Part I : An introduction to C Dr Robert N¨ urnberg
Introduction This course will give an introduction to the programming language C. It runs in the autumn term and is addressed to students with no or little programming experience in C. Attending this course will enable you to write simple C/C++ programs and complete possible practical assignments of other courses that run alongside this course. In this term, there will be no separate coursework assignments for the course “Computing in C++”. The course will be followed in the second term by an introduction to Object Oriented Programming in C++. That is why from the start, we will be using a C++ compiler, even though the programs are really written in C or at least in “C-like C++”. Concepts that are unique to C++ and do not form part of the C programming language will be marked with a footnote like this (∗ ). This will help you adapt your code to a C compiler, if you ever have to. Throughout this script, new concepts and ideas will be illustrated with easy to understand example programs. These examples can be compiled as they are and, if your pdf-viewer allows you to, you can even copy and paste the code into your editor without having to type it in yourself. These lecture notes are organised as follows. Chapter 1 gives a short overview over the main techniques and features of C and is designed to enable you to start programming straight away. The remaining chapters will then cover each topic in more detail.
Recommended books and sites • C only:
• C++:
-
Tony Zhang and John Southmayd, Teach Yourself C in 24 Hours, 2000 Bradley Jones and Peter G. Aitken, Teach Yourself C in 21 Days, 2003 Brian W. Kernigham and Dennis M. Ritchie, The C Programming language, 1988 Richard Johnsonbaugh and Martin Kalin, C for Scientists and Engineers, 1996 Steven R. Lerman, Problem solving and computations for scientists and engineers, an introduction using C, 1993 - publications.gbdirect.co.uk/c book (The C Book online)
-
Steve Oualline, Practical C++ Programming, 1995 Herbert Schildt, Teach yourself C++, 1992 Jesse Liberty, Teach yourself C++ in 24 hours, 1999 www.cplusplus.org, www.cplusplus.com, www.cppreference.com
Compilers • Windows:
· Microsoft Visual C++ .NET with Integrated Development Environment (IDE) · GNU C++ compiler (g++) as part of Cygwin or MinGW, without IDE · Dev-C++ – free compiler/IDE that is GNU compatible • Linux: · GNU C++ compiler (g++) – part of any distribution · Intel C++ compiler (icc) – free for students • Mac: · Xcode – free compiler/IDE that is GNU compatible • Windows/Linux/Mac: · Code::Blocks – free compiler/IDE that is GNU compatible · NetBeans – free compiler/IDE that is GNU compatible ∗A
footnote to highlight parts that are not pure C.
Computing in C++ , Part I
Dr Robert N¨ urnberg
Contents 1 Basics 1.1 “Hello World!” . . . . . . . . . 1.1.1 Waiting for input . . . . 1.2 Comments . . . . . . . . . . . . 1.3 Variables and types . . . . . . . 1.4 User input . . . . . . . . . . . . 1.5 Control flow . . . . . . . . . . . 1.6 Basic operators . . . . . . . . . 1.7 Arrays . . . . . . . . . . . . . . 1.8 Functions . . . . . . . . . . . . 1.8.1 Mathematical functions 1.9 Output control characters . . . 1.10 File I/O . . . . . . . . . . . . . 1.11 Namespaces . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
3 3 3 3 4 4 5 6 6 7 8 8 9 10
2 Variables 2.1 Basic types . . . . . . . . . . . . . . . . . . . . 2.2 Internal representation of variables . . . . . . . 2.2.1 Precision problems and rounding errors 2.2.2 Type conversion . . . . . . . . . . . . . 2.2.3 Rounding functions . . . . . . . . . . . . 2.3 Names of variables . . . . . . . . . . . . . . . . 2.4 Initialization of variables . . . . . . . . . . . . . 2.5 Scope of a variable . . . . . . . . . . . . . . . . 2.5.1 Global variables . . . . . . . . . . . . . 2.6 Arrays and strings . . . . . . . . . . . . . . . . 2.6.1 Initialization of arrays . . . . . . . . . . 2.6.2 Strings in C . . . . . . . . . . . . . . . . 2.6.3 Multidimensional arrays . . . . . . . . . 2.7 Constants . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
10 10 11 11 12 13 13 14 14 15 15 15 16 17 18
3 Control flow 3.1 The while loop . . . . . 3.2 The for loop . . . . . . 3.3 The do-while loop . . . 3.4 The if-else statement 3.5 The switch statement . 3.6 break and continue . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
18 19 19 20 20 21 22
4 Operators 4.1 Arithmetic operators . . . . . . . . . 4.2 Precedence of operators . . . . . . . 4.3 Assignment operator . . . . . . . . . 4.3.1 Special assignment operators 4.4 Logical operators . . . . . . . . . . . 4.5 Unary operators . . . . . . . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
23 23 24 24 24 25 26
5 Functions 5.1 Passing by value . . . . 5.2 The return statement . 5.3 Passing arrays . . . . . . 5.4 Passing by reference . . 5.5 Keyword const . . . . . 5.6 Static variables . . . . . 5.7 Declaration of functions
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
26 27 28 28 29 30 30 31
. . . . . .
. . . . . . .
. . . . . .
. . . . . . .
. . . . . .
. . . . . . .
. . . . . .
. . . . . . .
. . . . . .
. . . . . . .
. . . . . .
. . . . . . .
. . . . . . .
Computing in C++ , Part I
Dr Robert N¨ urnberg
6 Pointers 6.1 Pointer syntax . . . . . . . . . . . . . . . . . . . . . . 6.2 Passing by pointer . . . . . . . . . . . . . . . . . . . . 6.2.1 Keyword const . . . . . . . . . . . . . . . . . . 6.3 The NULL pointer . . . . . . . . . . . . . . . . . . . . . 6.3.1 if statement and pointers . . . . . . . . . . . . 6.4 Pointers and arrays . . . . . . . . . . . . . . . . . . . . 6.4.1 Pointer arithmetic . . . . . . . . . . . . . . . . 6.4.2 Differences between pointers and arrays . . . . 6.5 Pointers to pointers . . . . . . . . . . . . . . . . . . . . 6.6 Dynamic memory allocation . . . . . . . . . . . . . . . 6.6.1 Segmentation faults and fatal exception errors 6.6.2 Dynamic allocation of multidimensional arrays 6.7 Pointers to functions . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
32 32 33 33 34 34 34 34 35 35 36 37 37 38
7 Structures 7.1 Structure syntax . . . . . . . 7.2 Operations on structures . . . 7.3 Pointers and structures . . . 7.4 Passing structures . . . . . . 7.5 typedef . . . . . . . . . . . . 7.5.1 typedef and #define 7.6 Selfreferential structures . . . 7.7 Examples . . . . . . . . . . . 7.7.1 Sparse matrices . . . . 7.7.2 Binary trees . . . . . .
. . . . . . . . . .
. . . . . . . . . .
8 Advanced material 8.1 enum and union . . . . . . . . . . 8.2 Conditionals . . . . . . . . . . . . 8.3 Preprocessor directives . . . . . . 8.3.1 #define . . . . . . . . . . 8.3.2 #undef . . . . . . . . . . 8.3.3 #if, #endif, #else and 8.3.4 #ifdef and #ifndef . . . 8.4 Header files . . . . . . . . . . . . 8.5 Command line parameters . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
40 40 40 41 41 41 42 42 43 43 45
. . . . . . . . . . . . . . . . . . . . #elif . . . . . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
46 46 47 47 47 48 48 49 49 50
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
Computing in C++ , Part I
1
Dr Robert N¨ urnberg
Basics
A C/C++ program is nothing other than a text file with a very special syntax. In order to distinguish these files from other text files, they are usually given the ending .cpp (for windows) or .cc (for linux). These files are also called source files, as they are the source for the actual program. A C/C++ compiler is then needed to translate the source files into an executable (or binary) file format, that can be executed by the operating system. This binary file is then the actual program. When working under windows in most cases you will not be aware of the executable file, as the compiler will allow you to run the code immediately after compilation. The following syntax guidelines you need to know, before you can start to write your first C++ program. Each C/C++ program needs to have a main() program. Inside this main() program we define what the code is supposed to do. The commands that are to be executed are grouped together inside some “{ }” brackets. Each statement in C must be terminated by a semi-colon “;”. See the first example programs in the next subsection.
1.1
“Hello World!”
There are two ways to output things on the screen. One uses the C++ stream cout (on the left) and the other uses the C function printf() (on the right). (∗ ) #include using namespace std; int main() { cout