Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 1400 24 Jan 2007 Chapter 3: sections 2 - 5. Variable initialization Variables may be initialized when declared –Form; type name = initial_value; –Example:

Similar presentations


Presentation on theme: "CS 1400 24 Jan 2007 Chapter 3: sections 2 - 5. Variable initialization Variables may be initialized when declared –Form; type name = initial_value; –Example:"— Presentation transcript:

1 CS 1400 24 Jan 2007 Chapter 3: sections 2 - 5

2 Variable initialization Variables may be initialized when declared –Form; type name = initial_value; –Example: int sum = 0; float interest_rate = 0.56;

3 Type conversions Rankings: –long double(highest) –double –float –unsigned long –long –unsigned int –int(lowest)

4 Rules Rule #1 –char, short, unsigned short are all considered as int Rule #2 –When a mixed-type arithmetic operation is encountered, the lower ranked value is promoted to the higher ranking value. Rule #3 –When a value is assigned to a variable, it is converted to that variable type.

5 Overflow / Underflow Assigning a too large or too small value to a variable results in incorrect values. An error message may not be generated!

6 Math library functions The math library is a selection of useful pre- written functions that can be automatically included into your program Example – the exponent or power function; #include int main() {int temp; temp = 5; answer = pow(temp, 2); cout << answer;// outputs 5 2 or 25

7 Manual type conversions… Type casting allows you to manually force a value to be converted to a type –Form: static_cast (value ) –Example: int sum, count; float average; cin >> sum >> count; average = static_cast (sum) / count; cout >> “average is: “ << average << endl;


Download ppt "CS 1400 24 Jan 2007 Chapter 3: sections 2 - 5. Variable initialization Variables may be initialized when declared –Form; type name = initial_value; –Example:"

Similar presentations


Ads by Google