Académique Documents
Professionnel Documents
Culture Documents
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
Functions
Arrays
Pointers
Arrays and Pointers
2006 February
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 1/256
Outline
Who am I? Who are You?
Overview
Quick Tour
Standard Input, Standard Output
Redirecting Output and Input
C++ and iostream library
An overview of the iostream library
Continuing our tour
hello.cpp
Basic Syntax
Input characters without skipping whitespace
I/O of other data to standard output and from standard
input
Reading into strings
Loops
Reading till the end of file
Infinite Loop while reading
Data Types
Integer Types
Characters
Octal, Hexadecimal Output with ostream
Floating Point Types
Named Constants
Enumerated Types: enum
Expressions
Arithmetic Expressions
Arithmetic Operators and Precedence
All Operators and their Precedence
Relational Expressions
Logical Expressions
Assignment Expressions
Increment, Decrement Operators
Comma Operator
Arithmetic if Expressions
Bitwise Operators
Casts
Statements
Simple Statements
Compound Statements
while Statement
do statement
Avoid Confusing == with =
Using a constant or single variable as a test condition
while and the null statement
for Statement
Comparing while and for
if and switch Statements
break, continue, goto
Functions
Defining Functions
Calling Functions
Using return Value from Functions
Function Parameters
Arrays
Defining Arrays
Arrays and Loops
Strings
Pointers
Pointers as Function Parameters
Arrays and Pointers
Strong relationship between arrays and pointers
Multidimensional Arrays and arrays of pointers
Arrays of pointers
Memory Allocation
Multidimensional Arrays
Command Line Arguments: argc, argv
Structures
Passing Structures to Functions
typedef
Reading and Writing Files
fstream: file input and output
Error Handling
Binary files
Character I/O
Reading a Line at a time: getline()
I/O of other data to/from Text Files
Guidelines
Style Guidelines
Program Design
Welcome!
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
I
I
Call me Nick
Statements
Arrays
Functions
Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 3/256
Welcome!
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
I
I
Call me Nick
Statements
Arrays
Functions
Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 3/256
Welcome!
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
I
I
Call me Nick
Statements
Arrays
Functions
Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 3/256
Welcome!
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
I
I
Call me Nick
Statements
Arrays
Functions
Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 3/256
Welcome!
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
I
I
Call me Nick
Statements
Arrays
Functions
Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 3/256
Welcome!
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
I
I
Call me Nick
Statements
Arrays
Functions
Pointers
I
I
I
slide 3/256
Welcome!
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
I
I
Call me Nick
Statements
Arrays
Functions
Pointers
I
I
I
slide 3/256
Welcome!
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
I
I
Call me Nick
Statements
Arrays
Functions
Pointers
I
I
I
slide 3/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
Next week:
I
I will print the notes out for you, now that I know what you
need
I will give you more information about this course
slide 4/256
C Programming for
Engineers
Outline
Nick Urbanik
slide 5/256
C Quick tour
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
Quick Tour
Standard Input, Standard
Output
slide 6/256
C is portable
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
Quick Tour
Standard Input, Standard
Output
See limits.h
slide 7/256
C Programming for
Engineers
Nick Urbanik
#include <iostream>
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 8/256
C Programming for
Engineers
Outline
Nick Urbanik
slide 9/256
C Programming for
Engineers
Nick Urbanik
Name
File Descriptor
normally connected to
Standard Input
keyboard
Standard output
screen
Standard error
I
screen
slide 10/256
C Programming for
Engineers
Outline
Nick Urbanik
slide 11/256
Output Redirection
C Programming for
Engineers
Nick Urbanik
For example:
C:\STUDENT> command > output
Data Types
Expressions
Statements
Functions
normally
Arrays
Pointers
command
>
with redirection
output (file)
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 12/256
Input Redirection
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
Quick Tour
Standard Input, Standard
Output
Redirecting Output and
Input
<
Expressions
n
di
analyse
st
Statements
Functions
Arrays
stdo
ut
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 13/256
C++
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
slide 14/256
C Programming for
Engineers
Outline
Nick Urbanik
slide 15/256
C Programming for
Engineers
Nick Urbanik
If you are using a very old C++ compiler, put this instead:
#include <iostream.h>
The library defines three standard stream objects:
std::cin pronounced see-in
I
slide 16/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
I
I
Overview
#include <iostream>
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 17/256
How do I remember?
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
An overview of the
iostream library
>> x
Expressions
Statements
Functions
Arrays
<< x
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 18/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
An overview of the
iostream library
slide 19/256
C Programming for
Engineers
Outline
Nick Urbanik
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
slide 20/256
First example
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
Program hello.cpp:
Basic Syntax
Input characters without
skipping whitespace
I/O of other data to standard
output and from standard
input
Reading into strings
Loops
Reading till the end of file
Infinite Loop while reading
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
slide 21/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
slide 22/256
C Programming for
Engineers
Outline
Nick Urbanik
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
slide 23/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
hello.cpp
Basic Syntax
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
slide 24/256
Variable declarations
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
I
I
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
slide 25/256
C Programming for
Engineers
Outline
Nick Urbanik
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
slide 26/256
C Programming for
Engineers
Nick Urbanik
Program one-char-io.cpp:
#include <iostream>
int main( void )
{
char letter ;
std::cin >> letter ;
std::cout << letter ;
}
Loops
Reading till the end of file
Infinite Loop while reading
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
slide 27/256
C Programming for
Engineers
Data Types
Nick Urbanik
integer
double
char
character
Program vars.cpp:
#include <iostream>
int main( void )
{
int num = 5;
float cost = 9.5;
std::cout << "Hello: num = " << num
<< " cost = " << cost
<< \n;
}
Basic Syntax
Input characters without
skipping whitespace
I/O of other data to standard
output and from standard
input
Reading into strings
Loops
Reading till the end of file
Infinite Loop while reading
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
slide 28/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
I
I
I
hello.cpp
Basic Syntax
Input characters without
skipping whitespace
I/O of other data to standard
output and from standard
input
Reading into strings
Loops
Reading till the end of file
Infinite Loop while reading
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
slide 29/256
C Programming for
Engineers
Outline
Nick Urbanik
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
slide 30/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
hello.cpp
Basic Syntax
#include <iostream>
int main( void )
{
double fahr = 212, cel;
cel = ( 5.0 / 9.0 ) * ( fahr 32 );
std::cout << fahr << " deg F => "
<< cel << " deg C\n";
}
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
slide 31/256
C Programming for
Engineers
Nick Urbanik
Program cin-cout-2.cpp:
Basic Syntax
Input characters without
skipping whitespace
#include <iostream>
int main( void )
{
int num;
float cost;
std::cout << "Enter number: ";
std::cin >> num;
std::cout << "Enter cost: ";
std::cin >> cost;
std::cout << "Num = " << num << ", cost = "
<< cost << \n;
}
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
slide 32/256
C Programming for
Engineers
Outline
Nick Urbanik
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
slide 33/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
hello.cpp
Basic Syntax
Program cin-cout-3.cpp:
#include <iostream>
int main( void )
{
int num;
char lname[ 1000 ];
std::cout << "Enter class number: ";
std::cin >> num;
std::cout << "Enter last name: ";
std::cin >> lname;
std::cout << "class number is " << num
<< ", lastname is " << lname
<< \n;
}
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
slide 34/256
C Programming for
Engineers
Outline
Nick Urbanik
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
slide 35/256
Loops
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
Program while-1.cpp:
#include <iostream>
I/O of other data to standard
output and from standard
int main( void )
input
Reading into strings
{
Loops
Reading till the end of file
int j = 0;
Infinite Loop while reading
while ( j < 5 ) {
Data Types
std::cout << "j has the value " << j << \n;
Expressions
j = j + 1;
Statements
}
Functions
}
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
slide 36/256
C Programming for
Engineers
Outline
Nick Urbanik
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
slide 37/256
End of file
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Data Types
Statements
or Control-d
on Linux to tell your program that it has
Expressions
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
slide 38/256
C Programming for
Engineers
Nick Urbanik
float num;
while ( std::cin >> num ) {
// Now we know that num is a valid float,
// so do something with it
}
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
slide 39/256
C Programming for
Engineers
Outline
Nick Urbanik
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
slide 40/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
hello.cpp
Basic Syntax
Input characters without
skipping whitespace
I/O of other data to standard
output and from standard
input
Reading into strings
Loops
Reading till the end of file
I
I
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
slide 41/256
Data Types
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
I
I
I
I
I
I
strings or characters
integers
floating point values
memory addresses
binary values representing music or video
...
Characters
Octal, Hexadecimal Output
with ostream
Floating Point Types
Named Constants
Enumerated Types: enum
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
slide 42/256
Guidelines
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
Data Types
Integer Types
Characters
Keywords
character
signed character
unsigned character
integer
unsigned integer
long integer
unsigned long integer
long long integer
unsigned long long integer
short integer
floating point
Named Constants
char
Enumerated Types: enum
signed char
Expressions
unsigned char
Statements
int
Functions
int
Arrays
Pointers
long
Arrays and Pointers
unsigned long
Multidimensional
long long
Arrays and arrays of
pointers
unsigned long long
Structures
short
Reading and Writing
float or double or long double Files
slide 43/256
Guidelines
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
character
integer
short integer
long integer
long long integer
Overview
C++ and iostream
library
Continuing our tour
Data Types
Integer Types
Characters
Octal, Hexadecimal Output
with ostream
Floating Point Types
Named Constants
Enumerated Types: enum
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
slide 44/256
Guidelines
C Programming for
Engineers
Outline
Nick Urbanik
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
slide 45/256
Guidelines
C Programming for
Engineers
Signed integers 1
Nick Urbanik
int
Integer Types
Characters
Octal, Hexadecimal Output
with ostream
size:
Expressions
range:
example declaration:
int num;
example constant:
1000
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
slide 46/256
Guidelines
C Programming for
Engineers
Signed integers 2
Nick Urbanik
long
size:
usually 4 bytes.
range:
Overview
example declaration:
long lnum;
example constant:
5212000L
Expressions
Statements
short
Functions
Arrays
size:
Pointers
15
range:
declaration:
short snum;
constants:
slide 47/256
120
C Programming for
Engineers
long long
Nick Urbanik
long long
size:
usually 8 bytes.
range:
Data Types
Overview
example declaration:
example constant:
5212000LL
Integer Types
Characters
Octal, Hexadecimal Output
with ostream
Floating Point Types
Named Constants
Enumerated Types: enum
Expressions
Statements
Functions
Arrays
size:
usually 8 bytes.
Pointers
64
range:
example declaration:
example constant:
slide 48/256
5212000LL
Unsigned integers 1
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
unsigned int
Data Types
Integer Types
Characters
size:
range:
declaration:
constants:
5530u
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
slide 49/256
Guidelines
Unsigned integers 2
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
unsigned long
Overview
size:
range:
example declaration:
example constant:
76212000uL
Expressions
unsigned short
Statements
Functions
size:
range:
example declaration:
example constant:
34000u
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
slide 50/256
Guidelines
C Programming for
Engineers
Outline
Nick Urbanik
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
slide 51/256
Guidelines
C Programming for
Engineers
Characters
Nick Urbanik
Who am I? Who are
You?
I
I
Overview
C++ and iostream
library
Continuing our tour
Data Types
Integer Types
Characters
char
size:
range:
Named Constants
Enumerated Types: enum
Expressions
Statements
Functions
Arrays
Pointers
example declaration:
char ch;
example constant:
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
slide 52/256
Guidelines
C Programming for
Engineers
Nick Urbanik
signed char
size:
range:
example declaration:
example constant:
Data Types
Integer Types
Characters
Octal, Hexadecimal Output
with ostream
Floating Point Types
Named Constants
Enumerated Types: enum
Expressions
Statements
Functions
unsigned char
Arrays
Pointers
size:
range:
Multidimensional
Arrays and arrays of
pointers
example declaration:
Structures
slide 53/256
Character types
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Examples of declarations:
char letter ;
signed char sletter ;
unsigned char uletter ;
Named Constants
Enumerated Types: enum
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
slide 54/256
Guidelines
Program princhar.cpp
C Programming for
Engineers
Nick Urbanik
#include <iostream>
int main( void
{
char letter
int num
char ch1
char ch2
)
= A;
= letter ;
= b;
= B;
// A is character constant
// ASCII code = 98
// ASCII code = 66
Integer Types
Characters
Octal, Hexadecimal Output
with ostream
Floating Point Types
Named Constants
Enumerated Types: enum
Expressions
Statements
Functions
Arrays
Pointers
Multidimensional
Arrays and arrays of
pointers
letter = A, num = 65
letter + 1 = 66, num = 65
b - B = 32
slide 55/256
Structures
Reading and Writing
Files
Guidelines
C Programming for
Engineers
Outline
Nick Urbanik
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
slide 56/256
Guidelines
C Programming for
Engineers
Nick Urbanik
Integer
Octal
Hexadecimal
04
0x4
12
014
0xc
123
0173
0x7b
Expressions
Statements
Functions
Arrays
The long integer qualifier L can also used with the octal
and hexadecimal
Example of octal constant: 0553000L
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
slide 57/256
Guidelines
Program printvar.cpp
C Programming for
Engineers
Nick Urbanik
#include <iostream>
#include <iomanip>
int main( void )
{
int num = 77;
short int small = 0173;
short little = 0x7b;
long int big = 88000;
long large = 43000L;
unsigned int unum = 45000;
unsigned long ubig = 330000000UL;
std::cout
std::cout
<<
<<
std::cout
<<
std::cout
<<
std::cout
<<
std::cout
<<
}
slide 58/256
<< std::showbase;
<< "num (dec) = " << num
", (oct) = " << std::oct << num
", (hex) = " << std::hex << num << \n;
<< "small (oct) = " << std::oct << small
", little (hex) = " << std::hex << little << \n;
<< "big (dec) = " << std::dec << big
", large (dec) = " << large << \n;
<< "unum = " << unum
", ubig = " << ubig << \n;
<< "small (dec) = " << small
", little (dec) = " << little << std::endl;
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
C Programming for
Engineers
Nick Urbanik
slide 59/256
Characters
Octal, Hexadecimal Output
with ostream
Floating Point Types
Named Constants
Enumerated Types: enum
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
C Programming for
Engineers
Outline
Nick Urbanik
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
slide 60/256
Guidelines
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
Data Types
Integer Types
Characters
Octal, Hexadecimal Output
with ostream
float
size:
range:
example declaration:
float fnum;
example constant:
3.456f
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
slide 61/256
Guidelines
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
double
Data Types
Integer Types
size:
range:
example declaration:
double dnum;
example constants:
Characters
Octal, Hexadecimal Output
with ostream
Floating Point Types
Named Constants
Enumerated Types: enum
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
slide 62/256
Guidelines
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
long double
Data Types
Integer Types
size:
range:
Characters
Octal, Hexadecimal Output
with ostream
Floating Point Types
Named Constants
Enumerated Types: enum
Expressions
example declaration:
example constants:
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
slide 63/256
Guidelines
Program float-io.cpp
C Programming for
Engineers
Nick Urbanik
#include <iostream>
int main( void )
{
float cost = 15.92;
float total = 3.6e5;
float value = 357e1;
// value is 35.7
double debt = 1.2e15;
long double decrease = 5e6;
Named Constants
Enumerated Types: enum
Expressions
Statements
Functions
Arrays
Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
#include <float.h>
#include <iostream>
int main()
{
std::cout <<
std::cout <<
std::cout <<
std::cout <<
std::cout <<
std::cout <<
std::cout <<
std::cout <<
std::cout <<
std::cout <<
return 0;
}
Integer Types
Characters
Octal, Hexadecimal Output
with ostream
slide 65/256
Guidelines
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
slide 66/256
Guidelines
C Programming for
Engineers
Outline
Nick Urbanik
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
slide 67/256
Guidelines
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
Program define.cpp:
#include <iostream>
#define RATE 1.5 // Note: terminated by comment or newline
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
slide 68/256
Guidelines
C Programming for
Engineers
Outline
Nick Urbanik
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
slide 69/256
Guidelines
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
#include <iostream>
int main( void )
{
const float rate = 1.5;
float cost = rate * 8;
Overview
C++ and iostream
library
// with ;
Characters
Octal, Hexadecimal Output
with ostream
Floating Point Types
Named Constants
Enumerated Types: enum
Program enum-2.cpp:
Expressions
Statements
Functions
#include <iostream>
enum weather { clouds, rain, sunny, storm };
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Enumerated types 2
C Programming for
Engineers
Nick Urbanik
Program enum-3.cpp:
#include <iostream>
enum weather { clouds = 30, rain = 5,
sunny = 255, storm = 1 };
enum boolean { TRUE = 1, FALSE = 0 };
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
slide 71/256
Guidelines
C Programming for
Engineers
Expressions
Nick Urbanik
operator
Logical Expressions
Assignment Expressions
Increment, Decrement
Operators
Comma Operator
Arithmetic if Expressions
8 + x
Bitwise Operators
Casts
Statements
Functions
operands
Arrays
Pointers
Arrays and Pointers
slide 72/256
Multidimensional
Arrays and arrays of
C Programming for
Engineers
Outline
Nick Urbanik
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 73/256
Multidimensional
Arrays and arrays of
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
negative 6
( 6 )
positive 6
Logical Expressions
( 3 7 )
positive 4
Increment, Decrement
Operators
Assignment Expressions
Comma Operator
Arithmetic if Expressions
Bitwise Operators
Casts
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 74/256
Multidimensional
Arrays and arrays of
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Additive operators:
+ addition
subtraction
total = 8 + 5 2;
sum = num 3;
Multiplicative operators:
* multiplication
/ division
% modulo or remainder
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Arithmetic Expressions
Arithmetic Operators and
Precedence
All Operators and their
Precedence
Relational Expressions
Logical Expressions
Assignment Expressions
Increment, Decrement
Operators
Comma Operator
int total;
total = 8 * 5;
total = 23 / 4; // now total has the value 5
total = 23 % 4; // now total has the value 3
Arithmetic if Expressions
Bitwise Operators
Casts
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 75/256
Multidimensional
Arrays and arrays of
C Programming for
Engineers
Outline
Nick Urbanik
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 76/256
Multidimensional
Arrays and arrays of
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Arithmetic Expressions
6 * 2
Relational Expressions
Logical Expressions
I
I
I
I
Assignment Expressions
Increment, Decrement
Operators
Comma Operator
Arithmetic if Expressions
Bitwise Operators
Casts
Statements
Functions
Arrays
Pointers
Arrays and Pointers
2 + 20 3
slide 77/256
Multidimensional
Arrays and arrays of
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Arithmetic Expressions
Arithmetic Operators and
Precedence
All Operators and their
Precedence
Relational Expressions
Logical Expressions
2 + 10 5
Assignment Expressions
Increment, Decrement
Operators
Comma Operator
Arithmetic if Expressions
Bitwise Operators
Casts
12 5
Statements
Arrays
Functions
Pointers
Arrays and Pointers
slide 78/256
Multidimensional
Arrays and arrays of
C Programming for
Engineers
Outline
Nick Urbanik
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 79/256
Multidimensional
Arrays and arrays of
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Level
Operator
Function
15L
>, .
[]
()
sizeof
++,
!
+,
*, &
()
*, /, %
+,
14R
13L
12L
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Arithmetic Expressions
Arithmetic Operators and
Precedence
All Operators and their
Precedence
Relational Expressions
Logical Expressions
Assignment Expressions
Increment, Decrement
Operators
Comma Operator
Arithmetic if Expressions
Bitwise Operators
Casts
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 80/256
Multidimensional
Arrays and arrays of
C Programming for
Engineers
Nick Urbanik
Level
Operator
Function
<<, >>
<, <=, >, >=
==, !=
&
^
|
&&
||
?:
=
*=, /=, %=
+=, =, <<=
>>=, &=, |=, ^=
,
bitwise shift
relational operators
equality, inequality
bitwise AND
bitwise XOR
bitwise OR
logical AND
logical OR
arithmetic if
assignment operator
compound assignment operators
11L
10L
9L
8L
7L
6L
5L
4L
3L
2R
1L
Statements
Functions
comma operator
Arrays
Pointers
Arrays and Pointers
slide 81/256
Multidimensional
Arrays and arrays of
C Programming for
Engineers
Outline
Nick Urbanik
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 82/256
Multidimensional
Arrays and arrays of
Comparison Expressions
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
less than
>
greater than
<=
>=
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Arithmetic Expressions
Arithmetic Operators and
Precedence
All Operators and their
Precedence
Relational Expressions
Logical Expressions
Assignment Expressions
Increment, Decrement
Operators
Comma Operator
Arithmetic if Expressions
Bitwise Operators
Casts
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 83/256
Multidimensional
Arrays and arrays of
Relational expressions 1
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Example:
num = 3;
abc = ( num < 5 );
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Arithmetic Expressions
Arithmetic Operators and
Precedence
All Operators and their
Precedence
Relational Expressions
Logical Expressions
Assignment Expressions
Increment, Decrement
Operators
Comma Operator
Arithmetic if Expressions
Bitwise Operators
Casts
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 84/256
Multidimensional
Arrays and arrays of
Relational expressions 2
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
result is 1 or 0.
== equal,
!= not equal
equality operator is a double equal sign ==
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 85/256
Multidimensional
Arrays and arrays of
C Programming for
Engineers
Outline
Nick Urbanik
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 86/256
Multidimensional
Arrays and arrays of
Logical expressions 1
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
logical AND
||
logical OR
logical NOT
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 87/256
Multidimensional
Arrays and arrays of
C Programming for
Engineers
Logical expressions 2
Nick Urbanik
expr2
Overview
C++ and iostream
library
non-zero
non-zero
non-zero
non-zero
Data Types
0-zero
Expressions
Arithmetic Expressions
expr2
(expr1) || (expr2)
non-zero
non-zero
non-zero
non-zero
0-zero
Logical Expressions
Assignment Expressions
Increment, Decrement
Operators
Comma Operator
Arithmetic if Expressions
Bitwise Operators
Casts
Statements
expr
!(expr)
Functions
non-zero
Arrays
Pointers
Arrays and Pointers
slide 88/256
Multidimensional
Arrays and arrays of
Program countdig.cpp
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
Increment, Decrement
Operators
Comma Operator
Arithmetic if Expressions
Bitwise Operators
Casts
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 89/256
Multidimensional
Arrays and arrays of
C Programming for
Engineers
Outline
Nick Urbanik
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 90/256
Multidimensional
Arrays and arrays of
Assignment expressions
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Data Types
Overview
Expressions
Arithmetic Expressions
total = ( num = ( 4 + 2 ) );
Relational Expressions
Logical Expressions
Assignment Expressions
Increment, Decrement
Operators
total = num = 4 + 2;
Comma Operator
Arithmetic if Expressions
Bitwise Operators
which is identical to
total = 4 + 2;
num = 4 + 2;
Casts
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 91/256
Multidimensional
Arrays and arrays of
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
j += 3; is equivalent to j = j + 3;
j *= 3; is equivalent to j = j * 3;
*=
/=
%=
Logical Expressions
Assignment Expressions
Increment, Decrement
Operators
Comma Operator
Arithmetic if Expressions
Bitwise Operators
Casts
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 92/256
Multidimensional
Arrays and arrays of
C Programming for
Engineers
Outline
Nick Urbanik
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 93/256
Multidimensional
Arrays and arrays of
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Arithmetic Expressions
Arithmetic Operators and
Precedence
All Operators and their
Precedence
Relational Expressions
x = 32;
y = ++x;
Logical Expressions
Assignment Expressions
Increment, Decrement
Operators
Comma Operator
imply x = 32 + 1 = 33 and y = x = 33
But
x = 32;
y = x++;
Arithmetic if Expressions
Bitwise Operators
Casts
Statements
Functions
Arrays
Pointers
imply y = x = 32 and x = 32 + 1 = 33
slide 94/256
Program plusequl.cpp
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
#include <iostream>
int main( void )
{
int n, j, k ;
k = j = n = 4;
std::cout << "n = " << n << ", j = " << j
<< ", k = " << k << \n;
n += j = 3;
std::cout << "n = " << n << ", j = " << j
<< \n;
}
Casts
Statements
n = 4, j = 4, k = 4
n = 7, j = 3
Functions
Arrays
Pointers
Arrays and Pointers
slide 95/256
Multidimensional
Arrays and arrays of
C Programming for
Engineers
Outline
Nick Urbanik
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 96/256
Multidimensional
Arrays and arrays of
C Programming for
Engineers
Nick Urbanik
Example:
Relational Expressions
Logical Expressions
Assignment Expressions
The result is the value of the last expression in its list i.e.,
Increment, Decrement
Operators
r = num = 5
Bitwise Operators
Comma Operator
Arithmetic if Expressions
Casts
Statements
Arrays
Functions
Pointers
Arrays and Pointers
slide 97/256
Multidimensional
Arrays and arrays of
C Programming for
Engineers
Outline
Nick Urbanik
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 98/256
Multidimensional
Arrays and arrays of
Arithmetic if Expressions
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 99/256
Multidimensional
Arrays and arrays of
Arithmetic if expressions 2
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
equivalent to
if ( a > b )
max = a;
else
max = b;
Logical Expressions
Assignment Expressions
Increment, Decrement
Operators
Comma Operator
Arithmetic if Expressions
Bitwise Operators
Casts
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 100/256
Multidimensional
Arrays and arrays of
C Programming for
Engineers
Outline
Nick Urbanik
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 101/256
Multidimensional
Arrays and arrays of
Bitwise operators 1
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Arithmetic Expressions
Arithmetic Operators and
Precedence
All Operators and their
Precedence
Relational Expressions
Logical Expressions
Assignment Expressions
Increment, Decrement
Operators
Comma Operator
Arithmetic if Expressions
Bitwise Operators
Casts
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 102/256
Multidimensional
Arrays and arrays of
Bitwise operators 2
C Programming for
Engineers
Nick Urbanik
bit n
bit m
m & n
bit n
bit m
m | n
Increment, Decrement
Operators
Comma Operator
Arithmetic if Expressions
Bitwise Operators
Casts
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 103/256
Multidimensional
Arrays and arrays of
Bitwise operators 3
C Programming for
Engineers
Nick Urbanik
bit m
Expressions
Arithmetic Expressions
Arithmetic Operators and
Precedence
bit m
bit m
m ^ n
Comma Operator
Arithmetic if Expressions
Bitwise Operators
Casts
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 104/256
Multidimensional
Arrays and arrays of
C Programming for
Engineers
Shift Operators
Nick Urbanik
Who am I? Who are
You?
Overview
Arithmetic Expressions
Arithmetic Operators and
Precedence
All Operators and their
Precedence
Relational Expressions
Logical Expressions
//
//
//
//
0000
0000
0000
0000
0001
0010
1000
0001
Assignment Expressions
Increment, Decrement
Operators
Comma Operator
Arithmetic if Expressions
Bitwise Operators
Casts
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 105/256
Multidimensional
Arrays and arrays of
C Programming for
Engineers
Nick Urbanik
#include <iostream>
#include <iomanip>
Data Types
Expressions
Arithmetic Expressions
0 ] );
Relational Expressions
Logical Expressions
Assignment Expressions
Increment, Decrement
Operators
Comma Operator
if Expressions
2: " << ( n >> 2Arithmetic
)
" << ( n >> 2 ) Bitwise Operators
Casts
}
}
slide 106/256
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
C Programming for
Engineers
Nick Urbanik
slide 107/256
signed values have the upper two bits the same as the
previous value of the MSb (most significant bit)
so the signed value 0xfffffff0 (16) shifted right two places is
0xfffffffc
Arithmetic if Expressions
Bitwise Operators
Casts
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
For example,
x = x & 0xf;
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
For example,
Data Types
Expressions
Arithmetic Expressions
Arithmetic Operators and
Precedence
All Operators and their
Precedence
Relational Expressions
y = y | 0xf ;
will set all bits except for the least significant four bits of y
I
For example,
Logical Expressions
Assignment Expressions
Increment, Decrement
Operators
Comma Operator
Arithmetic if Expressions
Bitwise Operators
Casts
Statements
Functions
z = z ^ 0xf;
Arrays
Pointers
slide 108/256
Multidimensional
Arrays and arrays of
C Programming for
Engineers
Outline
Nick Urbanik
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 109/256
Multidimensional
Arrays and arrays of
Cast expressions
C Programming for
Engineers
Nick Urbanik
( float ) 7
Examples:
( int ) ( 5.8 * 2.7 )
( double ) ( k = 10 )
( float ) num
Data Types
Expressions
Arithmetic Expressions
Arithmetic Operators and
Precedence
All Operators and their
Precedence
Relational Expressions
Logical Expressions
Assignment Expressions
Increment, Decrement
Operators
Comma Operator
lost.
I
Casts
Example:
( int ) 3.75
resulting value is 3
Arithmetic if Expressions
Bitwise Operators
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
Conversions
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 111/256
Multidimensional
Arrays and arrays of
Conversions: promotion
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
int num = 6;
float cost;
cost = num * 5;
Comma Operator
Arithmetic if Expressions
Bitwise Operators
Casts
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 112/256
Multidimensional
Arrays and arrays of
Conversions: demotion
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
The fraction will be cut off, leaving just the integer, 62. The
value 62 is then assigned to the variable num.
Increment, Decrement
Operators
Comma Operator
Arithmetic if Expressions
Bitwise Operators
Casts
Statements
Functions
Arrays
Pointers
Arrays and Pointers
slide 113/256
Multidimensional
Arrays and arrays of
Statements: an introduction
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Statements
Simple Statements
Compound Statements
Scope
Looping Statements
while Statement
do statement
Avoid Confusing == with =
Using a constant or single
variable as a test condition
while and the null
statement
for Statement
Comparing while and for
if and switch Statements
break, continue, goto
Functions
Arrays
Pointers
Arrays and Pointers
slide 114/256
Multidimensional
Statements: intro 2
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
x = 0;
std::cout << "a";
Statements
Simple Statements
Compound Statements
Scope
Looping Statements
while Statement
do statement
Avoid Confusing == with =
Using a constant or single
variable as a test condition
while and the null
statement
for Statement
Comparing while and for
if and switch Statements
break, continue, goto
Functions
Arrays
Pointers
Arrays and Pointers
slide 115/256
Multidimensional
C Programming for
Engineers
Outline
Nick Urbanik
Functions
Arrays
Pointers
Arrays and Pointers
slide 116/256
Multidimensional
Expression statements
C Programming for
Engineers
Nick Urbanik
count = 8;
num = 3 + 4;
calc();
Simple Statements
Compound Statements
Scope
Looping Statements
while Statement
do statement
Example:
Functions
Arrays
Pointers
Arrays and Pointers
slide 117/256
Multidimensional
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Statements
Simple Statements
Compound Statements
Scope
; // just a semicolon
Looping Statements
while Statement
do statement
Avoid Confusing == with =
Using a constant or single
variable as a test condition
while and the null
statement
for Statement
Comparing while and for
if and switch Statements
break, continue, goto
Functions
Arrays
Pointers
Arrays and Pointers
slide 118/256
Multidimensional
C Programming for
Engineers
Outline
Nick Urbanik
Functions
Arrays
Pointers
Arrays and Pointers
slide 119/256
Multidimensional
Compound Statements
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
while Statement
num = 6;
fact = ( 5 3 );
std::cout << fact << ", " << num << \n;
}
do statement
Avoid Confusing == with =
Using a constant or single
variable as a test condition
while and the null
statement
for Statement
Comparing while and for
if and switch Statements
break, continue, goto
Functions
Arrays
Pointers
Arrays and Pointers
slide 120/256
Multidimensional
Blocks 1
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Statements
Simple Statements
Compound Statements
Scope
Looping Statements
while Statement
do statement
Avoid Confusing == with =
Using a constant or single
variable as a test condition
while and the null
statement
for Statement
Comparing while and for
if and switch Statements
break, continue, goto
Functions
Arrays
Pointers
Arrays and Pointers
slide 121/256
Multidimensional
Program blocks.cpp
C Programming for
Engineers
Nick Urbanik
#include <iostream>
int main( void )
{
int num = 10;
float cost = 100.0;
{
float cost = 50.0;
num = 5;
std::cout << "Inside: " << cost
<< ", " << num << \n;
}
std::cout << "Outside: " << cost
<< ", " << num << \n;
}
Functions
Arrays
Inside: 50, 5
Outside: 100, 5
slide 122/256
Pointers
Arrays and Pointers
Multidimensional
C Programming for
Engineers
Outline
Nick Urbanik
Functions
Arrays
Pointers
Arrays and Pointers
slide 123/256
Multidimensional
C Programming for
Engineers
Scope
Nick Urbanik
Functions
Arrays
Pointers
Arrays and Pointers
slide 124/256
Multidimensional
C Programming for
Engineers
Outline
Nick Urbanik
Functions
Arrays
Pointers
Arrays and Pointers
slide 125/256
Multidimensional
Iteration statements
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
while
do
for
compound statement, or
another loop, or
a null statement.
Functions
Arrays
Pointers
Arrays and Pointers
slide 126/256
Multidimensional
C Programming for
Engineers
Outline
Nick Urbanik
Functions
Arrays
Pointers
Arrays and Pointers
slide 127/256
Multidimensional
while statement
C Programming for
Engineers
Nick Urbanik
while Statement
do statement
Avoid Confusing == with =
Using a constant or single
variable as a test condition
while and the null
statement
for Statement
Comparing while and for
if and switch Statements
break, continue, goto
Functions
Arrays
Pointers
Arrays and Pointers
slide 128/256
Multidimensional
C Programming for
Engineers
Outline
Nick Urbanik
Functions
Arrays
Pointers
Arrays and Pointers
slide 129/256
Multidimensional
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
Example:
i = 0;
do {
std::cout << "abc\n";
++i;
// loop counter
} while ( i < 4 );
Scope
Looping Statements
while Statement
do statement
Avoid Confusing == with =
Using a constant or single
variable as a test condition
while and the null
statement
for Statement
Comparing while and for
if and switch Statements
break, continue, goto
Functions
Arrays
Pointers
Arrays and Pointers
slide 130/256
Multidimensional
Program square-1.cpp
C Programming for
Engineers
Nick Urbanik
#include <iostream>
while ( num != 0 ) {
int square = num * num;
std::cout << "Square of " << num
<< " = " << square << \n;
std::cout << "Enter a number (0 to quit): ";
std::cin >> num;
}
}
Compound Statements
Scope
Looping Statements
while Statement
do statement
Avoid Confusing == with =
Using a constant or single
variable as a test condition
while and the null
statement
for Statement
Comparing while and for
if and switch Statements
break, continue, goto
Functions
Arrays
slide 131/256
Pointers
Multidimensional
C Programming for
Engineers
Outline
Nick Urbanik
Functions
Arrays
Pointers
Arrays and Pointers
slide 132/256
Multidimensional
Test Expression
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
assignment operation
simple primary expression consisting of a variable or a
constant
Functions
Arrays
Pointers
Arrays and Pointers
slide 133/256
Multidimensional
C Programming for
Engineers
Nick Urbanik
I
I
I
while ( n == 3 ) {
...
}
Functions
Arrays
Pointers
Arrays and Pointers
slide 134/256
Multidimensional
Program quit-1.cpp
C Programming for
Engineers
Nick Urbanik
#include <iostream>
Overview
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
C Programming for
Engineers
Outline
Nick Urbanik
Functions
Arrays
Pointers
Arrays and Pointers
slide 136/256
Multidimensional
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
I
I
Data Types
Expressions
Statements
Simple Statements
Compound Statements
Scope
Looping Statements
while ( 1 )
while ( 0 )
while ( i )
// infinite loop
// never execute loop
// is equivalent to
//
while ( i != 0 )
while Statement
do statement
Avoid Confusing == with =
Using a constant or single
variable as a test condition
while and the null
statement
for Statement
Comparing while and for
if and switch Statements
break, continue, goto
Functions
Arrays
Pointers
Arrays and Pointers
slide 137/256
Multidimensional
Program square-2.cpp
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
#include <iostream>
int main( void )
{
int num;
while ( 1 ) {
// or for (;;)
std::cout << "Please enter a number: ";
if ( ! ( std::cin >> num ) )
break;
int square = num * num;
std::cout << "Square of " << num
<< " = " << square << \n;
char ch;
std::cout << "Another square? (y/n) ";
if ( std::cin >> ch && ch != y )
break;
}
}
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Statements
Simple Statements
Compound Statements
Scope
Looping Statements
while Statement
do statement
Avoid Confusing == with =
Using a constant or single
variable as a test condition
while and the null
statement
for Statement
Comparing while and for
if and switch Statements
break, continue, goto
Functions
Arrays
Pointers
Arrays and Pointers
slide 138/256
Multidimensional
C Programming for
Engineers
Outline
Nick Urbanik
Functions
Arrays
Pointers
Arrays and Pointers
slide 139/256
Multidimensional
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
Example:
Data Types
Statements
Expressions
Simple Statements
Compound Statements
Scope
Looping Statements
while Statement
do statement
Avoid Confusing == with =
Using a constant or single
variable as a test condition
while and the null
statement
for Statement
Comparing while and for
Functions
Arrays
Pointers
Arrays and Pointers
slide 140/256
Multidimensional
C Programming for
Engineers
Outline
Nick Urbanik
Functions
Arrays
Pointers
Arrays and Pointers
slide 141/256
Multidimensional
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
Functions
Arrays
Pointers
Arrays and Pointers
slide 142/256
Multidimensional
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Statements
Simple Statements
Compound Statements
Scope
Looping Statements
while Statement
do statement
Avoid Confusing == with =
Using a constant or single
variable as a test condition
while and the null
statement
for Statement
Comparing while and for
if and switch Statements
break, continue, goto
Functions
Arrays
Pointers
Arrays and Pointers
slide 143/256
Multidimensional
C Programming for
Engineers
Outline
Nick Urbanik
Functions
Arrays
Pointers
Arrays and Pointers
slide 144/256
Multidimensional
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
for loop:
while loop:
hiniti;
while ( htesti ) {
hbody of loopi
hupdatei;
}
example:
example:
int i = 0;
while ( i < 3 ) {
std::cout << "loop " << i
<< \n;
++i;
}
Looping Statements
while Statement
do statement
Avoid Confusing == with =
Using a constant or single
variable as a test condition
while and the null
statement
for Statement
Comparing while and for
if and switch Statements
break, continue, goto
Functions
Arrays
Pointers
Arrays and Pointers
slide 145/256
Multidimensional
C Programming for
Engineers
Nick Urbanik
Data Types
Expressions
Statements
Simple Statements
Compound Statements
Scope
Looping Statements
while Statement
do statement
Avoid Confusing == with =
Using a constant or single
variable as a test condition
I
I
Functions
Arrays
Pointers
Arrays and Pointers
slide 146/256
Multidimensional
C Programming for
Engineers
Outline
Nick Urbanik
Functions
Arrays
Pointers
Arrays and Pointers
slide 147/256
Multidimensional
Conditions: if statement
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
if ( htest expressioni )
hstatementi;
Functions
Arrays
Pointers
Arrays and Pointers
slide 148/256
Multidimensional
if and else
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
I
I
Functions
Arrays
Pointers
Arrays and Pointers
slide 149/256
Multidimensional
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Statements
Simple Statements
Compound Statements
Scope
Looping Statements
while Statement
do statement
Avoid Confusing == with =
Using a constant or single
variable as a test condition
while and the null
statement
for Statement
Comparing while and for
if and switch Statements
break, continue, goto
Functions
Arrays
Pointers
Arrays and Pointers
slide 150/256
Multidimensional
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Statements
Simple Statements
Compound Statements
Scope
Looping Statements
while Statement
do statement
Avoid Confusing == with =
Using a constant or single
variable as a test condition
while and the null
statement
for Statement
Comparing while and for
if and switch Statements
break, continue, goto
Functions
Arrays
Pointers
Arrays and Pointers
slide 151/256
Multidimensional
C Programming for
Engineers
Outline
Nick Urbanik
Functions
Arrays
Pointers
Arrays and Pointers
slide 152/256
Multidimensional
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
I
I
Functions
Arrays
Pointers
Arrays and Pointers
slide 153/256
Multidimensional
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Statements
Simple Statements
Compound Statements
Scope
Looping Statements
while Statement
do statement
Avoid Confusing == with =
Using a constant or single
variable as a test condition
Functions
Arrays
Pointers
Arrays and Pointers
slide 154/256
Multidimensional
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Statements
Simple Statements
Compound Statements
Scope
Looping Statements
while Statement
do statement
Avoid Confusing == with =
Using a constant or single
variable as a test condition
while and the null
statement
for Statement
Comparing while and for
if and switch Statements
break, continue, goto
Functions
Arrays
Pointers
Arrays and Pointers
slide 155/256
Multidimensional
Functions 1
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
A function is a little bit like a simple IC, with input pins and
output pins.
Statements
Functions
Defining Functions
Calling Functions
Using return Value from
Functions
Function Parameters
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 156/256
C Programming for
Engineers
Functions 2
Nick Urbanik
Who am I? Who are
You?
Overview
sin()
Data Types
Expressions
Statements
Functions
Defining Functions
Calling Functions
Using return Value from
Functions
y = sin( x );
Function Parameters
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 157/256
Functions 3
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Statements
Functions
Defining Functions
Calling Functions
Using return Value from
Functions
Function Parameters
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 158/256
C Programming for
Engineers
Outline
Nick Urbanik
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 159/256
Function definition
C Programming for
Engineers
Nick Urbanik
Example:
void calc( void )
{
int num;
num = 5;
}
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 160/256
C Programming for
Engineers
Outline
Nick Urbanik
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 161/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
#include <iostream>
// function definition:
void calc( void )
{
std::cout << "Now in Calc\n";
}
Data Types
Expressions
Statements
Functions
Defining Functions
Calling Functions
Using return Value from
Functions
Function Parameters
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 162/256
C Programming for
Engineers
Outline
Nick Urbanik
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 163/256
Functions as expressions
C Programming for
Engineers
Nick Urbanik
Overview
Program calc-2.cpp
#include <iostream>
float calc( void )
{
return 8.0 * 5.35;
}
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 164/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
I
I
I
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 165/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
#include <iostream>
// Example of a function that takes
// parameters
float calc( int num, float calc rate )
{
return calc rate * num;
}
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 166/256
Return inconsistencies
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Statements
Functions
Defining Functions
Calling Functions
Using return Value from
Functions
Function Parameters
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 167/256
Program retbad-1.cpp
C Programming for
Engineers
Nick Urbanik
#include <iostream>
short getnum( void )
{
long num = 2147483647L;
std::cout << "Number is " << num << \n;
return num;
}
Defining Functions
Calling Functions
Using return Value from
Functions
Function Parameters
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Number is 2147483647
result is -1
slide 168/256
Program retbad-2.c
C Programming for
Engineers
Nick Urbanik
#include <stdio.h>
calc( void )
{
Overview
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
42.000000
slide 169/256
C Programming for
Engineers
Outline
Nick Urbanik
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 170/256
Function parameters
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
I
I
Expressions
Statements
Functions
Defining Functions
Calling Functions
Using return Value from
Functions
Function Parameters
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 171/256
Function parameters 2
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Program funcmult.cpp
#include <iostream>
float mult( int a, float b )
{
return a * b;
}
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Statements
Functions
Defining Functions
Calling Functions
Using return Value from
Functions
Function Parameters
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 172/256
Function parameters 3
C Programming for
Engineers
Nick Urbanik
Calling Functions
Using return Value from
Functions
Function Parameters
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 173/256
C Programming for
Engineers
Arrays
Nick Urbanik
Who am I? Who are
You?
Overview
an array of integers
an arrary of characters
an array of structures
an array of pointers
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 174/256
C Programming for
Engineers
Outline
Nick Urbanik
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 175/256
Array declaration
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Statements
Functions
Arrays
Defining Arrays
Arrays and Loops
Strings
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 176/256
C Programming for
Engineers
Arrays 2
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
int mynums[ 5 ];
Data Types
Statements
Expressions
int total[10];
Functions
Arrays
Defining Arrays
Arrays and Loops
Strings
Pointers
Arrays and Pointers
char name[40];
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 177/256
Array initialisation
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
char mychar = E;
Expressions
Statements
Functions
Arrays
Defining Arrays
Arrays and Loops
Strings
Pointers
Arrays and Pointers
int mynums[ 5 ] = { 3, 4, 5, 6, 7, };
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 178/256
Array Length
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Statements
Functions
Arrays
Defining Arrays
Arrays and Loops
Strings
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 179/256
C Programming for
Engineers
Nick Urbanik
Pointers
mynums[
mynums[
mynums[
mynums[
mynums[
0
1
2
3
4
]
]
]
]
]
=
=
=
=
=
3;
4;
5;
6;
7;
//
//
//
//
//
first object
second object
third object
fourth object
fifth object
slide 180/256
C Programming for
Engineers
Outline
Nick Urbanik
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 181/256
C Programming for
Engineers
Nick Urbanik
Program arrayprt.cpp
Statements
#include <iostream>
int main( void )
{
int nums[ ] = { 23, 8, 11, 31 };
for ( int i = 0; i < 4; ++i ) {
std::cout << nums[ i ] << ;
}
std::cout << \n;
}
Arrays
Functions
Defining Arrays
Arrays and Loops
Strings
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 182/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Defining Arrays
Arrays and Loops
Strings
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 183/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
#include <iostream>
const int max = 4;
int main( void )
{
int mynums[ max ] = { 23, 8, 11, 31 };
for ( int i = 0; i < max; ++i )
std::cout << mynums[ i ] << ;
std::cout << \n;
}
Statements
Functions
Arrays
Defining Arrays
Arrays and Loops
Strings
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 184/256
C Programming for
Engineers
Outline
Nick Urbanik
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 185/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
char name[ 20 ];
char string[ ] = "This is a string";
char str [ 10 ] = "string";
char letters[ 10 ] = {
s, t, r, i, n, g, \0
};
I
Data Types
Expressions
Statements
Functions
Arrays
Defining Arrays
Arrays and Loops
Strings
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 186/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Example:
char string2[ ] = "string";
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Statements
Functions
Arrays
Defining Arrays
Pointers
slide 187/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
I
I
Overview
Example:
char c;
// Wrong!
c = "A";
Data Types
Expressions
Statements
Functions
Arrays
Defining Arrays
Arrays and Loops
Strings
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 188/256
C Programming for
Engineers
Nick Urbanik
Statements
#include <string.h>
Arrays
Functions
Defining Arrays
Arrays and Loops
I
I
Strings
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 189/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
int len;
len = strlen( "a string" ); // len = 8
Expressions
Statements
Functions
Arrays
Defining Arrays
Arrays and Loops
Strings
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 190/256
Pointers
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
I
I
a pointer variable
an address
and another variable
slide 191/256
C Programming for
Engineers
Nick Urbanik
#include <iostream>
int main( void )
{
int num = 12;
int *nptr ;
nptr = #
std::cout << "num holds " << num
<< " and nptr points to " << *nptr << \n;
std::cout << "The address held in nptr is " << nptr
<< \n;
}
I
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Pointers as Function
Parameters
C Programming for
Engineers
Outline
Nick Urbanik
slide 193/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Statements
and call it like this: swap( a, b );, the values of a and b are
copied by value, so the final values are not changed.
The right way is to pass the address of a and b like this:
swap( &a, &b ); and define the function like this:
Functions
Arrays
Pointers
Pointers as Function
Parameters
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
C Programming for
Engineers
Outline
Nick Urbanik
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 195/256
C Programming for
Engineers
Nick Urbanik
I
slide 196/256
similarly *( pa + i ) == a[ i ] is true.
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
C Programming for
Engineers
Nick Urbanik
Data Types
Expressions
Statements
Functions
. . . but not:
++a; // compiler error
slide 197/256
Arrays
Pointers
Arrays and Pointers
Strong relationship between
arrays and pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
If you find the size of an array with sizeof, you are given
the number of elements the size of one element
Data Types
I
I
I
I
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Strong relationship between
arrays and pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 198/256
Program array-parameter.cpp
#include <iostream>
int ar [ 10 ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
void check array parameter ( int a[ ] )
{
std::cout << "sizeof( a ) = " << sizeof( a ) << \n;
std::cout << "sizeof( ar ) = " << sizeof( ar ) << \n;
}
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Output of array-parameter.cpp
sizeof( a ) = 4
sizeof( ar ) = 40
sizeof( ar ) = 40
number of elements in ar[] is 10
slide 199/256
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Strong relationship between
arrays and pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
C Programming for
Engineers
Nick Urbanik
Example:
slide 200/256
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
C Programming for
Engineers
Outline
Nick Urbanik
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 201/256
Arrays of pointers
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 202/256
C Programming for
Engineers
Outline
Nick Urbanik
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 203/256
C Programming for
Engineers
Nick Urbanik
Overview
C++ and iostream
library
Continuing our tour
Data Types
Statements
delete hpointer i;
if allocating an array or string, use syntax like this:
hpointer i = new htypei[ hlengthi ];
Expressions
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Arrays of pointers
Memory Allocation
Multidimensional Arrays
Command Line Arguments:
argc, argv
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 204/256
#include <iostream>
#include <stdlib.h>
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Arrays of pointers
Memory Allocation
Multidimensional Arrays
Command Line Arguments:
argc, argv
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
Program new.cpp 1
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
#include <iostream>
#include <string.h>
Overview
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
Program new.cpp 2
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Arrays of pointers
Memory Allocation
Multidimensional Arrays
Command Line Arguments:
argc, argv
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 207/256
C Programming for
Engineers
Outline
Nick Urbanik
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 208/256
Multidimensional Arrays
C Programming for
Engineers
Nick Urbanik
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Arrays of pointers
Note that this is wrong, and just uses the comma operator:
int entry = matrix[ 1, 2 ]; // WRONG!!!
%
Memory Allocation
Multidimensional Arrays
Command Line Arguments:
argc, argv
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 209/256
C Programming for
Engineers
Outline
Nick Urbanik
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 210/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Data Types
Overview
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Arrays of pointers
Memory Allocation
Multidimensional Arrays
Command Line Arguments:
argc, argv
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 211/256
Program echo.cpp
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Arrays of pointers
Memory Allocation
Multidimensional Arrays
Command Line Arguments:
argc, argv
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 212/256
Structures 1
C Programming for
Engineers
Nick Urbanik
slide 213/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
struct employee {
int id;
float salary ;
};
// somewhere else:
struct employee person;
I
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Passing Structures to
Functions
typedef
slide 214/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
The first item in that list initialises the first element of the
structure,
Data Types
I
I
Expressions
Statements
Functions
Arrays
...
We could initialise our struct employee like this:
Multidimensional
Arrays and arrays of
pointers
Pointers
Structures
Passing Structures to
Functions
typedef
slide 215/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
#include <iostream>
int main( void )
{
struct employee {
int id;
float salary ;
};
struct employee person;
person.id = 8;
person.salary = 80000;
std::cout << "ID = " << person.id << \n;
std::cout << "Salary = $" << person.salary << \n;
}
slide 216/256
C Programming for
Engineers
Outline
Nick Urbanik
slide 217/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Statements
Functions
Arrays
Pointers
p>id = 8;
p>salary = 80000;
std::cout << "ID = " << p>id << \n;
std::cout << "Salary = $" << p>salary << \n;
I
I
slide 218/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
I
I
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Passing Structures to
Functions
typedef
slide 219/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Statements
Functions
Arrays
Pointers
C Programming for
Engineers
Nick Urbanik
#include <iostream>
struct complex {
int re;
int im;
};
complex cadd( complex z1, complex z2 )
{
complex zt;
zt.re = z1.re + z2.re;
zt.im = z1.im + z2.im;
return zt;
}
int main( void )
{
complex za;
za.re = 1;
za.im = 2;
complex zb = za;
complex zc = cadd( za, zb );
std::cout << "zc.re = " << zc.re
<< ", zc.im = " << zc.im << \n;
}
C Programming for
Engineers
Outline
Nick Urbanik
slide 222/256
typedef
C Programming for
Engineers
Nick Urbanik
Example:
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Passing Structures to
Functions
typedef
slide 223/256
C Programming for
Engineers
Outline
Nick Urbanik
slide 224/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
I
I
I
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
fstream: file input and
output
Error Handling
Binary files
Character I/O
Reading a Line at a time:
getline()
slide 225/256
C Programming for
Engineers
Outline
Nick Urbanik
slide 226/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
slide 227/256
Text Files 7
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
#include <iostream>
#include <fstream>
// . . .
std::ifstream fin( "data.in" );
if ( ! fin ) {
std::cerr << "error: unable to open file "
<< "data.in for reading\n";
exit( 1 );
}
std::ofstream fout( "data.out" );
if ( ! fout ) {
std::cerr << "error: unable to open file "
<< "data.out for writing\n";
exit( 1 );
}
slide 228/256
Appending to files
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
#include <iostream>
#include <fstream>
// . . .
std::ofstream fout( "data.out", std::ios::app );
if ( ! fout ) {
std::cerr << "error: unable to open file "
<< "data.out for appending\n";
exit( 1 );
}
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
I
I
slide 229/256
C Programming for
Engineers
Outline
Nick Urbanik
slide 230/256
Binary files 1
C Programming for
Engineers
Nick Urbanik
#include <iostream>
#include <fstream>
// . . .
std::ofstream fout( "data.out", std::ios::app | std::ios::binary );
if ( ! fout ) {
std::cerr << "error: unable to open binary file "
<< "data.out for appending\n";
exit( 1 );
}
slide 231/256
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
fstream: file input and
output
Error Handling
Binary files
Character I/O
Reading a Line at a time:
getline()
I/O of other data to/from
Text Files
Binary files 2
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
#include <iostream>
Expressions
#include <fstream>
Statements
Functions
// . . .
std::ofstream fout( "data.out", std::ios::out | std::ios::binary ); Arrays
Pointers
if ( ! fout ) {
std::cerr << "error: unable to open binary file "Arrays and Pointers
Multidimensional
<< "data.out for writing\n";
Arrays and arrays of
pointers
exit( 1 );
Structures
}
Reading and Writing
Files
fstream: file input and
output
Error Handling
Binary files
Character I/O
Reading a Line at a time:
getline()
slide 232/256
C Programming for
Engineers
Outline
Nick Urbanik
slide 233/256
C Programming for
Engineers
Nick Urbanik
#include <iostream>
#include <fstream>
#include <stdlib.h>
int main( void )
{
char ch;
std::ifstream fin( "abc.txt" );
if ( ! fin ) {
std::cerr << "Cannot open file abc.txt\n";
exit( 1 );
}
// skips whitespace.
// while ( fin >> ch )
//
std::cout << ch;
while ( fin.get( ch ) )
std::cout.put( ch );
}
slide 234/256
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
fstream: file input and
output
Error Handling
Binary files
Character I/O
Reading a Line at a time:
getline()
I/O of other data to/from
Text Files
C Programming for
Engineers
Nick Urbanik
Program copyfile.cpp
#include <iostream>
#include <fstream>
#include <stdlib.h>
int main( void )
{
char ch;
std::ifstream fin( "abc.txt" );
std::ofstream fout( "mmm.txt" );
if ( ! fin | | ! fout ) {
std::cerr << "Problem opening files\n";
exit( 1 );
}
// The following skips white space:
// while ( fin >> ch )
//
fout << ch;
while ( fin.get( ch ) )
fout.put( ch );
}
slide 235/256
C Programming for
Engineers
Outline
Nick Urbanik
slide 236/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
I
I
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
fstream: file input and
output
Error Handling
Binary files
Character I/O
Reading a Line at a time:
getline()
slide 237/256
Program copy-lines-from-file.cpp:
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
#include <iostream>
#include <fstream>
#include <stdlib.h>
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
fstream: file input and
output
Error Handling
Binary files
Character I/O
Reading a Line at a time:
getline()
slide 238/256
C Programming for
Engineers
Outline
Nick Urbanik
slide 239/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Program fileout.cpp
#include <iostream>
#include <fstream>
int main( void )
{
int num = 127;
std::ofstream fout( "abc.txt" );
fout << num;
std::cout << "Wrote " << num
<< " to abc.txt\n";
}
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
fstream: file input and
output
Error Handling
Binary files
Character I/O
Reading a Line at a time:
getline()
slide 240/256
C Programming for
Engineers
Nick Urbanik
Program filein.cpp:
#include <iostream>
#include <fstream>
#include <stdlib.h>
int main( void )
{
int num;
std::ifstream fin( "abc.txt" );
if ( ! fin ) {
std::cerr << "Cannot open abc.txt\n";
exit( 1 );
}
fin >> num;
std::cout << "we got " << num
<< " from abc.txt\n";
}
slide 241/256
C Programming for
Engineers
Outline
Nick Urbanik
slide 242/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
printf ( "%d", i );
not
printf ("%d",i);
Data Types
Expressions
and
Statements
x = x + 3;
Arrays
not
x=x+3;
Multidimensional
Arrays and arrays of
pointers
Functions
Pointers
I
I
I
slide 243/256
Structures
Reading and Writing
Files
Guidelines
Style Guidelines
Program Design
Modules
C Programming for
Engineers
Outline
Nick Urbanik
slide 244/256
Program design
C Programming for
Engineers
Nick Urbanik
I
I
slide 245/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Style Guidelines
Program Design
Modules
slide 246/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
slide 247/256
C Programming for
Engineers
Outline
Nick Urbanik
slide 248/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
one .c, .C, .cc, .cpp, or .cxx file (lets call this the source
file), and
one .h, .H, .hh, or .hpp file (we call this the header file).
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Style Guidelines
Program Design
Modules
slide 249/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
#include <iostream>
#include "calc.h"
#include "main.h"
int glob;
Data Types
Overview
Expressions
main.h:
#ifndef MAIN H
#define MAIN H
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Style Guidelines
Program Design
Modules
#endif
slide 250/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
#include "main.h"
#include "calc.h"
Data Types
Expressions
Statements
Functions
Arrays
Pointers
calc.h:
#ifndef CALC H
#define CALC H
Style Guidelines
Program Design
Modules
#endif
slide 251/256
static variables
I
I
I
A variable defined with the keyword static is visible only within its
file, and does not conflict with a variable with the same name
defined in another file
the static keyword can (and should) be used with functions that
are to be used only within one file
The static keyword can be used inside functions and blocks
I It is initialised once, and its value remains even between
function calls. See Program static.cpp:
#include <iostream>
void show times called( void )
{
static int called = 0;
std::cout << "called " << ++called << " times\n";
}
int main( void )
{
for ( int i = 0; i < 5; ++i )
show times called();
}
output of static.cpp:
called
called
called
called
called
slide 252/256
1
2
3
4
5
times
times
times
times
times
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Style Guidelines
Program Design
Modules
Resources I
C Programming for
Engineers
Nick Urbanik
Steve Oualline.
Practical C Programming.
OReilly 1993.
slide 253/256
Resources II
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Paul Davies.
The Indispensable Guide to C With Engineering
Applications.
Addison-Wesley 1995.
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 254/256
Resources III
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
Data Types
Bjarne Stroustrup.
A Tour of the Standard Library.
Chapter 3 of The C++ Programming Language.
http:
//public.research.att.com/~bs/3rd_tour2.pdf
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 255/256
C Programming for
Engineers
Nick Urbanik
Who am I? Who are
You?
Overview
C++ and iostream
library
Continuing our tour
Data Types
Expressions
Statements
Functions
Arrays
Pointers
Arrays and Pointers
Multidimensional
Arrays and arrays of
pointers
Structures
Reading and Writing
Files
Guidelines
Some Things to Read
slide 256/256