DATA TYPES AND PROGRAM STRUCTURE Standard Data Types Constants and Variables Enumerated Data Types Anonymous Unions Program Structure Standard Data Types Pre-defined, built-in... Integers Floating point Characters Integer Data Type int -- usually 4 bytes (-61287) short int -- usually 2 bytes (472) unsigned int, unsigned short int Floating Point Data Type [+|-] int.int 323.725 -18.62 0.05 [+|-] float E int 1.75E12 float -- usually 4 bytes double -- usually 8 bytes long double -- more than 8 bytes Character Data Type Every char has integer representation ASCII: 'q' = 113 Can perform integer operations on characters 'q'+1 = 114 ('r') String = sequence (array) of characters "Super Bowl" S u p e r _ B o w l \0 [0],[1],[2],[3],[4],[5],[6],[7],[8],[9],[10] \0 is null terminator -- end of string 'q' -- character q "q" -- string -- q followed by 0 No arithmetic operations on strings Identifiers Identifier -- name of constant, variable, function, .... Letters, digits, underscores index firstName first_name (preferred) Total Case matters ... sum, Sum, SUM All different, but don't use more than one No length restriction 5-10 characters is usually about right Use mnemonic identifiers total_charges ... much better than ... x Constants Constant can never change const = ; Put name in all capital letters Easier to tell constants from variables Constants declared "at top" of program Makes changes easier const float POSTAGE = 0.32; ... cost = letters * POSTAGE; const int NUM_VEHICLES = 10; ... float weight [NUM_VEHICLES]; Constant may be initialized only with a constant expression // Caution - the following does not work const float INTEREST_RATE = interest; const char GRADE = 'A': const char HEADER [] = "Employee Records"; const char HEADER [30] = "Employee Records"; Variables Variable can take on different values Can be defined anywhere before first use Usually defined "at top" of program ; = ; Define one variable per line and comment it float voltage; // voltage is measured in kv int current; // current is to the nearest amp float sum = 0.0; // sum is the total pay amount char group; // group is A-E char name [20]; // name is employee's last name char day [10] = "Monday"; // day is day of the week Enumerated Data Types Create your own data type Enhance program readability enum pet {dog, cat, fish, ferret, pig}; ... pet companion; ... companion = cat; if (companion == ferret) ... ... if (companion < fish) ... Careful: // Caution - the following does not work companion = "pig"; Anonymous Unions To overlay variables in storage... union { int life; int death; int taxes; }; ... taxes = 35; cout << death << endl; Program Structure #include other #includes (if any) all constants any global variables function prototypes (if any) void main () { ... } functions (if any)