You are on page 1of 18

ECE 431

CHAPTER 1: INTRODUCTION AND BASIC


C++ PROGRAMMING EP2 – DATA TYPES

1
char Data Type
The smallest integral data type

Used for characters: letters, digits, and special symbols

Each character is enclosed in single quotes


• 'A', 'a', '0', '*', '+', '$', '&'

A blank space is a character


• Written ' ', with a space left between the single quotes

2
char Data Type
Used to hold single characters or very small integer values
Usually occupies 1 byte of memory
A numeric code representing the character is stored in memory

SOURCE CODE MEMORY

char letter = 'C'; letter 67

3
Character Input
Reading in a character:
char ch;
cin >> ch; // Reads in any non‐blank char
cin.get(ch); // Reads in any char
//Example 1.2.4

4
String Literals

 Can be stored as a series of


characters in consecutive
memory locations
"Hello" H e l l o \0
 Stored with the null terminator,
\0, automatically placed at the
end

 Is comprised of characters
between the " "

5
Must #include <string> to create and use string
objects

Can define string variables in programs

The C++
string name; string
Class
Can assign values to string
variables with the name = "George";
assignment operator

cout << "My name is " <<


Can display them with cout name;

6
Reading in a string object
string str;
cin >> str; // Reads in a string

String Input // with no blanks


getline(cin, str); // Reads in a string
// that may contain
// blanks

3‐7
bool type
• Two values: true and false
• Manipulate logical
(Boolean) expressions
bool Data
Type true and false
• Logical values

bool, true, and false


• Reserved words
8
The bool Data Type
Represents values that are true or false
bool values are stored as integers
false is represented by 0, true by 1
bool allDone = true;
bool finished = false;

allDone finished

1 0

9
Floating‐Point Data Types
Designed to hold real numbers
• 12.45 ‐3.8

Stored in a form similar to scientific notation

Numbers are all signed

Available in different sizes (number of bytes): float, double, and long double

Size of float  size of double

 size of long double

10
Floating‐Point Data Types
C + + U S E S S C I E N T I F I C N O TAT I O N T O R E P R E S E N T R E A L N U M B E R S ( F L O AT I N G ‐ P O I N T
N O TAT I O N )

11
Maximum number of significant digits (decimal places) for
float values is 6 or 7
Maximum number of significant digits for double is 15
Precision: maximum number of significant digits
◦ Float values are called single precision
◦ Double values are called double precision

Floating‐Point Data Types


12
If a floating‐point value is assigned to an integer
variable
◦ The fractional part will be truncated (i.e., “chopped off”
and discarded)
◦ The value is not rounded
int rainfall = 3.88;
cout << rainfall; // Displays 3

Assigning Floating‐point Values to Integer


Variables
2‐13
Literal
◦ Data item whose value does not change during program execution
◦ Is also called a constant

'A' // character constant


"Hello" // string literal
12 // integer constant
3.14 // floating-point constant

Constants
14
Allocating Memory with
Constants and Variables
Named constant: memory location whose content can’t change during
execution
The syntax to declare a named constant is:
In C++, const is a reserved word

15
Also called constant variables

Variables whose content cannot be


changed during program execution
Named
Constants Used for representing constant
values with descriptive names
• const double TAX_RATE = 0.0675;
• const int NUM_STATES = 50;

Often named in uppercase letters

16
Benefits of Named Constants

Makes program code more Simplifies program const double TAX_RATE =


readable by documenting the maintenance: 0.0725;
purpose of the constant in the
name:
const double TAX_RATE = 0.0675;

salesTax = purchasePrice * TAX_RATE;

17
1. BUCKYS C++ PROGRAMMING
TUTORIALS ‐ 4 – VARIABLES
2. BUCKYS C++ PROGRAMMING
TUTORIALS ‐ 5 ‐ CREATING A
BASIC CALCULATOR
Other
References 3. BUCKYS C++ PROGRAMMING
TUTORIALS ‐ 6 ‐ VARIABLES
MEMORY CONCEPTS
4. BUCKYS C++ PROGRAMMING
TUTORIALS ‐ 7 ‐ BASIC
ARITHMETIC

18

You might also like