02_expressions_summer
@NCStateECE
ECE 209
Computer Systems Programming
Building Blocks
LiteralsVariables
Operators
Expression
Statement
Function
Program
Unit of work
Unit of modularity
Values
(Data)
Source Code File Unit of compilation
Nouns
Verbs
Phrase
Statement
Paragraph
Document
Chapter
Building Blocks
LiteralsVariables
Operators
Expression
Statement
Function
Program
Nouns
Verbs
Phrase
Statement
Paragraph
Document
Unit of work
Unit of modularity
Values
(Data)
Source Code File
Chapter
Unit of compilation
Data vs. Information
Information
Data
Program
Data
Information
Types of Data
C Representation Inside the CPU
Integer 123
0xab10
binary
(2’s comp)
Floating-point 12.34
0.999
123.
2.3e44
8e-6
binary
(IEEE fl pt)
Text ‘a’
“abc”
binary
(ASCII)
Literals
Integer Literals
Decimal Integer
123
Hexadecimal Integer
0x123
0xabc
0xABC
0XaBc
Character
‘a’
‘1’
‘\n’
‘ ‘
‘\0’
Boolean
true
false
Value is ASCII code of
character.
INT_MAX What about negative integers? Floating-point Literals Decimal Point DBL_MAX Exponent 6.023E26 1×10$ 3.0×10′( 6.023×10($ String Literal “abc” Sequence of characters. Photo by Ruchindra Gunasekara on Unsplash A variable is Each variable is a “box” In C, every variable has a type. A variable’s type tells you How is data encoded? How are bits interpreted? C Data Types int double char signed integer floating-point single character What about string? Later. C Data Type Sizes int double char 4B 8B 1B 1 word 2 words 1 word x86 LC-3 Size is platform-specific, not standard. In C, every variable has a Variable = Memory Location -397 3.14159 ‘s’ int double char A memory location always In C, every variable has a name. A variable’s name is a user-defined symbol Rules: Declaration type name ; int count; count sum mode Declaration int count; count sum mode Declaration causes the compiler The location is not important (for now). Initialization type name = value; 0 200.0 101 int count = 0; count sum mode Value must match the type. Can declare/initialize multiple variables of 0 int count = 0, limit = 10, i; count 10limit i Assignment name = value; 0count Value must match the type. count = 3; 3 3count Assignment name1 = name2; 10a Value must match the type. int a = 10; 20b 20a 20b Assignment 10aint a = 10; x 3a 3.5 x Might get a compiler warning. Audience Q&A Session Expression So far: • How to represent data (literals) How do we process data? Operators Operators a+b a&b a<>b a&&b a==b Arithmetic (floating-point) Operator Meaning Example Value + Add a + b 3.4 double a = 2.6, b = 0.8; Arithmetic (integer) Operator Meaning Example Value + Add a + b 14 % Modulo (remainder) a % b 4 int a = 9, b = 5; discards remainder of division Arithmetic Example Value b / c 1.0 c’s value is cast to double a / c 1 integer division a % b ??? illegal b – a -4.0 a’s value is case to double If types don’t match, cast (convert) to representation with larger range. Arithmetic To force a conversion, use the cast operator. a / (int) b (double) a / c Convert b to int, then use integer division. Convert a to double, then use floating-point division. Bitwise (integer) Operator Meaning Example Value Relational (integer, floating-point) Operator Meaning Example Value int a = -25, b = 62; Operators a+b a&b a<>b a&&b a==b Practice ASCII code for ‘0’ is 48 or 0x30. fee – fie ___________ foe – fee ___________ fum + fie ___________ fum / fee ___________ foo + foe ___________ Practice fee – fie ____5______ foe – fee __15.0_____ fum + fie _53_0x35_’5’_ fum / fee ____4______ foo + foe ____525.___ In C, an expression represents a value. Expression + Literal Variable expr OP expr OP expr binary op unary op a + b * c a + b * c a + b * c 2 + 3×4 = 24 (2 + 3)×4 = 20 plus, minus, logical not, bitwise not +, -, !, ~ multiply, divide, modulus *, /, % add, subtract +, – bitwise left shift, bitwise right shift <<, >> comparisons >, >=, <, <=
comparisons ==, !=
bitwise AND &
bitwise XOR (exclusive-OR) ^
bitwise OR |
logical AND &&
logical OR ||
Precedence
___ op1 ___ op2 ___
Operator from higher row
is applied first.
a + b * c
a + b * c a + b * c
expr OP expr expr OP expr
2 + 3×4 = 24
C D
If you want to override precedence,
use parentheses.
(a + b) * c
What if operators on the same row?
a * b % c
2×(8 mod 3) = 4 (2×8) mod 3 = 1
What if operators on the same row?
(a * b) % c
Group left to right,
except for unary and assignment.
Assignment is an Operator
var = expr This is an expression!
Value is the right-hand side.
Lefthand side is restricted. Must be an Lvalue.
For now, this must be a variable name.
3 = a
Doesn't make sense, because 3 is not
a variable, and does not have a
memory location.
NOTE: No semicolon.
A semicolon at the end makes it a statement.
plus, minus, logical not, bitwise not
cast expression to type
+, -, !, ~
(type) expr
multiply, divide, modulus *, /, %
add, subtract +, -
bitwise left shift, bitwise right shift <<, >> comparisons >, >=, <, <=
comparisons ==, !=
bitwise AND &
bitwise XOR (exclusive-OR) ^
bitwise OR |
logical AND &&
logical OR ||
assignment =
Precedence
int a = -25, b = 62, c = 10;
Expression Interpreted as... Result
a = b + c a = (b + c) a changed to 72,value of expression is 72
a + b = c (a + b) = c ILLEGAL
a + (b = c) a + (b = c) b changes to 10,value of expression is -15
This is rarely (but occasionally) useful, but it's important because a
common error is to write a = b when you mean a == b,
and both are legal expressions.
Combined Assignment Ops
Expression Interpreted as...
var += expr var = var + (expr)
var *= expr var = var * (expr)
var &= expr var = var & (expr)
etc.
Programming is not math!
Looks like math...
but it's not.
a = a+5
In math, this can't possibly be true.
Makes no sense.
In a program, makes perfect sense.
"Change a to become the current value
of a plus 5."
Looks like math...
but it's not.
a <= 10
In math, this would be ≤.
Don't write that in a C program.
The operator requires two characters.
Looks like math...
but it's not.
10 < x < 20
Suppose the value of x is 5.
Is this expression true or false?
Math: false
C program: true
(10 < x) < 20
0 < 20
1
Probably meant: (10 < x) && (x < 20)
Building Blocks
LiteralsVariables
Operators
Expression
Statement
Function
Program
Unit of work
Unit of modularity
Values
(Data)
Source Code File Unit of compilation
INT_MIN
123.
123.1
0.45
.45
DBL_MIN
1e6
3e-2
“This is a test.”
“First line.\nSecond line.”
“x”
Starts and ends with double-quote (“).
May contain spaces, linefeeds, etc.
Ends with a null character (‘\0’).
a place to store data.
with a specific shape and size.
what kind of data can be stored there.
What operations can be performed on this data?
How much memory space is required?
(integer)
memory location.
contains a value (bits). An empty
box does not mean “empty,” it
means “unknown.”
that represents its storage location.
• Only contains letters, digits, underscore.
• Cannot begin with a digit. (Avoid starting with underscore.)
• Case-sensitive.
• Up to 31 characters.
To create a variable, declare it.
double sum;
char mode;
double sum;
char mode;
to allocate space for the variable.
Specify an initial value to be stored.
double sum = 2e2;
char mode = ‘e’;
the same type in one declaration.
To change a variables value, use the
assignment operator (=).
Assign (copy value) from one variable to another.
int b = 20;
a = b;
If the types don’t match, compiler will implicitly convert.
double x = 3.5;
a = x;
3.5
• How to store data (variables)
• How to copy data (assignment)
Arithmetic Bitwise Logical Relational
a-b
a*b
a/b
a%b
-a
a|b
a^b
~a
a||b
!a
ab
a<=b
a>=b
a!=b
– (unary) Change sign -a -2.6
– Subtract a – b 1.8
* Multiply a * b 2.08
/ Divide a / b 3.25
– (unary) Change sign -a -9
– Subtract a – b 4
* Multiply a * b 45
/ Divide a / b 1
fractional
result
int a = 9
double b = 5.0;
int c = 5;
a / b 1.8 a’s value is cast to double
int a = 9
double b = 5.0;
int c = 5;
& AND a & b 0x8b
| OR a | b 0xaf
^ XOR a ^ b 0x24
~ NOT ~a 0x..fff54
>> Right shift a >> 2 0x2a
<< Left shift a << 2 0x2ac
int a = 0xab, b = 0x8f;
depends on
size of int
...0010101011 ...0010001111
a b
Bitwise (integer)
int a = 0xab, b = 0x8f;
"Why hex? Does bitwise only work on hex integers?"
Bitwise (integer)
int a = 0xab, b = 0x8f;
"Why hex? Does bitwise only work on hex integers?"
NO!
There's no such thing as a "hex integer" or a "decimal integer."
There are only integers.
We can write integer values as decimal or hex. There is no difference.
(Always binary in the CPU.)
Bitwise (integer)
Operator Meaning Example Value
& AND a & b 139
| OR a | b 175
^ XOR a ^ b 36
~ NOT ~a -172
>> Right shift a >> 2 42
<< Left shift a << 2 684
int a = 171, b = 143;
...0010101011 ...0010001111
a b
Logical (integer)
Operator Meaning Example Value
&& Logical AND a && b false (0)
|| Logical OR a || b true (1)
! Logical NOT
!a
!b
true (1)
false (0)
int a = 0, b = 0x8f;
Entire value is treated as true (non-zero) or false (zero).
== is equal to a == b false (0)
!= is not equal to a != b true (1)
> is greater than a > b false (0)
< is less than a < b true (1)
<= is less than or equal to a <= b true (1)
>= is greater than or equal to a >= b false (0)
Arithmetic Bitwise Logical Relational
a-b
a*b
a/b
a%b
-a
a|b
a^b
~a
a||b
!a
ab
a<=b
a>=b
a!=b
fee 10
fie 5
foe 25.0
fum ‘0’
foo 5e2
fee 10
fie 5
foe 25.0
fum ‘0’
foo 5e2
ASCII code for ‘0’ is 48 or 0x30.
expr OP expr expr OP expr
cast expression to type
(type) expr