4. Data Types
Data Type C++ Keyword Byte(s) Typical range
Integer int 4 -2,147,483,648 to
2,147,483,647
Unsigned integer unsigned int 4 0 to 4,294,967,295
Long integer long 8 ~ -9 Billion to ~ 9 Billion
Floating point float 4 +/- 3.4e +/- 38 (~7 digits)
Double floating point double 8 +/- 1.7e +/- 308 (~15 digits)
Character char 1 128 to 127 or 0 to 255
String/Text string ∞ -
Presented by TA. Nada Kamel
7. Variable Declaration
C++ requires every variable to be declared with its type BEFORE its first
use.
Examples:
int height;
char letter;
float radius, diameter;
In these examples, variables have an undetermined value.
Presented by TA. Nada Kamel
8. Variable Initialization
Allows variable to have a specific value from the moment it is declared.
Example:
int depth = 67;
(data type) (identifier) = initial_value;
Presented by TA. Nada Kamel
10. Character Escapes
Keyword Meaning
n or endl newline
r carriage return
t tab
v vertical tab
b backspace
a alert (beep sound)
’ single quote
” double quote
? question mark
backslash
Presented by TA. Nada Kamel
12. Strings
Strings are able to store sequences of characters.
To utilize strings we must include a directive
#include <string>
Can be initialized with any of the three valid initialization formats:
string mystring = “Hello World!”;
string mystring(“Hello World!”);
string mystring {“Hello World!”};
Presented by TA. Nada Kamel
13. Strings (Cont.)
To input a string you will need to use:
getline(cin, mystring);
If the user will enter one word only, cin >> mystring can be used.
Example:
#include <iostream>
#include <string>
void main()
{
string mystring;
getline(cin, mystring);
std:: cout << “The string entered is ” << mystring << endl;
}
Presented by TA. Nada Kamel
17. Constants
Can be used to give names to constant values.
Example:
const float pi = 3.14159;
The value of the constant variable cannot be changed after being
initialized.
Presented by TA. Nada Kamel