← Back to C++ Course | Chapter 1: Introduction & Basics | Lesson 9 of 15

C++ Data Types

Integer Type

The int type stores whole numbers with no fractional component, and on most modern 32-bit and 64-bit systems it occupies 4 bytes, giving it a range of roughly -2 billion to +2 billion. Use int for counters, indexes, and any value that will never need a decimal point.

Example: Integer Type

cpp
#include <iostream>

int main() {
	int counter = 2000000;
	std::cout << "Counter: " << counter << std::endl;
	return 0;
}

Character Type

The char type stores a single character in exactly 1 byte, using single quotes like A rather than the double quotes used for full strings. Internally, a char is really just a small integer representing that character's position in the ASCII table, which is why you can perform arithmetic directly on characters.

Example: Character Type

cpp
#include <iostream>

int main() {
	char grade = 'A';
	std::cout << "Grade: " << grade << std::endl;
	return 0;
}

Floating-Point Types

float and double both store numbers with a decimal point, but they trade off memory for precision: float uses 4 bytes and roughly 7 significant digits of accuracy, while double uses 8 bytes for about 15-16 digits. Most C++ code defaults to double unless memory is tightly constrained, since the extra precision avoids subtle rounding errors.

Example: Floating-Point Types

cpp
#include <iostream>

int main() {
	float price = 9.99f;
	double pi = 3.14159265358979;
	std::cout << price << " " << pi << std::endl;
	return 0;
}

Boolean Type

The bool type holds only two possible values, true or false, and is the natural type for anything that represents a yes/no condition, like whether a user is logged in. Using bool instead of an int flag (0 or 1) makes conditional code self-documenting, since isLoggedIn reads far more clearly than loggedInFlag == 1.

Example: Boolean Type

cpp
#include <iostream>

int main() {
	bool isLoggedIn = true;
	std::cout << "Logged in: " << isLoggedIn << std::endl;
	return 0;
}

Void Type

void represents the deliberate absence of a value, most commonly seen as the return type of a function that performs an action but doesn't hand anything back to the caller, like void printMessage(). It's also used in a few lower-level contexts, such as a generic pointer type void* that can point to data of any type.

Example: Void Type

cpp
#include <iostream>

void printMessage() {
	std::cout << "No value returned" << std::endl;
}

int main() {
	printMessage();
	return 0;
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.