Title: CS149D Elements of Computer Science
1CS149D Elements of Computer Science
Ayman Abdel-Hamid Department of Computer
Science Old Dominion University Lecture 16
10/24/2002
2Outline
- Variables and Data types
- Assignment statement
- Arithmetic operators
- Increment/Decrement operators
3Variables Names
- Program variables correspond to memory spaces
reserved for storage - A variable name is called an identifier
- An identifier in C can be up to 255 characters
long - Can not begin with a digit (Invalid 1First)
- Can not contain blanks (Invalid num elements)
- Can not contain a hyphen, underscore is OK
(Invalid num-elements) - Special symbols are not allowed (Invalid
cost, cost!) - Reserved words can not be used as identifiers
(Invalid int, const, float) - A reserved word is a word that has a special
meaning in C. It can not be used as a
programmer-defined identifier - C is case sensitive, so count is different than
Count - To declare a variable, need to identify its data
type
4Data Types
- C Data types (built-in data types)
- Integers
- Floating-point numbers
- Characters
- And more
- The data type determines how data is represented
in the computer and the kind of processing that
the computer can perform on it - The number of bytes that a data type occupies in
memory is system dependent
5Variable Declaration
- DataType identifier
- int x
- DataType identifier, identifier,
- int x,y,z
- The initial value stored in a variable is not
know unless you initialize it with the
declaration - int x 10
- Can mix declaration with declaration/initializatio
n - int x10, y, z 5
- Use const keyword to indicate that the value of
this variable can not change - const float PI 3.141593f
6Numeric Data Types
- Integer numbers
- short, int, long Can use unsigned keyword
(unsigned short) - Numbers with fractions
- float, double, long double (double is double
precision) - Can use scientific notation float x 5.0e6
which means x ? 5 106 - const float x 2.3 //x is considered a double
constant - const float x 2.3f //or const float x 2.3F
- Number of bytes and range of values is system
dependent. In Microsoft VC
The textbook lists 2 bytes for int on a Borland
Turbo C 3.0 compiler See Table 2.2 on page 29
7Scientific Notation
- Rewrite the floating-point number as a mantissa
times a power of 10 - Mantissa absolute value greater than or equal to
1.0 and less than 10.0 - 25.6 ? 2.56 101
- Letter e is used to separate mantissa from
exponent - 25.6 ? 2.56e1
- Precision number of digits allowed for decimal
portion of mantissa - Exponent range number of digits allowed for for
exponent - Float double long double
- Precision 6 15 19 see Table 2.2
8Alphanumeric Data Types
- Single alphanumeric character
- char x a char y 1 is different than int
y 1 - Most programming languages use ASCII to represent
the English alphabet and other symbols (1
byte/character) - Sequence of characters
- string name CS149 Lecture
9Assignment statement
- Identifier expression
- Where expression can be a constant, another
variable, or the result of an operation - int x, y
- x 10 // x ? 10, x is assigned the value of 10
- y x
- x x 10
- Multiple assignments x y z 0
- Look out for the data types of both sides of the
assignment - int a
- a 12.8 // actually a is assigned 12
- Numeric conversion with possibility of
information loss
10Arithmetic Operators1/3
Operators Unary operator One operand Binary
operator two operands Unary Plus
Addition - Unary minus - Subtraction Multiplica
tion / floating-point division or integer
division (no fractional part) modulus operator
(remainder of integer division) Examples Area_tria
ngle 0.5 base height Y -x
11Arithmetic Operators2/3
- Examples of integer division and modulus
- 7/2 3 72 1
- 2/7 0 27 2
- 7.0/2.0 3.5 5 2.3 Error (both operands
must be integers) - Note that the result of integer division is a
truncated result, not a rounded result. 5/3
1 5.0/3.0 1.66666 - Operation between different types is a mixed
operation - Value with lower type is converted or promoted to
the higher type - int sum 18, count 5
- float average
- average sum/count // average assigned 3.0 and
not 3.6 (Why?) - The correct way to compute average would be
- average (float) sum / (float) count //
explicit type casting
12Arithmetic Operators3/3
Practice problems page 34
int a 27, b 6 float c c a/(float)b
int b 6 float a, c 18.6 a (int) c/b
13Increment and Decrement Operators
- Unary operators for incrementing and decrementing
variables , -- - Can be used in a prefix (count) or postfix
(count) position - int x
- x is equivalent to x x 1 is equivalent to
x - How about
- x y 3
- x y 3 //is the value assigned to x the same
in both cases NO - Will see what is the difference next lecture when
we talk about operator precedence and compound
arithmetic expressions