C Data Types Data Type A key capability

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. 75 e 3 (mantissa and exponent) – same as 2. 73 x 103 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], [name 2=value 2], …; • 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, abc 123, last_name • Bad: last-name • Cannot begin with a number • Bad: 1 letter • 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; int j, k, l; int n=10; float x, pi= 3. 14159; char a= ’A’; string name= ”John”; bool maybe= true; // variable (no initialization) // multiple variables // variable with initialization // init and no init 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 type name=value, name 2=value 2, … • 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 1 e-38 and 1 e 38 (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 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 • Floating point data float x, y, z; // declare 3 floating point variables x= 3. 14159; // decimal notation y= 3. 2 e 5; // scientific notation z= -15 e-10; Numerical values above are floating point literals Professor John Carelli Kutztown University Scientific Notation • Two parts • Mantissa – the decimal number before the “e” • Exponent – the characteristic (exponent) is the integer after the “e” number= mantissa x 10 characteristic Computer Science Department

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

Data/Variable Storage Counting in Binary • 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 Professor John Carelli Kutztown University 0 0 1 1 2 10 3 11 4 100 5 101 6 110 7 111 8 1000 … - 255 1111 Computer Science Department

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

Variables “under the hood” • For each variable, compiler needs to keep track of 4 pieces of information: • Variable name • Location of data float pi= 3. 14159; Variable • Memory address of stored data • Variable type • How to interpret data • How many bytes? Name pi Location 0 x 1001 Type float • Value • Stored at the memory address Professor John Carelli Memory locations Kutztown University … ? 0 x 1000 ? 0 x 1001 0 x 1002 0 x 1003 3. 14159 0 x 1004 0 x 1005 ? … ? Computer Science Department
- Slides: 17