Lecture #7: Declarations
CSE
5A
Bob
Pitha
administrivia page 1
Class
web page has been updated recently
homework #1
course
outline
Homework
issues
you can use
the lab
you can use
your home computer
you can use
your friends computer
you can use
any computer you like
but if its
not the labs computer, its up to you to get and use the software
administrivia page 2
Homework
(continued)
how to turn
homework in (again)
best option:
leave it in the directory I specified in the assignment
H:\Homework\HW1
second best
option: e-mail the files to cs5a@ieng9.ucsd.edu
third best
option: floppy disk
marked with
your name and class account name
administrivia page 3
Quiz
Friday, not
Wednesday, during class
will cover
everything up through today
general
programming concepts
data types
constants
declarations
declarations
What
is an declaration?
how we
introduce an identifier
specifies
the interpretation and attributes of an identifier
in other
words, tells the computer what an identifier means
variable
function
various
other things
lets focus
on variables right now
variables
So,
what is a variable?
Sometimes
called an object
though less
so these days, since its not the same as the objects in object-oriented
programming
your text
defines object as a named region of storage
ANSI
standard:
A region of data storage in the execution environment, the contents of
which can represent values.
objects are composed of contiguous sequences of
one or more bytes, the number, order, and encoding of which are either
explicitly specified or implementation-defined.
variables
So
variables are memory regions you can use to hold data while your program is
running.
For
your convenience, you give them names (instead of having to know the address)
Because
different types of data are different sizes, the computer needs to know what
type each variable is
so it can
reserve the memory you need
back to declarations
So
all this means that before you can use a variable, you need to tell the
compiler:
its name
its data
type
anything
else that the compiler needs to know
storage
class
right now,
well just stick to the first two
why declare variables?
Easier
for the compiler
it doesnt
have to guess what you want
it can
reserve the right amount of memory
machine code
can be different for some different data types
Discipline
forces you
to think about what you need
this is a
subtle issue, well revisit it
declaration rules
Each
declaration consists of:
a data type
int, double,
char
a list of
variable names
remember the
rules for identifiers
letters,
digits, underscore
cant start
with a digit
case
sensitive
optional
initialization
ends with a
semicolon
simple declaration examples

initialization?
Sets
the initial value of the variable
this doesnt
mean it cant change subsequently
must be a
constant expression
a constant
an
expression whose value is constant

initialization and data types
The
initializer must be:
of the same
data type as the variable
of a data
type the compiler can convert into the data type of the variable
Good:
Less good:
placement of declarations
Placement
in program is significant
At beginning
of statement block
this is what
weve seen so far
Outside a
function - global variable
Parameter
declaration - in function declaration
variables in statement blocks
What
statement blocks have we seen so far?
after all,
we havent yet even talked about statement blocks!

statement blocks
are surrounded by { }
another statement block

back to variables and statement blocks
So
any statement block can start with some variable declarations
but those
variables are only available to the code inside the statement block
this is a
concept called scope
global variables
Variables
that are defined outside of any functions
Potentially
available to all functions
there are
some rules here
Analogy:
account
slips: local variables
web page:
global variable
function parameters
We
havent talked about functions much yet
Parameters
are data you give to a function each time it is invoked
remember my
driving analogy of a function
adjust speed
to X mph
the same
instructions, no matter what X is each time
remember how
we have used printf:



well talk about
parameters more when we get into functions