Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Data Types Data Type

Similar presentations


Presentation on theme: "C++ Data Types Data Type"— Presentation transcript:

1 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

2 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: Scientific 2.75e3 (mantissa and exponent) – same as 2.73x103

3 Alphabetic Data Character data Strings 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>

4 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!

5 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

6 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

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

8 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= ;

9 Compiler Directive Another way to define a fixed (constant) value
Syntax: #define NAME value Example: #define PI 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

10 Integers Can store positive and negative integers Three types
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 and (approx. +/- 2 billion)

11 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

12 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

13 Floating point Literals
Scientific Notation Two parts Mantissa – the decimal number before the “e” Exponent – the characteristic (exponent) is the integer after the “e” number= mantissa x 10characteristic Floating point data float x, y, z; // declare 3 floating point variables x= ; // decimal notation y= 3.2e5; // scientific notation z= -15e-10; Numerical values above are floating point literals

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

15 Data/Variable Storage
Counting in Binary 1 2 10 3 11 4 100 5 101 6 110 7 111 8 1000 - 255 Data stored in memory Each memory location has a numerical address Each location can store one byte of information One byte is 8 bits One bit can store one binary digit (0 or 1) So, a byte can store 28 unique values (256 values) Number of bytes required to store information will depend on the type of data being stored

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

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


Download ppt "C++ Data Types Data Type"

Similar presentations


Ads by Google