Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

lOMoARcPSD|5739061

C programming - Lecture notes

Computer Programming (Monash University)

StuDocu is not sponsored or endorsed by any college or university


Downloaded by Suman Saha (suman.struc@gmail.com)
lOMoARcPSD|5739061

Overview of a Computer - Hardware

Reference: Brooks, Chapter 1; KP, Chapter 1

 Central Processing Unit (CPU) controls the flow of instructions and data and
performs the necessary manipulation of data
 Primary storage (memory) is used to store information for immediate access by the
CPU
 Note that there are many levels of cache used in primary storage
 Secondary storage devices (e.g., the hard drive, CD-ROM drives, tapes, etc.) provide
permanent storage of large amounts of data, but are much slower than primary
storage
 Input and Output devices provided interfaces between the computer and the user

Secondary
Storage

Primary
Storage

Input Output
Control
Devices Devices
Unit

Arithmetic
Logic Unit

Central Processing
Unit
(CPU)

Copyright (c) 1998 by Robert C. Carden IV, Ph.D.


8/27/2019
Downloaded by Suman Saha (suman.struc@gmail.com)
lOMoARcPSD|5739061

Overview of a Computer - Data Representation


 The first computers used vacuum tubes to hold data
 Vacuum tubes have two states - ON and OFF
 An ON state represents a 1
 An OFF state represents a 0
 Eight vacuum tubes strung together can represent an 8 digit string of 0s and 1s
 Put another way, this string is an 8 digit binary (base 2) number

 We use decimal (base 10) numbers in our daily life


 A decimal number is a string of digits whose values are drawn from the set
{0,1,2,3,4,5,6,7,8,9}

 In general, a number system is simply a way of representing numbers


 A number system has a base (the number of digits used in the number system)
 Consider a number in a base b number system:
a n a n  1  a1a0

 The value of this number is:


a n b n  a n  1b n  1    a1b1  a 0 b 0

 A binary number has a base of 2 where the valid digits are 0 or 1


 E.g., 1001 binary == 9 decimal (1*8 + 0*4 + 0*2 + 1)
 An octal number has a base of 8 where the valid digits are 0 through 7
 E.g., 031 octal == 25 decimal (3*8 + 1)
 A decimal number has a base of 10 where the valid digits are 0 through 9
 E.g., 2000 decimal == 2000 (Y2K bug ;-))
 A hexadecimal number has a base of 16 where the valid digits are 0 through F, i.e.
{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}
 E.g., xABBA hex == 43962 decimal (10*4096 + 11*256 + 11*16 + 10)

Downloaded by Suman Saha (suman.struc@gmail.com)


lOMoARcPSD|5739061

Overview of a Computer - Data Representation (2)

Powers of 2
1 2^11 2048 2K
2^0
2^1 2 2^12 4096 4K
2^2 4 2^13 8192 8K
2^3 8 2^14 16,384 16K
2^4 16 2^15 32,768 32K
2^5 32 2^16 65,536 64K
2^6 64 2^17 131,072 128K
2^7 128 2^18 263,144 256K
2^8 256 2^19 524,288 512K
2^9 512 2^20 1,048,576 1M
2^10 1024 1K 2^21 2,097,152 2M

1 KILO = 2^10 = 1024


1 MEG = 2^20 = 1024*1024 = 1,048,576
1 GIGA = 2^30 = 1024*1024*1024 = 1,073,741,824

 To evaluate a binary number, say 101101, simply add up the corresponding powers
of 2:

101101 1 2 5  0 2 4  1 2 3  1 2 2  0 21  1 2 0

32  0  8  4  0  1 45

Downloaded by Suman Saha (suman.struc@gmail.com)


lOMoARcPSD|5739061

Overview of a Computer - Hexadecimal Numbers


 A hexadecimal number is a string of hexadecimal digits
 Digits A, B, C, D, E, F represent the numbers 10, 11, 12, 13, 14, and 15
 Hexadecimal is popular in the computer field because it can be used to concisely
represent a long string of binary digits
 Consider a 16 digit binary number:

1011000111000101
 Break this up into groups of 4:

1011 0001 1100 0101


 Convert each group of 4 into decimal:

11 1 12 3
 Then convert each decimal number into hex:

B1C3
 And you now have the number: Baker 1 Charlie 3

Able = A = 10 = 1010
Baker = B = 11 = 1011
Charlie = C = 12 = 1100
Dog = D = 13 = 1101
Easy = E = 14 = 1110
Fox = F = 15 = 1111

 By the same token, the HEX number 3F2C1596 represents the binary string:

0011 1111 0010 1110 0001 0101 1001 0110

Downloaded by Suman Saha (suman.struc@gmail.com)


lOMoARcPSD|5739061

Overview of a Computer - Primary Storage


 Primary storage, also known as main memory or RAM (random access memory) is
used to store information for immediate access by the Central Processing Unit (CPU)
 Memory can be viewed as a series of memory cells with each cell having its own
individual address
 E.g., think of a bank of mailboxes at a post office
 The information contained in a memory cell is called the contents of that cell
 Memory cells can be used to store data, such as characters or numbers
 Internally, of course, they are all numbers, but we can choose to interpret some
numbers as characters
 They can also be used to store program instructions
 These are “special” numbers that are meaningful to a CPU!
 The smallest unit of computer storage is a bit, as in binary digit
 Most computers group bits together to form larger entities
 E.g., 8 consecutive bits often form a byte and 32 consecutive bits often form a
word
 A word on an Intel 286 computer is 16 bits or 2 bytes
 A word on an Intel 386, 486, and Pentium computers is 32 bits or 4 bytes
 The next generation of Intel computers (e.g. the Merced) will use 64 bit words,
i.e. 8 bytes
 The DEC Alpha computer is currently using 64 bit words
 Many mainframe computers, such as the Unisys A-Series, use 48 bit words
 Memory cells in a computer are typically one byte or one word in size
 A word is a unit of information that can be transferred to and from memory

 A kilobyte of memory, as in 1K, is 1024 bytes of memory


 A megabyte of memory, as in 1M, is 1024K of memory, or 2^20 bytes
 A gigabyte of memory, as in 1G, is 1024M of memory, or 2^30 bytes
 Note that a 32 bit word can represent 2^32 different possible values

Downloaded by Suman Saha (suman.struc@gmail.com)


lOMoARcPSD|5739061

Programming Languages - Low Level Languages


 Computers only do what they are told to do
 Except in Hollywood movies, e.g., 2001, Terminator 2, the Matrix, etc. ;-)
 In order for a computer to perform a task, it must be given a series of specific
instructions in a language it can understand

 The fundamental language of any computer is its machine language


 This is typically sequences of zeroes and ones
 In the very early days of computers, this was the only way one could write
programs!!!

 To relieve the suffering of these early programmers, a higher level language called
assembly language was developed
 Assembly language contains mnemonic words and symbols for the binary
machine instructions
 An assembler maps assembly language instructions into machine language
instructions

 Assembly language programming is indeed a significant improvement over machine


language programming
 However, it has the following drawbacks:
 Machine dependent - each computer architecture has its own unique assembly
language
 Low level instructions - writing programs is very time consuming, tedious, and
error-prone

Downloaded by Suman Saha (suman.struc@gmail.com)


lOMoARcPSD|5739061

Programming Languages - High Level Languages


 A general trend in computing over the past 4 decades is to elevate programming from
low level to higher level languages
 I.e., high level languages are geared more toward people writing the programs
rather than the computer
 Assembly language instructions map directly to machine instructions
 High level language instructions must be translated/compiled into machine
instructions

 High level languages are more “problem-oriented” than assembly/machine languages


 E.g, they require little or no knowledge of the underlying computer architecture
 Learning how to write/debug programs in high level languages is much easier and
less error-prone than learning how to write/debug equivalent programs in assembler
 E.g., high level languages required fewer statements to do the same thing as
assembler
 Programs written in high level languages can be ported much more easily to different
computer architectures
 E.g., the compiler encapsulates the machine-dependent details of the target
assembly language

 A special program called a compiler is needed to translate a program written in a


high level language into assembly code (which is then transformed into native
machine code by an assembler)
 The statement written in the high level language are called source code
 The compiler/assembler's output is called object code

Source Compiler Object


Code Assemble Code
r

Downloaded by Suman Saha (suman.struc@gmail.com)


lOMoARcPSD|5739061

Language Taxonomy

FORTRAN COBOL ALGOL ASSEMBLER

FORTRAN IV PL/1
B,BCPL
BURROUGHS
FORTRAN77 EXTENDED
ALGOL

SMALLTALK
ALGOL68
APL

COBOL85
PROLOG
LISP
C
PASCAL

ANSI C C++
MODULA-2

ADA E

Downloaded by Suman Saha (suman.struc@gmail.com)


lOMoARcPSD|5739061

History of C

1970'S C

Early 1980's TRADITIONAL C

Late 1980's ANSI C


[OR STANDARD C]

Downloaded by Suman Saha (suman.struc@gmail.com)


lOMoARcPSD|5739061

Significant Points About C

 Small number of keywords


 Widely available, particularly on personal computers and workstations
 Can be used very portably
 Standard library
 Preprocessor may be used to isolate machine dependent code
 Unlike Pascal, which has many dialects
 Native language of UNIX (tm) and Windows NT
 Terse
 Powerful set of operators
 Statements can be very powerful
 Some are bit level operators
 Designed to be implemented efficiently on many machines
 Modular -- functions
 Parameters are typically passed `by value’
 No nested functions
 Syntax is complicated
 Semantics of certain features are complex and error-prone

Downloaded by Suman Saha (suman.struc@gmail.com)


lOMoARcPSD|5739061

A Comparison of Programming Language Philosophy

Pascal
 Strict Parent (a “bondage and discipline” language ;-))
 Restricts programmer for his/her own good
 A white, automatic transmission automobile with lots of safety features (e.g., air
bags, controls that limit speed to 55 miles per hour and prohibit leaving the lights on
or locking the keys in the car)

C
 A permissive, easy going parent (a ‘lassize-faire’ language ;-))
 Assumes that the programmer knows what he/she is doing and will assume
responsibility for his/her actions. (Some describe it as a “gun with which you can
shoot yourself in the foot.”)
 A bright red ’65 Corvette with a big block engine, manual transmission, optional
seat belt, and with fuzzy dice hanging from the rear view mirror.

C++
 A less permissive, yet open minded parent (e.g., ‘Thomas Huxtable’ ;-))
 Assumes that the programmer generally knows what he/she is doing, but provides
more checking by default. (“With C++ it’s harder to shoot yourself in the foot, but
when you do, you’ll blow off both of your legs” – Bjarne Stroustrup)
 A bright red 2000 Corvette with a 6 speed manual transmission, air bag, and heads
up display, many on board computers

Downloaded by Suman Saha (suman.struc@gmail.com)


lOMoARcPSD|5739061

Example C Program
/*
File: hello.c
Description: Prints a greeting to stdout.
Author: Douglas C. Schmidt <schmidt@uci.edu>
*/
#include <stdio.h>

int main (void) /* execution starts in main */


{
printf ("Hello world.\n");
return 0;
}

 All C programs must have a function in it called main


 Execution starts in function main
 C is case sensitive!
 Comments start with /* and end with */. Comments may span over many lines.
 C is a “free format” language.
 The #include <stdio.h> statement instructs the C compiler to insert the entire
contents of file stdio.h in its place and compile the resulting file.

Downloaded by Suman Saha (suman.struc@gmail.com)


lOMoARcPSD|5739061

Compiling a C Program
 On a personal computer using Visual Studio, you will create a .c file, e.g. hello.c (as
in the previous example). You will then compile it to produce a .exe file (e.g.,
hello.exe) and perhaps a .obj file (hello.obj). Consult your compiler documentation
(each one is different).

Downloaded by Suman Saha (suman.struc@gmail.com)


lOMoARcPSD|5739061

 In Visual Studio, do a File|New and you will see the following dialog...

Downloaded by Suman Saha (suman.struc@gmail.com)


lOMoARcPSD|5739061

 Here, you must first select Win32 Console Application. Then press the ... button
that appears to the right of the Location

Downloaded by Suman Saha (suman.struc@gmail.com)


lOMoARcPSD|5739061

 Now choose the location. You may need to create a folder on your file system. In
this case, I brought up the Windows NT explorer and created the new folder Ece11-
s98. Whatever you do, you should place the project in a place such that you can
easily find it later.

Downloaded by Suman Saha (suman.struc@gmail.com)


lOMoARcPSD|5739061

 Now that you have specified a Location, i.e. a folder where you want your project
created, you should see the following...

Downloaded by Suman Saha (suman.struc@gmail.com)


lOMoARcPSD|5739061

 Now, specify the name of your project. Visual Studio will create a folder underneath
the one you specified for Location by that name. This will also become the name of
your executable... Finally, click on the OK button to record your selection.

 It is very important that you do each of these initial steps in the exact order that I
have described above. That is, first specify that you want a Console Application,
then specify the location, then specify the project name. Do not forget any of these
steps. In other words, pay attention!!!

Downloaded by Suman Saha (suman.struc@gmail.com)


lOMoARcPSD|5739061

 As a result, you will see the following in Visual Studio


 Notice that the Workspace window now has three tabs. It has a ClassView tab, a
FileView tab, and an InfoView tab. Select (point the mouse to) the FileView tab.

Downloaded by Suman Saha (suman.struc@gmail.com)


lOMoARcPSD|5739061

 After selecting the FileView tab, you should see the following. Notice what appears
in the Workspace window...

Downloaded by Suman Saha (suman.struc@gmail.com)


lOMoARcPSD|5739061

 Now we need to create a source file and include it in the project. If we do the correct
sequence of steps, Visual Studio will automatically include the new file into the
project.
 Do a File|New and select Text File and then specify a File name. Do not accept the
default choice, Active Server Page (or whatever happens to be the default). Do not
select C/C++ Header File. Do not select C++ Source File. Select Text File. Do not
forget to type in the file name, i.e. hello.c (or whatever you wish to call it), but it
must end with .c !!! The file suffix tells Visual Studio what type of file this is so it
knows which compiler to invoke on it. Finally, click OK to select your choice.

Downloaded by Suman Saha (suman.struc@gmail.com)


lOMoARcPSD|5739061

 As a result of your efforts, you should see the following.

Downloaded by Suman Saha (suman.struc@gmail.com)


lOMoARcPSD|5739061

 I usually move the windows around at this point, adjusting the sizes.
 Type in the program.

Downloaded by Suman Saha (suman.struc@gmail.com)


lOMoARcPSD|5739061

 If you click on the + by Hello files under Workspace, you will see a list of all of the
files in your project
 Build your program by selecting the Build|Build menu option

Downloaded by Suman Saha (suman.struc@gmail.com)


lOMoARcPSD|5739061

 Run it (Build | Execute)

Now let's look at the files:


Hello.dsw: this is your WORKSPACE
Hello.dsp: this is the project build file
These two files, along with hello.c are worth saving.

Downloaded by Suman Saha (suman.struc@gmail.com)


lOMoARcPSD|5739061

 The program we just built is HELLO.EXE under the Debug directory

 Because it is a CONSOLE application, we can run it directly from our Windows NT


command (or Windows 95 Dos) prompt.
 When I typed hello below at the Dos prompt, Windows ran the program hello.exe.
Below you can see the results of this run.

Downloaded by Suman Saha (suman.struc@gmail.com)


lOMoARcPSD|5739061

Compiling Under UNIX


To compile a C program under UNIX, one may do it in the following ways:

% cc hello.c

This compiles the file and creates an executable named a.out

% cc hello.c -o hello

This compiles the file but renames the executable hello

% cc -c hello.c

This compiles the file but does not link it, thus producing an object module which may
be linked later on. That file, by default, is hello.o

% cc hello.o -o hello

This links the object file hello.o to create the executable hello

Under UNIX, suffixes are important (i.e., .c versus .o). They tell the compiler what type
of file they are, i.e. a C program file versus an object module.

% cc foo.o bar.o -o fubar

This links two separately compiled modules into an executable named fubar.

Downloaded by Suman Saha (suman.struc@gmail.com)


lOMoARcPSD|5739061

The compiling process -- overview

C Program -- foo.c

% cc -c foo.c

cpp -- C preprocessor

Handles #-directives; removes comments

foo.E

ccom -- C Compiler
compile program

C Optimizer
(optional)

foo.s

as -- assembler

foo.o

Downloaded by Suman Saha (suman.struc@gmail.com)


lOMoARcPSD|5739061

Compiling process -- translation phases (ANSI)

1. Physical source file characters are mapped to the source character set (including
new-line characters and end-of-file indicators) if necessary. Trigraph sequences are
replaced by corresponding single-character internal representations.

2. Each instance of a new-line character and an immediately preceding backslash


character (\) is deleted, splicing physical source lines to form logical source lines.

3. The source file is decomposed into preprocessing tokens and sequences of white-
space characters (including comments). A source file shall not end in a partial
preprocessing token or comment. Each comment is replaced by one space character.
New-line characters are retained.

4. Preprocessing directives are executed and macro invocations are expanded. A


#include preprocessing directive causes the named header or source file to be
processed from phase 1 through phase 4 recursively.

5. Each source character set member and escape sequence in character constants and
string literals is converted to a member of the execution character set.

6. Adjacent character string literal tokens are concatenated and adjacent wide string
literal tokens are concatenated.

7. White-space characters separating tokens are no longer significant. Each


preprocessing token is converted into a token. The resulting tokens are syntactically
and semantically analyzed and translated.

8. All external object and function references are resolved. Library components are
linked to satisfy external references to functions and objects not defined in the
current translation. All such translation output is collected into a program image
which contains information needed for execution in its execution environment.

Downloaded by Suman Saha (suman.struc@gmail.com)

You might also like