Lecture
#4: The C Language
CSE
5A
Bob
Pitha
class business
No
office hours this afternoon
will start
on Wednesday
A
course outline will be posted on the web page by Monday.
Discussion
section is not mandatory, only recommended
a second example
Heres
another example C program
its not
much more sophisticated
it also
doesnt do much of any practical use

new concepts
Variables
Loops
variables
How
we manipulate data in our program
Name
associated with some memory
Has
a specific data type
In
this program:
a variable
named i is used to hold an integer value
as the
program runs, is value goes from 0 to 9

loops
A
way of repeating the same instructions over and over
C
provides some flexible ways of deciding how many times to loop, or when to exit
a loop
second example

So
lets look at this example again
second example

Heres
that include directive again.
like before,
this allows printf to work
well just
do it now, without worrying about why
second example

And
like before, this line introduces our main function.
remember
that main is where the program starts running
second example

These
braces enclose blocks of statements.
a function
is an example of a block
second example

This
line introduces a variable.
its called
a declaration
it tells the
compiler that well be using a variable
named i
it holds an
integer
second example

Here
is a loop
it will
execute its contents 10 times
each time, i
will have a different value
from 0 to 9
second example

This
statement is the contents of the loop
so it gets
executed 10 times
i
is 0
i
is 1
i
is 2
etc.
we could have written it like this

ok, whats going on here?
Weve
just seen 2 simple programs
These
are C programs
not machine
language
the C
compiler is used to turn them into something the machine understands
The
C compiler is itself a program
it reads the
C program you write
it writes
out (hopefully) a program the computer can run
remember what a program is
The
real program is just numbers
A
C program is a sequence of C instructions
we can
define variables to hold our data
we write C
statements to do things with our data
we can group
our statements into functions
so whats a compiler?
A
compiler is a program that converts a C program into machine language
remember,
the computer can only run machine language
so lets look at the whole process
Write
a program
text editor
Compile
the program
C compiler
Will do one
of two things:
print error
messages
produce an
executable program
Run
the program
lets talk about this variable concept
Variables
are central to almost any C program
They
are your programs data
the
information your program works with
If
you want to store any data for use elsewhere in the program, you need to use a
variable
variable declarations
Before
you can use a variable, you must declare it.
a
declaration introduces a variable name
associates
it with a data type
sets some
optional characteristics called qualifiers
variable names
Identifiers
used for all
sorts of names (e.g. function names)
rules
letters,
digits, underscore
i
CSE_5a
a_VERY_long_variable_name_that_isnt_very_useful
fahrenheit
first
character cannot be a digit
5a_cse is
not legal
case
sensitive
CSE_5a is
different from cse_5a
some
identifiers are already taken: keywords