Beruflich Dokumente
Kultur Dokumente
Slide Set #1
Introduction to the C
Programming Language
General Philosophy of
Class
I believe strongly in learning by doing
not so much by listening to my lectures
certainly not by watching others do.
General Structure of
Class
The class sessions will be composed of:
Lectures, where the principles of computing are
explained.
Workshops, where a homework assignment will
be discussed at length.
Exams: Self-explanatory
Computers
First thing to always remember:
Computers are dumb creatures!
They only do exactly what you tell
them to do.
They cant figure out what you mean for
them to do like humans can (You know
what I mean ).
Computers
To get them to be smart, one must
communicate with them in a way
they understand what you want them
to do.
Unfortunately, they do not speak
English or French or Spanish
They speak terse, vaguely Englishlike languages that are very specific
in what they state.
Computers
To program a computer, one must understand
these languages.
These include C, C++, Java, Python, Pascal, Basic,
Fortran, Lisp, Prolog, , many others
Each has its strengths and its weaknesses.
Of course, we will only cover C in this class.
In my opinion, it is one of the hardest languages
to learn, but once learned, it is one of the most
useful
Plus, it provides a window into how computers
work that other languages do not.
Instructions
As the name implies, these describe to the
computer exactly what we want it to do.
Computers basically perform actions on data,
and move the data around to achieve an objective
A computer program tells the computer exactly
what actions to perform on exactly what data and
in what sequence.
The program does this by providing the
processor with instructions to execute and
the specific relevant data that are to be acted
upon and somehow transformed.
Instructions (continued)
Instructions (as well as data) are all long sequences
of very simple elements called bits that only take
on values of 0s and 1s. For example,
010001101110010011010011001100
If instructions, their specific pattern indicate a
specific action to be executed. This is the so-called
machine language
If data, it will represent a number, a symbol, a
memory location, a pixel color, a sound element or
some other representation of data to be
transformed somehow.
Instructions (continued)
The 0s and 1s are electrical voltage values at
the output of a transistor
The transistors either put out zero voltage (a 0), or
a saturated maximum voltage (interpreted as a 1)
This value can be easily and very rapidly switched
on (1) and off (0) by the hardware as the
instructions might require,
thereby executing large programs very quickly.
Computer Languages
OK, then how do we communicate with computers?
Humans think in terms of words, pictures, sounds,
concepts, etc., not in machine language (1s & 0s)
This makes it hard for humans and computers to
communicate.
Imagine writing computer programs in terms of
instructions as 0s and 1s!
This was actually done in the early days of computing
Computer Languages
Think of it as when having to speak to a
French person who speaks only French.
To communicate with her, we have to speak
to her in French;
otherwise, she will have no idea what you are
saying
And the desired results will not be achieved.
Modern Computer
Programming
Computer programming, therefore, an be defined
as the process of putting together a (possibly
long) sequence of statements that, when
compiled, linked and executed, will cause the
computer to act as desired and produce the
desired result.
Modern computing is very complex, and includes
graphic programming, internet programming and
writing programs that control devices in real time
But at their most fundamental level, these are all
simply a sequence of instructions to a processor, just
as you will learn to do in this class in C.
Stored-Program Digital
Computer
The oldest computers (1940s) were very large,
special-purpose, hard-wired computers that could only
perform the actions that they were designed to do
Any deviations would require extensive re-wiring.
Stored-Program Digital
Computer
Modern computer are, of course,
programmable.
The instructions (the stored program)
and the data are now stored in the same
memory elements (Random Access
Memory)
Well hopefully have time later in the
semester to discuss memory
organization very important!
Registers
Compiler
Application program
Preliminaries
You will learn how to program a computer this
semester
That is, create instructions in the C high level language
for a compiler to convert into machine language code
to be linked to other programs and/or libraries
for the processor to execute
and return the desired output
Programming Cycle
1) Compose the C statements that make up the
program (Edit)
2) Compile the C statements
1) (If it fails to compile, go back to 1)
Code::Blocks
Let us first download Code blocks
Its free!
Go to http://www.codeblocks.org
Follow instructions for downloading
Open the downloaded program and
get ready to use it.
Go to create new project
Click on console application.
Code::Blocks
Enter c
Enter the name of the project
Introduce HW #1
At this point, let us now do HW #1
Basic C Program
Structure
//Comment lines
The C Preprocessor
Header files
Macros
Comment Lines
Sometime we want to write things in the code
that are purely for our (i.e., human) benefit.
We dont want to compiler to translate them
Comments
Comment lines can be placed
anywhere within the program.
Well discuss this later when we get to
documentation of the code.
The C Preprocessor
It is sort of awkward to discuss this now,
before even discussing C statements, but they
do come first in the program
These are special commands called
preprocessor directives
Indicate certain things to be done to the
program code prior to compilation.
Include certain other files when compiling
Replace some code by other code
Header Files
The #include directive tells the
preprocessor to include the named
files in the compilation process.
Part of the afore-mentioned linking process
math.h
stdio.h
stdlib.h
string.h
( PI * (x) * (x) )
When called:
area = CIRCLE_AREA(4);
Scope
The scope of something in C means
exactly in what parts of the program
is this element valid.
What is the extent of the validity of a
statement?
C Statements
Composed of one or more instructions that
will later be compiled into machine language.
Generally conclude with a semicolon ;
Could be grouped together as a block of code
by using the right and left braces. { }
Example:
result = a + b + c * (d + e);
This means compute the mathematical
expression above and set the result to the
variable result.
Comments
// John Smith
// My first C program
These two lines are comments.
They are ignored by the compiler,
printf() command
printf() is the basic output
function in C
Defining Functions
Functions can be defined by the
programmer
Can be called later in the program to
execute their designed computations
Used to abstract long and/or complex
computations into a simple call.
It is the basis of programming in C
and C++
You will be defining many functions.
Much more about this later