Swiftorial Logo
Home
Swift Lessons
AI Tools
Learn More
Career
Resources

Primitive Data Types in C++

Introduction

In C++, primitive data types are the basic data types provided by the language. They serve as the building blocks for data manipulation and are the foundation upon which more complex data types and structures are built. Understanding these primitive types is essential for any C++ programmer.

Integer Types

Integer types are used to store whole numbers. C++ provides several variants of integer types, each with a different range and storage size:

  • int: A typical integer type.
  • short: A shorter version of an integer.
  • long: A longer version of an integer.
  • long long: An even longer version of an integer, introduced in C++11.
  • unsigned: An integer type that can only store non-negative values.

Example:

int a = 10;
short b = 20;
long c = 30000;
long long d = 4000000000;
unsigned int e = 50;
                    

Floating-Point Types

Floating-point types are used to store numbers with fractional parts. C++ provides three floating-point types:

  • float: Single-precision floating-point type.
  • double: Double-precision floating-point type.
  • long double: Extended precision floating-point type.

Example:

float f = 5.75;
double g = 9.123456789;
long double h = 1.23456789012345;
                    

Character Type

The character type is used to store single characters. The char type occupies 1 byte and can store any character from the ASCII set.

Example:

char letter = 'A';
                    

Boolean Type

The boolean type is used to store truth values. The bool type can only take two values: true and false.

Example:

bool isTrue = true;
bool isFalse = false;
                    

Wide Character Type

The wide character type is used to store larger character sets, such as Unicode. The wchar_t type is typically used for this purpose.

Example:

wchar_t wideChar = L'Ω';
                    

Summary

Primitive data types are fundamental to programming in C++. They provide the basic means to store and manipulate data. Understanding these types is crucial for writing efficient and effective C++ programs.