Transcript Document

CS1061 C Programming (5 credits)
Lecturer: Adrian O’Riordan
Contact: email is [email protected], office is 312, Kane Building
Lectures:
Practicals:
24 in total
Monday @9:00
Wednesday @9:00
G2, Kane Building
ORB212, O’Rahilly Building
Organised later in year (12 hours)
Assessment: Total Marks 100:
End of Year Written Examination (1½hr paper) 80 marks;
Continuous Assessment 20 marks.
Objectives

Module Objective: To teach fundamental techniques of computer
programming using the C language.
Module Content:





Principles of programming: algorithms, design and specification;
testing and debugging.
Fundamentals of C programming: data types; operators;
expressions; control structures; console and file i/o.
Program structure: functions, recursion, standard libraries.
Aggregate data types: arrays; pointers; strings; records.
Dynamic data structures: allocation; deallocation; memory
management; list structures.
Reading material

The Boole library has a number of good books on C:




C for scientists and engineers / Richard Johnsonbaugh, Martin
Kalin, Prentice Hall, 1997.
A book on C : programming in C / Al Kelley, Ira Pohl, AddisonWesley, 1998.
C : how to program / H. M. Deitel, P. J. Deitel, Prentice-Hall, 1994.
There are also a number of good tutorials freely available on the
WWW.
The computer

The primary components of a computer are the






central processing unit (CPU),
arithmetic and logic unit,
main memory,
and various input/output devices.
This is the hardware. The programs that run on a computer are
the software. The CPU controls the execution of these programs
using its own internal clock.
A computer program is a sequence of instructions issued to a
computer. The instructions are stored in the computer’s
memory.
Instructions
The instructions are read from memory one-by-one by the
computer processor. Main memory is fast to access. Secondary
storage, i.e. hard disks, is typically slower to access but much
larger in capacity.
Program instructions are read and executed extremely quickly by a
computer compared to human speeds. (For example, an
instruction (executed in a single clock cycle) on a 3GHz
processor could potentially be executed in 0.33 nanoseconds.
Programs often perform input (from keyboard/mouse) and output
(to terminal) functions in addition to executing instructions. This
tends to be much slower.
Programming languages


Programs are written in a programming language.
The earliest programming languages were all low-level such as
machine code and assembler.



Low-level languages are hard and tedious to write but tend to
execute quickly.
High-level languages have a more structured form and English-like
syntax. A program called a compiler (or interpreter) is used to
translate the high-level source into machine code that the
computer can understand.
The development of higher-level languages greatly increased
programming productivity.
Examples of Languages
Example taken from Dietel and Dietel
1.Machine language - strings of numbers giving machine specific instructions
1300042774
1400593419
1200274027
2.Assembly languages
LOAD BASEPAY
ADD OVERPAY
STORE GROSSPAY
3.High-level languages - Code similar to everyday English
grossPay = basePay + overTimePay
Problem Solving
Programming is a form of problem solving.


Faced with the requirement to implement a solution to a problem,
the programming has to translate the problem description into a
language a computer can understand. Though requirements may
be expressed as clearly, succinctly and unambiguously as possible,
there is still a lot of scope for errors, omissions and inconsistencies.
Implementation can introduce a new layer of errors.
Thus software development has a reputation for being hard, and
cannot be fully automated.
A key idea in computer science is that of an algorithm (see later)
Introducing C




C is a high-level language that also retains many features of
low-level programming.
C is very fast – as fast as machine code on many platforms.
Thus C is the first choice for developers concerned with
performance, for example in applications such as embedded
systems, real time systems, and reconfigurable hardware.
C was probably the world’s most widely used programming
language until more recently being challenged by the rise of
Java.
A lot of modern programming languages, such as C++ and
Java, are influenced and retain a similar syntax to C.
Advantages of C








Widely adopted industry standard
Very fast
Supports structured programming (see later)
Relatively small number of commands and keywords
Powerful pointer-based implementation (see later)
Low-level (bit-wise) operations (see later)
Supports dynamic memory allocation (see later)
Code libraries available (See later)
Background Context

History



C was developed at Bell labs. c. 1971 by Martin Ritchie based on
Martin Richard’s BCPL (1967) and Ken Thompson’s B language
(1970).
C was initially used predominantly for systems programming, but is
now very much a general-purpose language. It has been ported to
a large number of platforms including Windows, Macintosh and
many flavours of UNIX.
Standardisation

Initially many slight variations of C existed, and were incompatible
so a committee was formed to create a unambiguous, machineindependent definition. ISO Standard created in 1989 (most widely
used) and updated in 1999 (C99).
Related Languages



C++ - The C++ programming language was derived from C
and has object-oriented functionality with C-like syntax.
Objective-C - Objective-C is a very "thin" layer on top of, and
is a strict superset of C
Java and C#- Modern languages that retain the basic C syntax