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

TypeHoldsExampleHeader
intWhole numbers42, -3built in
doubleFloating-point numbers3.14, -0.5built in
charOne character'A', '7'built in
booltrue or falsetrue, falsebuilt in
stringText 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;
}

FAQ: C++ Data Types

Common questions about this page.

What is the StudyGrid C++ tutorial?

The StudyGrid C++ tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, classes, the STL, templates, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run c++ data types examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c++ data types in this C++ C++ lesson (C++ Data Types).

Is the C++ editor the same as Try Python or Try HTML?

No. Try C++ compiles with g++ at /cpp/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. C++ lessons never open those editors.

Do I need to install a compiler to learn C++?

No. Open a chapter, click Try it in C++, and compile in the browser. You can also download a .cpp file and compile locally with g++.

Where should I start the C++ tutorial?

Start at C++ Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open C++ Examples, then templates, map, and lambdas. Use Next at the bottom of each chapter.

Is the C++ tutorial free?

Yes. The C++ workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.