C++ Tutorial
C++ Data Types
int, double, char, bool, and string store different kinds of values. Pick the type that matches the data.
The type is a contract
Every variable has a type. The type says what kind of value fits and how cout and operators treat it. Choose int for whole counts, double for measurements, char for one glyph, bool for yes or no, and string for text.
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
int seats = 24;
double length_m = 1.75;
char row = 'C';
bool open = true;
string title = "StudyGrid";
cout << seats << endl;
cout << length_m << endl;
cout << row << endl;
cout << open << endl;
cout << title << endl;
return 0;
}bool prints as 1 or 0 unless you ask for words. That is normal forcout.
Compile this at /cpp/try with g++. That editor is only for C++. Python lives at/try. HTML lives at /html/try.
A table of common types
| Type | Holds | Example | Header |
|---|---|---|---|
int | Whole numbers | 42, -3 | built in |
double | Floating-point numbers | 3.14, -0.5 | built in |
char | One character | 'A', '7' | built in |
bool | true or false | true, false | built in |
string | Text of any length | "hello" | <string> |
string is not a built-in keyword in the same way as int. You must#include <string> before you use it. Forgetting the include is a common first error.
sizeof reports bytes
sizeof tells you how many bytes a type (or a variable) uses. The number can differ between compilers and machines. On the StudyGrid g++ setup you will typically see int as 4 bytes anddouble as 8.
Example
#include <iostream>
using namespace std;
int main() {
cout << "char " << sizeof(char) << endl;
cout << "int " << sizeof(int) << endl;
cout << "double " << sizeof(double) << endl;
cout << "bool " << sizeof(bool) << endl;
return 0;
}You almost never pick a type because of sizeof at this stage. You pick it because of the kind of value. sizeof is here so you know the tool exists.
int versus double
Integer division truncates toward zero: 7 / 2 is 3. If either side is adouble, you get a fractional result: 7.0 / 2 is 3.5. Useint for counts. Use double for measurements and money-style decimals in this tutorial.
Example
#include <iostream>
using namespace std;
int main() {
cout << 7 / 2 << endl;
cout << 7.0 / 2 << endl;
return 0;
}char is not a string
A char holds one character in single quotes: 'A'. A string holds many characters in double quotes: "ABC". Mixing them up — double quotes for a char, or assigning a sentence to a char — is a type error.
Other types exist
C++ also has float, long, unsigned, and more. This track stays withint, double, char, bool, and string until you have a reason to switch. Prefer double over float unless you are told otherwise.
Next: operators, which combine these values.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the C++ editor at /cpp/try. Change one measurement and check whether the result still has the right unit.
Chemistry
Atomic number versus pH
Atomic number is a count of protons. Oxygen has 8. That value is an integer; you never have 8.3 protons in a nucleus.
pH is −log10 of hydrogen-ion activity. Rainwater around 5.6 is weakly acidic because of dissolved CO2. It needs a double.
pH = −log₁₀[H⁺]
Example
#include <iostream>
using namespace std;
int main() {
int protons = 8; // oxygen
double ph = 5.6; // rainwater, roughly
cout << "O has " << protons << " protons" << endl;
cout << "pH = " << ph << endl;
return 0;
}Physics
A kelvin reading and its unit
Absolute temperature is kelvin. 293.15 K is 20 °C. The number and the scale are different kinds of data: one is a measurement, the other is a label.
char holds a single character. Using K instead of a string is enough when the scale cannot be “kelvin” spelled out.
T(K) = t(°C) + 273.15
Example
#include <iostream>
using namespace std;
int main() {
double temp = 293.15;
char scale = 'K';
cout << temp << " " << scale << endl;
return 0;
}