Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Understanding C++ Data Types: Fundamentals, Modifiers, and Best Practices

Understanding C++ Data Types

This tutorial delves into the core data types of C++, from basic integers and floating‑point numbers to characters and booleans. Practical examples illustrate how each type is declared and used.

C++ data types are declarations that specify the kind and size of data a variable can hold. For instance:

int age = 13;

Here, age is an int, capable of storing 2‑ or 4‑byte integers. The exact size is implementation‑defined, but int is guaranteed to be at least 16 bits.


C++ Fundamental Data Types

The table below lists the fundamental types, their meanings, and typical sizes in bytes:

Data TypeMeaningSize (in Bytes)
intInteger2 or 4
floatFloating‑point4
doubleDouble floating‑point8
charCharacter1
wchar_tWide character2
boolBoolean1
voidEmpty (no data)0

Let’s explore each type in detail.


1. C++ int

int salary = 85000;

2. C++ float and double

float area = 64.74;
double volume = 134.64534;

These types also support scientific notation:

double distance = 45E12; // 45 × 10^12

3. C++ char

char test = 'h';

Note: Internally, char holds an integer code point.


4. C++ wchar_t

wchar_t test = L'ם'; // Hebrew letter

The leading L denotes a wide string literal.

Note: C++11 introduced char16_t and char32_t for fixed‑width Unicode.


5. C++ bool

bool cond = false;

6. C++ void


C++ Type Modifiers

Modifiers refine fundamental types. The four primary modifiers are:

  1. signed
  2. unsigned
  3. short
  4. long

They can be applied to int, double, and char.

C++ Modified Data Types List

Data TypeSize (in Bytes)Meaning
signed int4Standard integer (equivalent to int)
unsigned int4Only non‑negative values
short2Small integer: -32,768 to 32,767
unsigned short2Positive only: 0 to 65,535
longat least 4Large integer
unsigned long4Large positive integer
long long8Very large integer
unsigned long long8Very large positive integer
long double12Extended precision floating‑point
signed char1Signed character: -127 to 127
unsigned char1Unsigned character: 0 to 255

Examples:

long b = 4523232;
long int c = 2345342;
long double d = 233434.56343;
short e = 3434233; // Out of range error
unsigned int a = -5;    // Compile‑time error

Derived Data Types

Derived types are built from fundamental types: arrays, pointers, function pointers, structures, unions, and enumerations. They are covered in subsequent tutorials.


C Language

  1. C Data Types Explained: int, float, char, and More – A Complete Guide
  2. Mastering Python Data Types: A Practical Guide
  3. Understanding C Data Types: A Comprehensive Guide
  4. C++ Variable Types Explained: Memory, Limits, and Operations
  5. Understanding C++ Data Type Modifiers
  6. Understanding C++ Loop Types: For, While, Do-While Explained
  7. Mastering Data Abstraction in C++: Simplify Design with Interface-Implementation Separation
  8. Data Encapsulation in C++: Safeguard Your Code
  9. Understanding MATLAB Data Types: A Comprehensive Guide
  10. Comprehensive Guide to C# Data Types: Value, Reference, and Pointer