PF - 5
PF - 5
PF - 5
Fundamentals
5
2 Data Types
bool type
75.924 7.592400E1
0.18 1.800000E-1
0.0000453 4.530000E-5
-1.482 -1.482000E0
7800.0 7.800000E3
11 Floating-Point Data Types (cont'd.)
datatype identifier;
int counter;
double interestRate;
char grade;
14 Datatypes and their ranges
Type Width(bits) Common Range
char 8 -128 to 127
unsigned char 8 0 to 255
int 32 –2,147,483,648 to
2,147,483,647
unsigned int 32 0 to 4,294,967,295
short int 16 -32768 to 32767
unsigned short int 16 0 to 65535
long int 64 -2,147,483,648 to
2,147,483,647
unsigned long int 64 0 to 4,294,967,295
float 32 3.4E-38 to 3.4E+38
double 64 1.7E-308 to 1.7E+308
long double 80 3.4E-4932 to 3.4E+4932
15 string Type
To use the string type, you need to access its definition from the header file
string
Include the following preprocessor directive:
#include <string>
17 Using string datatype
String Position of a Character in the String Length of String
----------------------------------------------------------------------------------
“Test String” Position of ‘T’ is 0 11
Position of ‘i’ is 8
Position of ’ ’ (the space) is 4
Position of ‘S’ is 5
Position of ‘g’ is 10
When determining the length of a string, you must also count any spaces in the string. For
example, the length of the following string is 22
“It is a beautiful day.”
18 Arithmetic Operators and Operator
Precedence
C++ arithmetic operators:
+ addition
- subtraction
* multiplication
/ division
% modulus operator
+, -, *, and / can be used with integral and floating-point data types
Operators can be unary or binary
19 Example
Arithmetic Results Description
Expression
5.0 + 3.0
3.0 + 9.4
16.3 - 5.2
4.2 * 2.5
5.0 / 2.0
34.5 / 6.0
34.5 / 6.5
21 Example
#include <iostream>
using namespace std;
int main()
{
cout << "5.0 + 3.0 = " << 5.0 + 3.0 << endl;
cout << "3.0 + 9.4 = " << 3.0 + 9.4 << endl;
cout << "16.3 - 5.2 = " << 16.3 - 5.2 << endl;
cout << "4.2 * 2.5 = " << 4.2 * 2.5 << endl;
cout << "5.0 / 2.0 = " << 5.0 / 2.0 << endl;
cout << "34.5 / 6.0 = " << 34.5 / 6.0 << endl;
cout << "34.5 / 6.5 = " << 34.5 / 6.5 << endl;
return 0;
}
22 C++ Math Operator Rules
* : Multiplication
/ : Division
Integer division truncates remainder
7 / 5 evaluates to 1
% : Modulus operator returns remainder
7 % 5 evaluates to 2
+ : Addition
- : Subtraction
2+5=7
13 + 89 = 102
34 - 20 = 14
45 - 90 = -45
2 * 7 = 14
5/2=2
14 / 7 = 2
34 % 5 = 4
4%6=4
38 Example (Float)
Thank you