HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to C++VariablesData TypesOperatorsStringsVectors (vector)Conditions (if / switch)Loops (for / while)FunctionsClasses (OOP)
REFERENCE
📖 Overview
🖥️ Output & Input
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array & Vector
🔁 Condition & Loop
🧩 Function & Class
C++›Data Types

Data Types

int, double, char, bool, string; type conversion (casting and stoi).

Simple enough for a 5-year-old to understand

Casting (type conversion)

Example code
1
#include <iostream>
2
using namespace std;
3
int main() {
4
int a = 7, b = 2;
5
cout << a / b << endl;
Integer division: 3
Output / InputInteger division: 3
6
cout << (double)a / b << endl;
With (double): 3.5
Output / InputWith (double): 3.5
7
return 0;
8
}
Output / Input
💡 Hint: By writing (double)a, you convert int to decimal and perform decimal division.

About this topic

C++'s basic types come from C: integer, floating-point, character, and boolean. Unlike C, bool exists in the language as a real type; true and false are keywords and no extra header is needed.

C++ adds standard library types on top of these. string represents text, vector represents a variable-sized array, and map represents key-value mapping. These are part of the library, not the language itself, but in practice they are used like basic types.

The pitfalls inherited from C apply here just as well: integer division truncates the fractional part, floating-point numbers cannot be represented exactly in binary, and overflow is undefined behavior in signed types. These behaviors are the price of C++'s compatibility with C.

What you will learn

  • Basic types and that bool is built into the language
  • The difference between the string type and a char array
  • The result of integer division
  • Why floating-point number comparison is unreliable

When to use?

If you are processing text, use string instead of a char array: it manages its own size, works with concatenation and comparison operators, and does not produce buffer overflow errors.

Floating-point types are not used for monetary calculations. Storing the amount as an integer in cents or using a fixed-point library is the correct solution.

Common mistakes

Wrong

#include <iostream>
using namespace std;

int main() {
    double average = 7 / 2;
    cout << average << endl;
    return 0;
}

The division is performed BEFORE the assignment, and the division of two integers is an integer: the result becomes 3, and then it is assigned as 3.0. Making the variable a double does not affect the division. At least one of the operands must be floating-point.

Correct

#include <iostream>
using namespace std;

int main() {
    double average = 7.0 / 2;
    cout << average << endl;
    return 0;
}

Wrong

#include <iostream>
using namespace std;

int main() {
    double a = 0.1 + 0.2;
    if (a == 0.3) {
        cout << "Equal" << endl;
    } else {
        cout << "Not equal" << endl;
    }
    return 0;
}

Because these values cannot be represented exactly in binary, the sum is very close to 0.3 but not equal; "Not equal" is printed to the screen. Floating-point numbers are not compared with equals; the absolute value of the difference between two values is compared with a small tolerance.

Correct

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    double a = 0.1 + 0.2;
    if (abs(a - 0.3) < 1e-9) {
        cout << "Equal" << endl;
    } else {
        cout << "Not equal" << endl;
    }
    return 0;
}

Wrong

#include <iostream>
using namespace std;

int main() {
    string name = "Ali";
    cout << name << endl;
    return 0;
}

The string header must be included for the string type. This code works on some compilers because the iostream header indirectly includes string, but this is not guaranteed and on another compiler you will get an "'string' was not declared" error.

Correct

#include <iostream>
#include <string>
using namespace std;

int main() {
    string name = "Ali";
    cout << name << endl;
    return 0;
}

Frequently asked questions

Which one should I use between string and a char array?

Almost always string. It tracks its length automatically, grows when needed, does not cause overflows, and works with operators. A char array is only used when communicating with C libraries or when precise control over memory is required.

What happens if the int limit is exceeded?

Overflow in signed types is undefined behavior; the compiler can transform the code assuming this will never happen, meaning the result is unpredictable. Use long long for large numbers; you can access limit values from the limits header.

Does auto make the type ambiguous?

No, the type is still definite and known at compile time; you are simply not writing it out. The ambiguity may exist for the human reader, but not for the compiler. It is preferred to write the type explicitly where it provides useful information to the reader.

Summary

bool is a real type in C++, no extra header is required.

Integer division happens before assignment and truncates the fractional part.

Floating-point numbers are not compared with equals; a tolerance is used.

Include the header for every library type you use.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code