Professional Documents
Culture Documents
8.3
Chapter Overview
Christian Jacob
8.4
Arithmetic Expressions
8.4.1 8.4.2 8.4.3 Standard Arithmetic Operators Assignments and Compound Assignments Operator Priorities
8.5
Symbolic Constants
8.5.1 8.5.2 Declaration of Symbolic Constants Hexadecimal and Octal Constants
8.6 8.7
Chapter Overview
Page 3
Christian Jacob
8.1
try
typedef typeid typename union unsigned using virtual void volatile wchar_t while
return short signed sizeof static static_cast struct switch template this throw true
main
mutable namespace new operator private protected public register
delete
goto
First
Back
TOC
Prev Next
Last
Page 4
Christian Jacob
8.2
an actual value (a number, a character, ...) contains a value (= constant) refers to a variable, points to a variable refers to a level-1 pointer
Prev Next Last
Page 5
Christian Jacob
Constant:
Const_Name
Example:
3.14159
internal object
internal
representation
Variable:
Name
reference (address)
internal
representation
lvalue
rvalue
First
Back
TOC
Prev Next
Last
Page 6
Christian Jacob
8.2.2 Variables
Variables are data objects with the following properties: Identier, name - Dened in its declaration (example: int size = 12) Type - Dened in its declaration (example: int size = 12) Internal object = address of the manipulable memory section (lvalue) Value = alterable contents of the addressed memory section - Its interpretation depends on its type. - Its value can be changed by assignments (=) or specic operators (++). Lifespan: - Dynamically dened by the program structure (see: functions).
First Back TOC Prev Next Last
Page 7
Christian Jacob
bool correct_input; // true or false int number_of_items; // number of items float width, height; // width and height
Initializing Variables basic_variable_declaration ::= type {name_init,}*name_init; name_init ::= { name | name ( init ) | name = init }
First
Back
TOC
Prev Next
Last
Page 8
Christian Jacob
8.3
int normal_integer; long int long_integer; short int short_integer; unsigned short int unsigned_short_int;
Floating Point Numbers
char single_character;
First Back TOC Elementary C++ Data Types Prev Next Last
Page 9
Christian Jacob
8.3.1 Integers
Integers (= whole numbers) have no fractional part or decimal point. Range of values
32 bit = 4 bytes (UNIX, Mac) 16 bit = 2 bytes (PC DOS)
Types of Integers int: long int: short int: unsigned: the most efcient size for the machine is chosen (2 or 4 bytes) extra storage for integer (4 bytes guaranteed) reduced storage (2 bytes) uses all bits for the number (for 1 byte: range 0 to 65,535)
Elementary C++ Data Types Prev Next Last
First
Back
TOC
Page 10
Christian Jacob
1. For further details consult: S. Qualline, Practical C++ Programming, OReilly, 1997, Chapter 19.
First
Back
TOC
Prev Next
Last
Page 11
Christian Jacob
Types of Floats float: double: long double: normal precision (usually 4 bytes = 32 bits) twice the range and precision of float (usually 8 bytes = 64 bits) extended precision (at least 8 bytes)
First
Back
TOC
Prev Next
Last
Page 12
Christian Jacob
long int
int
char
short int
First
Back
TOC
Prev Next
Last
Page 13
Christian Jacob
Operator + * / %
Floating-point example 5.5 + 2.2 7.7 5.5 - 2.2 3.3 5.5 * 2.2 12.1 5.5 / 2.2 1.375 5.5 % 2.2 type error
First
Back
TOC
Prev Next
Last
Page 14
Christian Jacob
Operator + * / %
Integer / oating-point example 5 + 2.2 7.7 5.0 - 2 3.0 4 * 2.2 8.8 6.0 / 2 3.0 5.0 % 2.0 type error
First
Back
TOC
Prev Next
Last
Page 15
Christian Jacob
Special volatile
<blank>
First
Back
TOC
Prev Next
Last
Page 16
Christian Jacob
Special:
- volatile: - <blank>: special variable whose value may change at any time normal variable
Class:
- register: - static: - extern: - auto: - <blank>: frequently used variable, kept in a machine register meaning depends on context dened in another le variable allocated from the stack
Size:
- long: - short: - <blank>:
First Back TOC
larger than normal integer or very large oat smaller than normal integer normal size number (integer or oat)
Elementary C++ Data Types Prev Next Last
Page 17
Christian Jacob
Sign:
- signed: - unsigned: signed integer or character use all integer or character bits (no sign bit)
Type:
- int: - float: - double: - char: - bool: integer oating point number double-size oat single character, also used for very short integer Boolean value, true or false
First
Back
TOC
Prev Next
Last
Page 18
Christian Jacob
8.4
Arithmetic Expressions
expression
simple value
function call
unary expression
binary expression
...
simple value
number constant
string constant
character constant
identifier (declared)
...
First
Back
TOC
Arithmetic Expressions
Prev Next
Last
Page 19
Christian Jacob
unary expression
unary operator
simple value
expression
binary expression
simple value
binary operator
simple value
expression
expression
First
Back
TOC
Arithmetic Expressions
Prev Next
Last
Page 20
Christian Jacob
unary operator
++
--
...
binary operator
<<
>>
...
First
Back
TOC
Arithmetic Expressions
Prev Next
Last
Page 21
Christian Jacob
int oat
==, !=, <=, <, >, >= ==, !=, <=, <, >, >=
oat op oat
First
Back
TOC
Arithmetic Expressions
Prev Next
Last
Page 22
Christian Jacob
Logical Operators op int int int int ! (not) && (and), || (or)
op int
Bitwise Logical Operators op int int int int ~ (not) & (and), | (or), ^ (xor), >> (right shift), << (left shift)
op int
x ~x
1 0
0 1
0 1
1 0
1 0
1 0
0 1
0 1
x x << 2
1 0
0 1
0 1
1 1
1 0
1 0
0 1
0 0
First
Back
TOC
Arithmetic Expressions
Prev Next
Last
Page 23
Christian Jacob
First
Back
TOC
Arithmetic Expressions
Prev Next
Last
Page 24
Christian Jacob
The Comma Operator: Concatenation of Two Expressions expression1 , expression2 Evaluates expression1 and then expression2, and nally returns the value of expression2 as the result. Example:
First
Back
TOC
Arithmetic Expressions
Prev Next
Last
Page 25
Christian Jacob
int pounds = 0; float kilos = pounds / 2.2; int x, y, z; int f(int n); ... x = -y * (z++ - f(4));
First
Back
TOC
Arithmetic Expressions
Prev Next
Last
Page 26
Christian Jacob
Compound Assignments A common operation in programming is to apply an operator to a variable, and then store the result in the same variable. For example, the following assignment doubles the value of j:
j = j * 2;
This can be rewritten as:
j *= 2;
This works for the following operators in C++ :
First
Back
TOC
Arithmetic Expressions
Prev Next
Last
Page 27
Christian Jacob
Special Case: Increments and Decrements by One For increasing or decreasing a variable value by 1, there is even a shorter notation in C++. Instead of writing
k = k + 1;
one can write
Page 28
Christian Jacob
Examples:
a = 1; b = ++a; c = a++;
// a = 1 // a = 2, b = 2 // a = 3, c = 2
First
Back
TOC
Arithmetic Expressions
Prev Next
Last
Page 29
Christian Jacob
First
Back
TOC
Arithmetic Expressions
Prev Next
Last
Page 30
Christian Jacob
8.5
Symbolic Constants
PI = 3.0
// Doesnt work!
First
Back
TOC
Symbolic Constants
Prev Next
Last
Page 31
Christian Jacob
Base 10 6 9 15
First
Back
TOC
Symbolic Constants
Prev Next
Last
Page 32
Christian Jacob
8.6
The backslash character (\) is called the escape character, signalling that a special character follows.
First
Back
TOC
Prev Next
Last
Page 33
Christian Jacob
A small selection of special characters: Character \b \f \n \r \t \ \ \nnn \NN Name Backspace Form feed New line Return Tab Single quote Double quote Meaning Move the cursor one character to the left Go to the top of a new page Go to the next line Go to the beginning of the current line Advance to the next tab stop The character The character
The character with The ASCII character with number nnn ASCII code nnn (octal) The character with The ASCII character with number NN (hexadecimal) ASCII code NN
First
Back
TOC
Prev Next
Last
Page 34
Christian Jacob
8.7
References
G. Blank and R. Barnes, The Universal Machine, Boston, MA: WCB/ McGraw-Hill, 1998. Chapter 3.2.
First
Back
TOC
References
Prev Next
Last