Sei sulla pagina 1di 52

Lecture 1

C++ Overview
Preprocessor Directives
 The preprocessor is a specialized text editor that
is not (usually) part of the compiler. It uses a
different syntax than C++.
Instructions to the preprocessor begin with #.
They are terminated at the end of the line, but
may be continued to the next line with a slash
(/).

Lect ur e 1 - C Over vi ew 2
Preprocessor Directives
They come first, before C code.
they cause modification of code before the
program is compiled.

Ex.
#include<iostream>
#define YEAR 1776

Lect ur e 1 - C Over vi ew 3
Preprocessor Directives
The #include<iostream> gives a program
access to the standard I/O library, its
constants, and its input/output functions.
library has a standard header file for giving
I/O using cout and cin .

Lect ur e 1 - C Over vi ew 4
Preprocessor Directives
The #include gives a program access to a
library and causes the preprocessor to insert
definitions from its standard header file into a
program before compilation.

Lect ur e 1 - C Over vi ew 5
Complete C program
 int main() is the beginning of the main function;
execution begins here. The rest is the body of
the function, enclosed in { } The body has:
Variable declarations.
Executable statements.

Lect ur e 1 - C Over vi ew 6
Complete C program
 Declarations allocate storage, name, and type it.
Executable statements do the work.

 Variables are named memory locations where


values may be stored.

 Symbolic constants are named memory


locations whose values cannot change.

Lect ur e 1 - C Over vi ew 7
Complete C program
 Variables and constants are typed (int, float, char,
etc.). Each holds a single value of its type.

 Named entities (variables, constants, function


names) are identifiers and the names must
follow certain rules.

Lect ur e 1 - C Over vi ew 8
Naming Conventions
 Principles:
Start with a letter or underscore.
Contain letters, digits, __.
No blanks, commas, special characters.
Cannot be more than 31 characters.

Lect ur e 1 - C Over vi ew 9
Naming Conventions
Must conform to case sensitivity.
Cannot be a C keyword.

Ex.

Valid and invalid naming

Lect ur e 1 - C Over vi ew 10
Variables and Constants
 Variables and Constants:
A variable is a symbolic name associated with
a physical address.
Defined constants associate symbolic names
with constant values.
By convention, variable names are mostly
lower case; constants are upper case.
Use mnemonic names !

Lect ur e 1 - C Over vi ew 11
Data Types
 Data type: A set of values and a set of
operations defined for those values.
The four standard types in C:
Int
Float
Double
Char

 C also has types: long, short, signed,


unsigned, . . .
Lect ur e 1 - C Over vi ew 12
Data Types
 Int (4 bytes):
Represents any positive or negative number
and zero; values have no decimal point. The
system (usually) will not convert a floating
point to an integer value.

Ex.

Lect ur e 1 - C Over vi ew 13
Data Types
 Float :
single precision, used for floating point values
(4 bytes).

 Double:
double precision, also used for floating point
values (8 bytes).

Lect ur e 1 - C Over vi ew 14
Data Types
Both float and double values require a
decimal point. The system will convert
integers to floating point values when
necessary.

Ex.

Lect ur e 1 - C Over vi ew 15
Data Types
 Range of Values:
Integer (4 bytes):
– can be represented exactly.
– range of values is up to +- 2,147,483,647.
Float (4 bytes):
– precision of 6 decimal digits.
– range of positive values ~ 10 -38 and 10 38 with a
similar range of negative values.

Lect ur e 1 - C Over vi ew 16
Data Types
Double (8 bytes):
– precision of 15 decimal digits.
– range of positive values ~ 10 -308 and 10 308 with
a similar range of negative values.

Lect ur e 1 - C Over vi ew 17
Data Types
 Char (1 byte):
Represents a single character, in ASCII.
Use single quotes as delimiters.
Requires one byte of memory.
We use the ASCII character set of 128
characters.
Ex.

Lect ur e 1 - C Over vi ew 18
Program Variables
 Variables:
A variable has a programmer-defined name
for a memory location, and its value may be
changed by the program.
By convention, variable names are usually
lower case.
Ex.

Lect ur e 1 - C Over vi ew 19
Symbolic Constants
 Any value that will not change during the life of a
function should be a constant, not a variable.

 The use of constants improves readability.

Lect ur e 1 - C Over vi ew 20
Symbolic Constants
At compile time, a programmer may define a
symbolic name for a constant value.
Symbolic constants cannot be changed by the
program.

Lect ur e 1 - C Over vi ew 21
Symbolic Constants
Syntax: const <type> <symbolic name> =
<value>;

Ex.
const int CARDS = 52;
const float PI = 3.14159;
const char CODE = 'd';
const int FALSE = 0;

Lect ur e 1 - C Over vi ew 22
Assignment Statements
 Syntax: <variable> = <expression>;
What is the semantics (meaning)?
x = 7;
letter = 'M';
y = y + 1; // add 1 to y
y++; // add 1 to y
++y; // add 1 to y
area = PI * radius * radius;
// not the same as constant RADIUS

Lect ur e 1 - C Over vi ew 23
Assignment Statements
x = x; // WHAT??

 Assign values at compile time:


int number_items = 0;
float cost = 24.95 + TAX;
char code = 'X';

Lect ur e 1 - C Over vi ew 24
Assignment Statements
 Assign values at run time:
int number_items;
float cost;
char code;
number_items = 0;
cost = 24.95 + TAX;
code = 'X' + 1;

Lect ur e 1 - C Over vi ew 25
I/O: formatted cout and cin
 Output:
Syntax:
cout << <format string>;
cout is a function which converts values to a
printable form, then displays them.

Lect ur e 1 - C Over vi ew 26
I/O: formatted cout and cin
\n:
– one of several escape sequences defined by C to
represent special characters.
– It is the line feed character and it moves the cursor
to the beginning of the next line.
Other commonly used escape sequences are
\t the tab character; \f the form feed character;
\b the backspace character.

Lect ur e 1 - C Over vi ew 27
I/O: formatted cout and cin
Ex.
cout << "Hello!”;
cout << "\n”; // go to a new line
cout << endl; // same as above

Lect ur e 1 - C Over vi ew 28
I/O: formatted cout and cin
Assume these values:
number = 500 , letter = ‘B’ , average = 89.965

Output:
I have 500 baseball cards.
Average: 89.965000 Grade: B
Hello!
-- blank line --
-- blank line --
[cursor]

Lect ur e 1 - C Over vi ew 29
I/O: formatted cout and cin
 Input:
Syntax:
cin >> <variable name>;
cin is a keyword which read values and
converts them to a machine readable format.

Lect ur e 1 - C Over vi ew 30
I/O: formatted cout and cin
Ex.
float radius; // radius of water tower
int length; // length of copper pipe
cin >> radius;
cin >> length;
Another Approach:
cin >> radius >> length;

Lect ur e 1 - C Over vi ew 31
I/O: formatted cout and cin
The location of radius and length in memory

Lect ur e 1 - C Over vi ew 32
Arithmetic in C++
 Arithmetic:
+ : addition a + b
- : subtraction a – b
* : multiplication a * b
/ : division a / b
% : modulo (mod) a % b
integer division ---> quotient
modulo arithmetic ---> remainder

Lect ur e 1 - C Over vi ew 33
Arithmetic in C++
Ex.
17 / 5 ---> 3
5 / 17 ---> 0
17 % 5 ---> 2
5 % 17 ---> 5

Note: % requires positive integer operands.

Lect ur e 1 - C Over vi ew 34
Arithmetic in C++
 Operator Precedence (full table is in appendix):
( ) inside out high
* / % left to right |
+ - left to right low
Ex.
What is value of: 9 + 3 * 2 + 12 / 4
This is the same as:
9 + (3*2) + (12/4)
9+6+3
18 Lect ur e 1 - C Over vi ew 35
Arithmetic in C++
 Mixed Mode Arithmetic and Data Type Hierarchy:
Mix mode arithmetic involves calculations with
mixed data types. Highest mode determines
the mode of the operation.
Data type hierarchy, high to low:
– double
– float
– int

Lect ur e 1 - C Over vi ew 36
Arithmetic in C++
Ex1.
20.5 / 5 ---> 4.1 5 is converted to 5.0
20 / 5.0 ---> 4.0 20 is converted to 20.0
20 / 6 * 2.0 ---> 6.0 Why?
2.0 * 20 / 6 ---> 6.667 Why?

Lect ur e 1 - C Over vi ew 37
Arithmetic in C++
Ex2.
float x, y;
y = 20 / 6; // 20/6 is 3, converted to 3.0
x = y * 5; // 3.0 * 5, converted to 5.0
cout << “y= “ << y <<“x= ”<< x);

Output: y=3.000000; x = 15.000000

Lect ur e 1 - C Over vi ew 38
Arithmetic in C++
 Rules of evaluating expressions in assignment
statements:
Do right-hand-side using data type hierarchy.
Convert result to left-hand-side type.
Intermediate results follow type rules.

Lect ur e 1 - C Over vi ew 39
Arithmetic in C++
Ex1:
float x;
x = (4*2) + 24/5;
x is 12.0 because :
4 * 2 ---> 8, an integer
24 / 5 ---> 4, an integer and performed
before the addition
8 + 4 ---> 12, an integer
12 is converted to a float, 12.0.

Lect ur e 1 - C Over vi ew 40
Arithmetic in C++
Ex2:
x = (4*2) + 24.0/5;
4 * 2 ---> 8, an integer
24.0 / 5 ---> 4.8, a float
8 + 4.8 ---> 8.0 + 4.8 ---> 12.8, all floats

Lect ur e 1 - C Over vi ew 41
Arithmetic in C++
Ex3.
1) 20.0 / 3 * 20 / 3 = 44.4444
2) 20.0 / (3*20) / 3 = 0.1111
3) 20 / 3 * 20 / 3 = 40
4) 20 / (3 * 20.0) / 3 = 0.1111
5) 20.0 / 3 + 20 / 3 = 12.6667
6) 20.0 / (3+20) / 3 = 0.2899
Notice that all but number three are doubles.

Lect ur e 1 - C Over vi ew 42
Arithmetic in C++
 Shortcuts:
Compound assignment:
n = n + 5; or n += 5;
// caution: order of precedent is different
n = n * 5; or n *= 5;
// caution: order of precedent is different
Also valid for -= /= %=

Lect ur e 1 - C Over vi ew 43
Arithmetic in C++
Increment operator: ++
– x++; same as x = x + 1;
Decrement operator: --
– x--; same as x = x - 1;
Multiple assignment:
– x = y = z = 0; // right to left execution
same as z = 0; y = 0; x = 0;

Lect ur e 1 - C Over vi ew 44
Arithmetic in C++
 Postfix vs. Prefix:
Postfix: x++; Increment x after use.
Prefix: ++x; Increment x before use.

Ex1.
x = 3;
y = x++;
y ---> 3 x ---> 4

Lect ur e 1 - C Over vi ew 45
Arithmetic in C++
Ex2.
x = 3;
y = ++x;
y ---> 4 x ---> 4
Ex3.
x = 3;
y = ++x + x;
Compiler dependent!
(Also avoid in printf() and other function calls)
Lect ur e 1 - C Over vi ew 46
Arithmetic in C++
 Type Casting:
Use type casting to change the type of a
variable temporarily during an intermediate
step of execution.
Syntax: (<type>)<expression>

Lect ur e 1 - C Over vi ew 47
Arithmetic in C++
Ex.
float x; int y; double z;
x = (int)(2.5 + 3.1); //x is now 5.0
x = 14; x is 14.0
y = 14.0; <--- illegal!
x = 3.9;
z = (int)x + x; //z is now 3 + 3.9 or 6.9

Lect ur e 1 - C Over vi ew 48
Arithmetic in C++
//The next two lines are functionally equivalent!
z = (double)4/17;
z = 4.0/17;
//The next two lines do almost the same thing!
y = (float)x/r;
y = x/(float)r;

Note: Casting does not modify the value of its


variable, such as x or r above.
Lect ur e 1 - C Over vi ew 49
Arithmetic in C++
More Examples:
float y; int x=8, r=3;
– y = (float)x/r; 8.0/3 ---> 2.667
– y = x/(float)r; 8/3.0 ---> 2.667
– y = (float)(x/r); float (8/3) ---> 2.000
– y = x/r; float (8/3) ---> 2.000
– y = x%r; ---> 2
– y = (float)(x%r); ---> 2.000
– y = (float)(x)%r;
---> illegal - mod requires an integer operand

Lect ur e 1 - C Over vi ew 50
Arithmetic in C++
 What happens?
int x; float y; int r;
x = 4; r = 8;
y = x/r; // y holds 0.0
y = (float)x/r; // y holds 0.5
y = x/(r * 1.0); // y holds 0.5

Note: x is still 4, r is still 8.

Lect ur e 1 - C Over vi ew 51
Errors
 There are three errors:
syntax error : Code violates grammar rules.
Fix the first one, others often disappear. Code
doesn't compile.
run-time error : Code cannot run to
completion. It tells computer to perform an
illegal operation.
logic error : Code terminates normally, but
– produces incorrect results, or
– does not solve the problem (faulty algorithm).

Lect ur e 1 - C Over vi ew 52

Potrebbero piacerti anche