Patt Patel CH 12
Patt Patel CH 12
Variables and
Operators
Basic C Elements
Variables
• named, typed data items
Operators
• predefined actions performed on data items
• combined with variables to form expressions, statements
12-2
Data Types
C has three basic data types
12-3
Variable Names
Any combination of letters, numbers, and underscore (_)
Case matters
• "sum" is different than "Sum"
12-4
Examples
Legal
i
same identifier
wordsPerSecond
words_per_second
_green
aReally_longName_moreThan31chars
aReally_longName_moreThan31characters
Illegal
10sdigit
ten'sdigit
done? reserved keyword
double
12-5
Literals
Integer
123 /* decimal */
-123
0x123 /* hexadecimal */
Floating point
6.023
6.023e23 /* 6.023 x 1023 */
5E12 /* 5.0 x 1012 */
Character
'c'
'\n' /* newline */
'\xA' /* ASCII 10 (0xA) */
12-6
Scope: Global and Local
Where is the variable accessible?
Global: accessed anywhere in program
Local: only accessible in a particular region
12-7
Example
#include <stdio.h>
int itsGlobal = 0;
main()
{
int itsLocal = 1; /* local to main */
printf("Global %d Local %d\n", itsGlobal, itsLocal);
{
int itsLocal = 2; /* local to this block */
itsGlobal = 4; /* change global variable */
printf("Global %d Local %d\n", itsGlobal, itsLocal);
}
printf("Global %d Local %d\n", itsGlobal, itsLocal);
}
Output
Global 0 Local 1
Global 4 Local 2
Global 4 Local 1
12-8
Operators
Programmers manipulate variables
using the operators provided by the high-level language.
12-9
Expression
Any combination of variables, constants, operators,
and function calls
• every expression has a type,
derived from the types of its components
(according to C typing rules)
Examples:
counter >= STOP
x + sqrt(y)
x & z + 3 || 9 - w-- % 6
12-10
Statement
Expresses a complete unit of work
• executed in sequential order
12-11
Operators
Three things to know about each operator
(1) Function
• what does it do?
(2) Precedence
• in which order are operators combined?
• Example:
"a * b + c * d" is the same as "(a * b) + (c * d)"
because multiply (*) has a higher precedence than addition (+)
(3) Associativity
• in which order are operators of the same precedence combined?
• Example:
"a - b - c" is the same as "(a - b) - c"
because add/sub associate left-to-right
12-12
Assignment Operator
Changes the value of a variable.
x = x + 4;
12-13
Assignment Operator
All expressions evaluate to a value,
even ones with the assignment operator.
12-14
Arithmetic Operators
Symbol Operation Usage Precedence Assoc
* multiply x * y 6 l-to-r
/ divide x / y 6 l-to-r
% modulo x % y 6 l-to-r
+ addition x + y 7 l-to-r
- subtraction x - y 7 l-to-r
12-15
Arithmetic Expressions
If mixed types, smaller type is "promoted" to larger.
x + 4.3
if x is int, converted to double and result is double
12-16
Bitwise Operators
Symbol Operation Usage Precedence Assoc
~ bitwise NOT ~x 4 r-to-l
<< left shift x << y 8 l-to-r
>> right shift x >> y 8 l-to-r
& bitwise AND x & y 11 l-to-r
^ bitwise XOR x ^ y 12 l-to-r
| bitwise OR x | y 13 l-to-r
12-17
Logical Operators
Symbol Operation Usage Precedence Assoc
! logical NOT !x 4 r-to-l
&& logical AND x && y 14 l-to-r
|| logical OR x || y 15 l-to-r
12-18
Relational Operators
Symbol Operation Usage Precedence Assoc
> greater than x > y 9 l-to-r
>= greater than or equal x >= y 9 l-to-r
< less than x < y 9 l-to-r
<= less than or equal x <= y 9 l-to-r
== equal x == y 10 l-to-r
!= not equal x != y 10 l-to-r
12-19
Special Operators: ++ and --
Changes value of variable before (or after)
its value is used in an expression.
12-20
Using ++ and --
x = 4;
y = x++;
Results: x = 5, y = 4
(because x is incremented after assignment)
x = 4;
y = ++x;
Results: x = 5, y = 5
(because x is incremented before assignment)
12-21
Practice with Precedence
Assume a=1, b=2, c=3, d=4.
x = a * b + c * d / 2; /* x = 8 */
same as:
x = (a * b) + ((c * d) / 2);
12-23
Local Variable Storage
Local variables are stored in an
activation record, also known as a stack frame.
12-24
Allocating Space for Variables
Global data section 0x0000
• All global variables stored here
(actually all static variables)
• R4 points to beginning instructions PC
Run-time stack R4
global data
• Used for local variables
• R6 points to top of stack
• R5 points to top frame on stack
• New frame for each block R6
(goes away when block exited) run-time R5
Offset = distance from beginning stack
of storage area
• Global: LDR R1, R4, #4 0xFFFF
12-26
Example: Compiling to LC-3
#include <stdio.h>
int inGlobal;
main()
{
int inLocal; /* local to main */
int outLocalA;
int outLocalB;
/* initialize */
inLocal = 5;
inGlobal = 3;
/* perform calculations */
outLocalA = inLocal++ & ~inGlobal;
outLocalB = (inLocal + inGlobal) - (inLocal - inGlobal);
/* print results */
printf("The results are: outLocalA = %d, outLocalB = %d\n",
outLocalA, outLocalB);
}
12-27
Example: Symbol Table
12-28
Example: Code Generation
; main
; initialize variables
AND R0, R0, #0
ADD R0, R0, #5 ; inLocal = 5
STR R0, R5, #0 ; (offset = 0)
12-29
Example (continued)
; first statement:
; outLocalA = inLocal++ & ~inGlobal;
LDR R0, R5, #0 ; get inLocal
ADD R1, R0, #1 ; increment
STR R1, R5, #0 ; store
12-30
Example (continued)
; next statement:
; outLocalB = (inLocal + inGlobal)
; - (inLocal - inGlobal);
LDR R0, R5, #0 ; inLocal
LDR R1, R4, #0 ; inGlobal
ADD R0, R0, R1 ; R0 is sum
LDR R2, R5, #0 ; inLocal
LDR R3, R5, #0 ; inGlobal
NOT R3, R3
ADD R3, R3, #1
ADD R2, R2, R3 ; R2 is difference
NOT R2, R2 ; negate
ADD R2, R2, #1
ADD R0, R0, R2 ; R0 = R0 - R2
STR R0, R5, #-2 ; outLocalB (offset = -2)
12-31
Special Operators: +=, *=, etc.
Arithmetic and bitwise operators can be combined
with assignment operator.
Statement Equivalent assignment
x += y; x = x + y;
x -= y; x = x - y;
x *= y; x = x * y;
x /= y; x = x / y;
x %= y; x = x % y; All have same
precedence and
x &= y; x = x & y;
associativity as =
x |= y; x = x | y;
and associate
x ^= y; x = x ^ y; right-to-left.
x <<= y; x = x << y;
x >>= y; x = x >> y;
12-32
Special Operator: Conditional
Symbol Operation Usage Precedence Assoc
?: conditional x?y:z 16 l-to-r
y z
1 0
x
12-33