Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

C++ Data Types

• Data Type
• A key capability of a programming language
• Establishes both the type of information that can be stored as well as
operations that can be done on that information

• C++ has built-in support for 3 major data types:


• Numerical, Alphabetic, and Logical
• Numerical: Integers and floating point
• Alphabetic: character (and string)
• Logical: true or false
• These can be used to save information of the given type by setting aside
a memory location to store the information under a user defined name
Professor John Carelli Kutztown University Computer Science Department
Numerical Data
• Two broad categories
• Integers store “whole” numbers
• No fractions or decimals
• positive and negative “counting” numbers, incl. zero
• Three types: short, int, long

• Floating point numbers


• “Fractional” numbers (i.e. those with decimal points)
• Two main types: float and double
• Can be represented in either decimal or scientific notation
• Decimal: 3.14159
• Scientific 2.75e3 (mantissa and exponent) – same as 2.73x10 3

Professor John Carelli Kutztown University Computer Science Department


Alphabetic Data
• Character data
• Store a single alphanumeric character
• Type is char
• characters are represented in single quotes: ‘x’, ‘A’, or ‘=‘

• Strings data
• Store a “string” of characters, like a name
• Type is string – specified in double quotes: “John” or “apple”
• Not built-in, comes from a pre-defined library (distributed with C++)
• It is actually a class (which did not exist in the original C language)
• To use strings, must include the string library: #include <string>

Professor John Carelli Kutztown University Computer Science Department


Logical Data
• Boolean
• Named for George Boole
• English mathematician (mid 1800’s) – developed Boolean Algebra
• Type is bool – used to represent conditional values
• Supports only two values: true and false
• Note: true and false are not strings – no quotes!

Professor John Carelli Kutztown University Computer Science Department


Variables/Identifiers
• To store information of a given type, a variable (identifier) of that type
must be declared
• The type must be specified
• A name for the variable must be declared
• An initial value may, optionally, be assigned

type name[=value], [name2=value2], …;

• Multiple variables can be assigned in one statement


• Separated with commas
Professor John Carelli Kutztown University Computer Science Department
Valid variable names
• Rules
• Can only contain letters, numbers, and underscore (_)
• Good: name, x, abc123, last_name
• Bad: last-name
• Cannot begin with a number
• Bad: 1letter
• Cannot be a C++ reserved word
• Bad: float, int, main
• Is case-sensitive
• Name is not the same as name

Professor John Carelli Kutztown University Computer Science Department


Variable Declaration Examples
int i; // variable (no initialization)
int j, k, l; // multiple variables
int n=10; // variable with initialization
float x, pi= 3.14159; // init and no init
char a= ’A’;
string name= ”John”;
bool maybe= true;

Professor John Carelli Kutztown University Computer Science Department


const qualifier
• Variables (as the name implies) store information that can be changed
in a program
• Occasionally, it is desirable to disallow changes to stored information
• Use keyword const
const type name=value, name2=value2, …

• Error message if an attempt is made to change a const variable


• const variables must be initialized on declaration (since they can’t be changed)
const float pi=3.14159;

Professor John Carelli Kutztown University Computer Science Department


Compiler Directive
• Another way to define a fixed (constant) value
• Syntax:
#define NAME value
Example:
#define PI 3.14159
• Note – there is NO semicolon “;” after the declaration
• Because this is actually interpreted in a pre-compilation step
• “NAME” gets replaced globally in the program before the actual compilation occurs
• Accepted practice is to capitalize compiler directives
Professor John Carelli Kutztown University Computer Science Department
Integers
• Can store positive and negative integers
• No decimals!

• Three types
• short, int, long
• Usually representing different numerical ranges
• We will be using int primarily
• Ranges are compiler dependent
• Generally: short <= int <= long
• On our system, int can store values between -2147483648 and 2147483647
• (approx. +/- 2 billion)

Professor John Carelli Kutztown University Computer Science Department


Floating Point Numbers
• Stores numerical data with decimals

• Two types
• float and double
• We will be using float primarily
• Ranges are compiler and system dependent - on ours:
• float can store pos and neg values between 1e-38 and 1e38 (approx.)
• float has 6 decimal digits of precision
• double has much greater range and precision

Professor John Carelli Kutztown University Computer Science Department


Literals and the assignment operator
• Literal is a term used to describe an explicit, fixed data value.
• As opposed to a value that is the result of a computation (like z=x+y;)
• Any data type can be assigned a literal value
• Using the assignment (=) operator – i.e. the equals operator

• integer assignment
int i, j, k; // declare 3 integers
i= 10;
j= 0;
k= -23;
10, 0 and -23 are literals
Professor John Carelli Kutztown University Computer Science Department
Floating point Literals
Scientific Notation
• Floating point data • Two parts
float x, y, z; // declare 3 floating point • Mantissa – the decimal number
variables before the “e”
x= 3.14159; // decimal notation • Exponent – the characteristic
y= 3.2e5; // scientific notation (exponent) is the integer after
z= -15e-10; the “e”
Numerical values above are floating
point literals
number= mantissa x 10characteristic

Professor John Carelli Kutztown University Computer Science Department


Literals cont.
• char • string
• A single character enclosed in • One or more characters enclosed in
single quotes double quotes
• Examples • Examples
char a, b, c; // declare 3 chars string name; // declare a string
a= ‘A’; name= “John”;
b= ‘v’; “John” is a string literal
c= ‘?’; • bool
‘A’, ‘v’, and “?” are character literals • true or false (no quotes!)
bool maybe=true;

Professor John Carelli Kutztown University Computer Science Department


Data/Variable Storage
Counting in Binary

• Data stored in memory 0 0


1 1
• Each memory location has a numerical address
2 10
• Each location can store one byte of information
• One byte is 8 bits 3 11
• One bit can store one binary digit (0 or 1) 4 100
• So, a byte can store 28 unique values (256 values) 5 101
6 110
• Number of bytes required to store information 7 111

will depend on the type of data being stored 8 1000


… -
255 11111111

Professor John Carelli Kutztown University Computer Science Department


Typical data storage by
type
Type # of bytes/bits Data Storage examples
int 4/32 Memory
Type Name value
short 2/16 Address
long 8/64 int n 1000
1001
float 4/32 1002 10
double 8/64 1003
char 1/8 char a 1004 ‘A’
string N/(N*8) string name 1005 ‘J’
1006 ‘O’
bool 1/8 1007 ‘H’
1008 ‘N’
int n=10; bool maybe 1009 true
float x, pi=3.14159; float pi 1010
char a=’A’; 1011
string name=”John”; 1012 3.14159
bool maybe= true; 1013

Professor John Carelli Kutztown University Computer Science Department


Variables “under the hood”
• For each variable, compiler needs to
keep track of 4 pieces of information: Memory locations
• Variable name … ?
• Location of data float pi= 3.14159; 0x1000 ?
• Memory address of stored data Variable
0x1001
Name pi
• Variable type 0x1002
Location 0x1001 3.14159
• How to interpret data 0x1003
• How many bytes? Type float
0x1004
• Value 0x1005 ?
• Stored at the memory address
… ?

Professor John Carelli Kutztown University Computer Science Department

You might also like