Title: Variables and Operators
1Variables and Operators
2Outline
- Introduction
- Variables
- Operators
- Problem Solving
- Tying it All Together
- Additional Topics
- Summary
3Expanded Outline
- Introduction
- Variables
- Three Basic Types int, char, double Choosing
Identifiers, Scope, More Examples, - Operators
- Expressions and Statements, Assignment Operator,
Arithmetic Operators, Order of Evaluation,
Bitwise Operators, Relational Operators, Logical
Operators, Increment/Decrement Operators,
Expressions with Multiple Operators
4Expanded Outline
- Problem Solving
- Tying it All Together
- Symbol Table, Allocating Space for Variables,
Comprehensive Example - Additional Topics
- Variations of the Three Basic Types Literals,
Constants and Symbolic Values Storage Class,
Additional C Operators - Summary
5More C Stuff
- Comments / /
- Types
- Character
- Integers
- Warning Hex constants...Octal Constants
- Long and Short
- Signed and Unsigned
- Floating Point float and double
- Sizeof
- sizeof(int) / sizeof a type /
- sizeof x / sizeof a data object /
6C Types
- C "thinks" of 3 basic types char, int and float
in bytes, which are normally but not necessarily
8 bits
7sizeof
- Sizeof
- sizeof(int) / sizeof a type /
- sizeof x / sizeof a data object /
8Expressions
- A constant or variable by itself is an expression
- Combinations of constants and variables with
operators are also expressions - Examples
- 7 / Constant /
- x / Variable /
- x 7 / with operator /
- x x 7 / with operators /
- x x 7 / Simple statement /
9Operators
- Assignment Operator Evaluate expression on
right, store result in variable on the left - Add value on right side to variable on left
side - - Subtract value on right side from variable on
left side - Multiply variable on left side by value on
right side - Assignment expression itself has a value!
- a b c 0
10Is this legal?
11Will this work?
- int fact(int n)
-
- if(n lt 1)
- return 1
- else
- return n fact(n--)
12How about this?
- int fact(int n)
-
- if(n lt 1)
- return 1
- else
- return n fact(--n)
13Other Operators
- Binary Arithmetic - /
- Unary - --
- Bitwise ltlt gtgt
- Relational lt lt gt gt !
- Logical !
- Special ?
Pretty much the same as Java except there is no
boolean value
14Never Do This!
- return ((uCurlt1)?(uCur?01)(uCur4)?2(uCur1))
15Common Pitfalls
- / Java /
- int foo(int x)
-
- for(int i0iltMAXi)
-
- ...
- / C /
- int foo(int x)
-
- int i
- for(i0iltMAXi)
-
- ...
16Common Pitfalls
- int foo(int x)
-
- int a
- int b 7
- int c
- c 9
- int d
- Cannot mix code and declarations...
17Scope Storage Class
18Where can you put variables?
19Last Time...
- On stack
- Local variables (also allows recursion to work)
- In some "special" area
- Might be good for "global" variables (plus...)
- In with code
- Typically not done for several reasons
- On the heap
- Will come back to this point
20What if...
- ...parts of memory could be set to read-only?
- (After your program was loaded in)
- What could that be useful for?
21Where do you want to go today?
- Local variable inside a function
- Lasts only while the function is running
- Local variable inside a function
- Value persists throughout the life of the program
- Global variable visible to all functions within a
file - Global variable visible in more than one file
22Automatic Variables
- Local variable inside a function (lasts only
while thefunction is running) - auto keyword (never used!)
- Located on stack
- Storage Class AUTO
23Static Variables (I)
- Local variable inside a function (value persists
throughout the life of the program) - static keyword declared inside a function
- Located in static area
- Storage Class STATIC
24Static Variables (II)
- Global variable visible to all functions within a
file - static keyword declared outside of any function
- Located in static area
- Storage Class STATIC
25Static Variables (III)
- Global variable visible in more than one file
- Declared outside of any function in one file
- Declared extern in other files (or
functions/blocks) - Located in static area
- Storage Class STATIC
26Confused?
- int x
- static int y
- int main()
-
- static int z
- int w
- ...
-
27Questions?
28Memory Representation
x0000
Low Memory High Memory
- We typically draw diagrams representing the
memory of the computer, the memory of our
particular program or both as rectangles. - Our convention will be that "high-memory" will be
on the bottom and "low-memory" on top. - Typically these drawings are not to scale
xFFFF
29Typical Arrangement
x0000
Code
- Normally the actual program code (executable
instructions) is placed in low memory
xFFFF
30Typical Arrangement
x0000
Code
- Next we have an area for storage of constant data
Constant Data
xFFFF
31Typical Arrangement
x0000
Code
- Data that may be changed follows
Constant Data
Alterable Data
xFFFF
32Typical Arrangement
x0000
Code
- These three items comprise what is considered the
static area of memory. The static area details
(size, what is where, etc.) are known at
translation or compile time.
Constant Data
Static
Alterable Data
xFFFF
33Typical Arrangement
x0000
Code
- Immediately above the static area the heap is
located. - The heap can expand upward as the program
dynamically requests additional storage space - In most cases, the runtime environment manages
the heap for the user
Constant Data
Static
Alterable Data
Heap
xFFFF
34Typical Arrangement
x0000
Code
- Finally, the activation stack starts in high
memory and can grow down as space is needed. - Items maintained in the stack include
- Local variables
- Function parameters
- Return values
Constant Data
Static
Alterable Data
Heap
Stack
xFFFF
35Question?
- What is the activation stack particularly useful
for? - Polymorphism
- Recursion
- Fortran
- FIFO
- All of the above
- None of the above
36Typical Arrangement
x0000
- These items in the upper portion of the diagram
change during execution of the program. - Thus they are called dynamic
Code
Constant Data
Static
Alterable Data
Heap
Dynamic
Stack
xFFFF
Not just their value
37Typical Arrangement
x0000
Code
- The constant data area is where items such as the
"Hello" in a statement such as - printf("Hello")
- would be stored.
Constant Data
Static
Alterable Data
Heap
Dynamic
Stack
xFFFF
38const
- The const keyword is a type qualifier not a
storage class - The const keyword has NOTHING to do with the
constant area of memory!!! (sort of!) - It tells the compiler to not allow the programmer
to change the value of a variable once set. - Warning It is relatively easy to get around
this! - int foo(int n)
- const int k 8
- const int i n 7
- ...
- ...
Legal?
39auto
x0000
Code
- auto, short for automatic variables are those
that exist on the stack. The auto keyword is not
normally used. - Automatic means that space is allocated and
deallocated on the stack automatically without
the programmer having to do any special
operations.
Constant Data
Static
Alterable Data
Heap
Dynamic
Stack
xFFFF
40auto
- int foo(int z)
-
- int x
- ...
- if(x z)
-
- int y
- y ...
auto variables (implicitly)
41static
x0000
Code
- static variables exist in an area of memory set
aside for alterable (or readable/writeable) data - static can have different meanings depending on
where used.
Constant Data
Static
Alterable Data
Heap
Dynamic
Stack
xFFFF
42static
- Variables
- A global variable is stored in the static area of
memory - A variable that is global just to the functions
in one file must be designated using static - A variable that is local to a function AND
retains its value (i.e. persistence) must be
labeled static - Functions
- A function labeled as static is visible only
within the file where it is defined
43static
- int i / Global variable...stored in
- static area (storage class is
- static
- /
- int foo(...)
-
- ...
-
- int main()
-
- ...
44Multiple Files
foo.c
bar.c
baz.c
Linker
Advantage For maintenance only have to
recompile affected files
a.out
45Multiple File Scope
- Scope can be global within a file
- Scope can be global across all files
- Scope can be defined illegally!
- Actually Linkage...
- None
- Internal
- External
- Note global here means visible in more than one
function
46Scope can be global within a file
These are different variables with Global scope
within their respective files
- static int i
- int foo(...)
-
- ...
-
- int bar(...)
-
- ...
-
- static int i
- int main()
-
- ...
-
- int baz(...)
-
- ...
47Scope can be global across all files
int i
extern int i
void f() extern int i ... ...
These are the same variable!
48Scope can be defined poorly!
int i
int i
extern int i
49Scope can be defined poorly!
int i
int i 42
extern int i
50Scope can be defined illegally!
int i 42
int i 42
extern int i
51According to the C89 Standard...
- There are 4 types of scope
- Prototype Scope
- Just applies to prototypes
- Means this is illegal
- void func(int i, int i)
- File Scope
- Declared outside any function
- Function Scope
- only applies to labels!!!
- Block Scope
- includes function parameters
52Scope vs. Lifetime
- Scope
- File Scope
- Block Scope
- Lifetime
- Life of program
- Life of block
53Scope vs. Lifetime
- Scope
- File Scope
- Block Scope
- Lifetime
- Life of program
- Life of block
Can be overridden with static
54Memory
- Many languages have fixed mappings between scopes
and lifetimes - In C, we have the option to decide...
- void foo(void)
- int x
- static int a
- x 42
- When the function goes away, x and its value go
away since they were on stack. - Setting a value into a static variable (e.g. a)
means that the value is stored in a static area
and will persist throughout the entire execution
of the program
55Static Initialization
- void foo(void)
-
- int x 10
- static int y 20
- Both variables are initialized as allocated
- x is initialized to 10 every time function foo
runs - y is initialized when program starts
56Static Initialization?
- void foo(void)
-
- int x 10
- static int y
- y 20
- This would defeat purpose of static variable
having persistence!
57Static Initialization
- void foo(void)
-
- int x 10
- static int y 20
- printf(x d y d\n, x, y)
- x 40
- y 30
-
- What prints the first time foo is called?
- What prints the second time?
58Static also applies to functions
- Static has different meanings depending on where
used - include...
- define
- int x
- static int y / What does this mean? /
- void foo(void)
- static int z / What does this mean? /
- ...
-
- static void bar(void) / What does this mean?/
59Questions?
60Other goodies
- register
- Suggests to compiler that this variable should be
kept in a register - Cannot get address of variable declared register
- Not as important as it once was with modern
compilers - volatile
- Type qualifier
- Tells compiler that value in this variable may
change on its own! - Used in
- shared memory applications
- Memory mapped I/O
61volatile
- Used to indicate possibility that this variable
might be changed by someone else -
62"Pull n into a register?"
- Typical Assembly Language Sequence
- LDI R2, KBSR Get status
- ... Some code which
- ... doesn't access
- ... KBSR or KBDR
- LDI R2, KBSR
- BRnzp someplace
- KBSR .FILL xFE00
-
- Can this be optimized???
63"Pull n into a register?"
- Typical Assembly Language Sequence
- LDI R2, KBSR Get status
- ... Some code which
- ... doesn't access
- ... KBSR or KBDR
- LDI R2, KBSR
- BRnzp someplace
- KBSR .FILL xFE00
-
64But...
- Volatile storage class warns compiler to assume
value may be changed by something else.
65Conversely
- What if a particular variable needs to be
accessed very frequently? - Usually the compiler will recognize this and keep
the variable in a register - int i
- for(i0 ilt100000 i)
-
- / loop /
-
66Conversely
- To suggest to the compiler that this is a good
idea - register int i
- for(i0 ilt100000 i)
-
- / loop /
-
- The register keyword suggests to the compiler
that a variable should be maintained in a
register as opposed to memory.
67register
- Since putting one variable in a register can
speed up execution putting more variables in
registers can only make things better!?!?!? - Fact Overuse of register can adversely affect
the compilers register allocation algorithm - Don't try and outwit the compiler!
- In some isolated cases it is possible to improve
performance i.e. dumb compiler
68register
- Also, cannot get the address of a variable
declared to be in the register storage class -
- int i, ip
- ip i / (more later) /
- This will generate a warning and force the
variable to be put in memory. - In most cases use of register is discouraged
(often compilers ignore it!)
69(No Transcript)
70- int x
- int foo(int a)
- int main()
-
- int y
- volatile int z
- register int w
- y z w 0
- foo(4)
- return 0
- int foo(int a)
-
- static int p
- int q
- static int r 7
- char pointer "Hello"
- char array "World"
- const int f a 7
- array3 'x'
- p q r f
- pointer array
- if(a 0)
- return 1
- else
- foo(a-1)
- return 1
71Compiler Magic
- The compiler has the job of converting your C
program into assembly code - Thus it must convert the symbolic variable names
into addresses - How does it keep track of what is where?
72Symbol Table
- For each variable keeps track of
- Type
- Location (as an offset)
- Scope
- Other info (const, etc.)
73Offset?
x0000
Code
- Assembly code written by a compiler usually looks
a little different from assembly code written by
hand - Registers are dedicated to point to key areas of
memory - R4 is the Global Pointer
R4
Constant Data
Alterable Data
Heap
Stack
xFFFF
74Keeping Track of auto variables
x0000
Code
- Stack pointer is obvious but the compiler writer
needs more info... - Where is the activation stack frame?
R4
Constant Data
Alterable Data
Heap
R6
Stack
xFFFF
75Why do we care?
x0000
Code
- We would like to know where a variable is
throughout the execution of a function - But, wait you say, I can just reference the
variable from the stack pointer - HA!
R4
Constant Data
Alterable Data
Heap
R6
Stack
xFFFF
76Frame Pointer
- int f(int a, int b)
- int c
- c a b
- return c
-
- int main()
- int x
- int y 4
- x f(7, y)
- printf("d\n", x)
- return 0
- What do we need to keep track of?
77Frame Pointer
x0000
Code
- The Frame Pointer designates a fixed spot in the
activation stack which can be used as a reference
throughout execution of the function. - But can't we just use the stack pointer?
R4
Constant Data
Alterable Data
Heap
R6
Stack
R5
xFFFF
78Questions?
79(No Transcript)