<<

C++ 1 Basic data types In our first program the data type of the variables we have used is int. Thus we can store whole numbers in these variables.

In this part, we will discuss:  more about the int type for storing numbers  double type for storing numbers with a fractional or part  char data type for single characters  string data type for storing series of characters  various operators and their rules of precedence  the bool data type for logical expressions

Integer data type We can determine the size of a variable (number of bytes used) and what the largest and smallest int values are on our system by having a program output the value for them. For example consider the following program:

#include #include

using namespace std;

int main() { cout << "Memory bytes used for integer types by my system" << endl; cout << "variable type int: " << sizeof(int) << endl; cout << "Largest and smallest integers on my system" << endl; cout << "Largest integer value = " << INT_MAX << endl; cout << "Smallest integer value = " << INT_MIN << endl; return 0; }

The above program produces the following output:

C++ Basics 2

Memory bytes used for integer types by my system variable type int: 4 Largest and smallest on my system Largest integer value = 2147483647 Smallest integer value = -2147483648

Notes:  The constants INT_MIN and INT_MAX are defined in the header climits. This is the reason why we had to include the file climits.  When we try to store values outside the type’s range, then we get incorrect results. For example consider the following program.

#include #include

using namespace std;

int main() { int bigNum = INT_MAX; // now bigNum contains 2147483647 cout << "bigNum = " << bigNum << endl; cout << "bigNum + 1 = " << bigNum + 1 << endl; cout << "bigNum + 2 = " << bigNum + 2 << endl; cout << "bigNum + 3 = " << bigNum + 3 << endl; return 0; }

Output of this program: bigNum = 2147483647 bigNum + 1 = -2147483648 bigNum + 2 = -2147483647 bigNum + 3 = -2147483646

This incorrect output is a result of the fact that int values bigger than 2,147,483,647 wrap- around as if they are arranged around a circle, starting over again at the beginning of the leftmost end of the negative int values. C++ Basics 3

The double data type A double variable can be assigned an integer value in the ranges used by int. Moreover, a double variable can also be assigned much larger values and fractional values also.

Examples of some double values are

14.8 -153.37 4.55555 -1234567890. 0.000000123

Scientific notation We can store double values up to 15 digits of precision. If a double is very large or very small, then we should express the value in scientific (also referred to as exponential) notation.

For example  The number -4560000000000 can be expressed as -4.56e12, where e12 means that 4.56 is multiplied by 1012. This simply means move the decimal point 12 places to the right with appending zeros as needed.  The number 0.000000012 can be expressed as 12e-9. That is multiply 12 by 10−9. That is moving the decimal point nine places to the left.  In C++, the exponent of a double value can be between -308 and +308.  In the absence of any special formatting instructions for the output, the computer will examine the double value to be output and determine automatically how many digits to display and whether to use ordinary decimal notation or scientific notation.

For example, the following program: C++ Basics 4

#include

using namespace std;

int main() { cout << 1.20 << endl; cout << -.123 << endl; cout << 12.3456789 << endl; cout << 12300000.0 << endl; cout << 123000000000. << endl; cout << 0.000012 << endl; return 0; }

…produces the following output:

1.2 -0.123 12.3457 1.23e+007 1.23e+011 1.2e-005

Formatting double value outputs Consider the following statement:

double dval = 12.6666666;

Suppose that we want to output the value of the variable dval to two decimal places.

As seen before, by default, the statement

cout << dval; outputs: C++ Basics 5

12.667

We can override the default output format. We can use formatting methods in C++. For example, to output the value of the variable dval, accurately to two decimal places (12.67), we have to include the compiler directive #include .

For example the program:

#include #include

using namespace std;

int main() { double dval = 12.6666666; cout << setiosflags (ios::fixed | ios::showpoint) << setprecision(2); cout << dval << endl; cout << setiosflags (ios::fixed | ios::showpoint) << setprecision(3); cout << dval; return 0; } produces the following output

12.67 12.667

The statement

cout << setiosflags (ios::fixed | ios::showpoint) << setprecision(2);

• The statement sets the output stream, cout, to print a fixed decimal point that has a precision of two digits after the decimal point.

• The fixed manipulator, the showpoint manipulator, and some other manipulators are defined the iomanip library of C++. C++ Basics 6

• The setprecision in the above cout statement sets, until further notice, double values output to the console will be two decimal places.

• In the above program we reset the same to three decimal places so that the second line in our output displayed the value of the variable dval to three decimal places of precision.

• The C++ formatting statements are said to be persistent because they will remain in effect until overridden by another formatting statement.

Type suffixes C++ lets you explicitly tell the compiler what data type you want a literal number to be considered. To do this, you can add a “type suffix” to a number. i represents an integer, and d represents a double. There are other suffixes for a variety of other data types that we will not discuss in this course. Here are some example uses:

5 // will be type int 5d // will be type double 5. // will be type double 5i // will be type int 5.5i // will be type int, possibly with a compiler warning

Mixed-type expressions If an algebraic expression contains different data types, then the expression is called a mixed-type expression.

• Whenever an arithmetic operation mixes an operand of type int and one of type double, the result is of type double. • If both operands are of the same type, the result is also of that type.

Therefore, if d is of type double and i of type int then

d + i // will be type double i * d // will be type double i + 5 // will be type int i + 5. // will be type double i + 5d // will be type double

C++ Basics 7

• In general, when it is legal to mix two different types in an operation, the result is of the more inclusive type. So, if we add an int value and a short value, the result is of type int.

For example, assigning an integer expression to a variable of type double, such as:

double d; int i = 5; d = 4 * i;

… is perfectly safe. However, assigning a double value to an integer variable will result in loss of precision. In this case, the fractional portion of the double value will not be stored in the integer variable. The value is truncated because an integer variable cannot store a fractional part. For example:

double d = 5.8; int i; i = d; // assigns 5 to i

The above loss of precision may also generate a compiler warning depending on how you have configured your compiler. You would be better off using a cast here, which we will discuss in the next lecture.