Professional Documents
Culture Documents
C Programming Lecture Notes
C Programming Lecture Notes
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)
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
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
1011000111000101
Break this up into groups of 4:
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:
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
Language Taxonomy
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
History of C
1970'S C
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
Example C Program
/*
File: hello.c
Description: Prints a greeting to stdout.
Author: Douglas C. Schmidt <schmidt@uci.edu>
*/
#include <stdio.h>
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).
In Visual Studio, do a File|New and you will see the following dialog...
Here, you must first select Win32 Console Application. Then press the ... button
that appears to the right of the Location
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.
Now that you have specified a Location, i.e. a folder where you want your project
created, you should see the following...
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!!!
After selecting the FileView tab, you should see the following. Notice what appears
in the Workspace window...
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.
I usually move the windows around at this point, adjusting the sizes.
Type in the program.
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
% cc hello.c
% cc hello.c -o 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.
This links two separately compiled modules into an executable named fubar.
C Program -- foo.c
% cc -c foo.c
cpp -- C preprocessor
foo.E
ccom -- C Compiler
compile program
C Optimizer
(optional)
foo.s
as -- assembler
foo.o
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.
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.
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.
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.