On a 64-bit CPU. Size/Range vary by CPU model and Word size.
unsigned short x; //range 0 to 65553 signed short x; //range ± 32767 short x; //assumed signed
There are (usually) no unsigned floats or doubles. Using in a program will result in: - a compiler error (must be corrected) - a compiler warning (compiler ignores )
digits with an assumed radix point after the first digit. (actually an exponent of 2 in binary) (in decimal) 1234.567 = 1.234567 x 10 3 Mantissa = 1234567 Exponent = 3
1.234567 x 10 Exponents must match! 7.654321 x 10 (4-2 = 2) 123 456700 x 10 Move radix point, subtract from exponent 7.654321 x 10 2 131.110321 x 10 2 Add the mantissas, keep the common exponent
131.110321 x 10 2 1 31110321 x 10 Convert the result to Scientific Notation 1.311103 x 10 4 Round off to 7 significant digits 1.311103 x 10 4 Done!
Consideration Integer Float Increasing Size increases Range increases number of significant digits Arithmetic fast and easy slow and complicated Precision always precise imprecise due to max number of significant digits Range Limited (small) Unlimited (practically)
- Use Integers unless Floats are needed - Use larger Integer types when increased (larger numbers) is needed - Use larger Float types when increased (number of significant digits) is needed.
String variables are not native to C++; they are an Extension. To use this type, you must:
NAME: an identifier (created by the programmer) (DATA) TYPE: describes data values and operations CONTENTS: can change during execution of the program
type identifier; type identifier = literal; type id1, id2, id3; type id1 = lit1, id2 = lit2, id3 = lit3;
type identifier = literal; - Creates a new variable named identifier of type type - Sets the initial value of the variable to literal
Example: int a = 3; 1. Memory is allocated ( set aside ), the size of an int. 2. The location is named. 3. The value 3 is placed in the location.
change the value of a variable variable = expression; - The variable must already be declared - The expression must evaluate to a value that is compatible with the type of the variable.
compatible usually means same type, as for and. char lettergrade = A ; string our_cheer = Go Big Blue! ; char lettergrade = A ; string our_cheer = Go Big Blue! ; lettergrade = our_cheer;
floats and integers are generally cross- compatible syntactically, but semantically there are caveats ("dangers"). double y = 3; // no problem short x = 2.5;//OK, BUT see below
variable = expression; - Evaluate the expression on the right. - When the types differ but are compatible, convert the expression s type to the variable s type. - Change the current value of the variable to the evaluated result.
Float values are truncated (not rounded). int x = 2.9; // sets x to 2 double y = 3.6; int z = y; // sets z to 3 cout << x << " " << y; 2 3
Operators,,, work as expected except: - integer integer results in an integer - Mixed operands (one int, one float) always results in a float. double x = 2/3; // int/int->int cout << x; // prints 0 x = 2/3.0; // int / float -> float cout << x; // prints 0.6666
Modulus Operator for integers: int dividend, remainder; dividend = 7 / 5; // dividend = 1 remainder = 7 % 5; // remainder = 2
Order of Precedence: First:,, left to right Second:, left to right int x; x = 5 + 2 * 4 6 / 3; // x = 11 // x = 5 + (2*4) (6/3) // x = 5 + 8 2 // x = 11
Parentheses may be used double x; x = (3 + 2) * 5; // x = 25 X = 3 + 2 * 5; // x = 13
Raising to an Exponent, Square Root, Round double x = sqrt(16); // x = 4 double y = pow(3,2); // y = 9 double z = round(2.8); // z = 3.0 double w = round(2.2); // w = 2 double d = round(3.12345 * 100) / 100; // d = 3.12
int y=0, x=0; x++; // x = x + 1; ++x; // x = x + 1; y = x++; // y = x; x = x + 1; y = ++x; // x = x + 1; y = x; x--; // x = x 1; --x; // x = x 1;
int X=1; X += 2; // x = x + 2; X -= 2; // x = x 2; X *= 2; // x = x * 2; X /= 2; // x = x / 2;
An identifier with a value that can never change. Same as a variable declaration with an initial value, but with the reserved word in front: const type identifier = literal;
const double tax_rate = 0.06; // syntax error: value cannot be changed tax_rate = 0.07;
Why use constants? 1. Clarity 2. Consistency 3. Maintainability
Now we know! how well a programmer can understand a program due to use of good style. What is 0.06? A tax rate? Interest rate? Fee rate?
when a value that should be exactly the same throughout a program actually is. The value of is not consistent this is better:
Any change to a program after it is finished. The ease/speed of doing Maintenance.
In 2013, by coincidence, the state and local rates were the same In 2016, the local tax rate changed to 0.07 (but the state rate remained at 0.06) Now we have to go through all programs, find all 0.06 s and decide if each needs to change to 0.07 or not.
This would have been better: Now, all we have to do is change the constant s value in one place, and re-compile all programs that use it.
Term Variable Declaration Allocate Assignment Mantissa Modulus Constant Definition Container for data: has a name, type and current contents. Statement that defines a variable (name, type, initial variable) and allocates the variable during execution. During execution, the reservation of memory location(s) represented by a variable. Statement that changes the value (current contents) of a variable. The significant digits of a number in Scientific Notation, with an assumed radix point after the first digit. Operator that calculates the remainder of an integer division. % in C++ An identifier with a declared type and value that cannot change.
Term Clarity Consistency Maintenance Maintainability Definition How well a programmer can understand a program due to use of good style. When a value that should be the same throughout a program actually is. Any changes to a program after it is finished. The ease/speed of changing a program after it is finished.