codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

8 / 87 topics
6Keywords and Identifiers7Variables, Literals, Constants & Storage Classes8Data Types & Type Modifiers9Type Conversion & Casting Operators10Operators11Booleans12Math & Numbers13Date and Time
Tutorials/C++ Programming/Data Types & Type Modifiers
⚡C++ Programming

Data Types & Type Modifiers

Updated 2026-05-12
30 min read

Data Types & Type Modifiers

In C++, data types define the kind of data a variable can hold, such as integers, floating-point numbers, or characters. Understanding different data types and how to use them effectively is crucial for writing efficient and error-free programs.

This tutorial covers various types of data in C++ including primitive types, derived types, user-defined types, and type modifiers like signed/unsigned and short/long. We'll also explore the sizeof operator to determine the size of different data types. By the end of this tutorial, you'll have a solid understanding of how to choose the right data type for your needs.

Primitive Data Types

Primitive data types are the basic building blocks of C++. They include:

  • Integer Types: Used to store whole numbers.

    • int: Typically 4 bytes; range depends on system architecture.
    • short: At least 2 bytes, usually smaller than int.
    • long: At least 4 bytes, typically larger than int.
    • long long: At least 8 bytes.
  • Floating Point Types: Used to store decimal numbers.

    • float: Single precision floating point; typically 4 bytes.
    • double: Double precision floating point; typically 8 bytes.
    • long double: Extended precision floating point; size varies but is usually larger than double.
  • Character Types: Used to store single characters.

    • char: Typically 1 byte.
  • Boolean Type: Used for true/false values.

    • bool: Represents boolean values (true or false).

Example: Using Primitive Data Types

primitive_types.cpp
1#include <iostream>
2
3int main() {
4 int num = 10;
5 float pi = 3.14f;
6 char initial = 'A';
7 bool isTrue = true;
8
9 std::cout << "Number: " << num << std::endl;
10 std::cout << "Pi: " << pi << std::endl;
11 std::cout << "Initial: " << initial << std::endl;
12 std::cout << "Is True: " << isTrue << std::endl;
13
14 return 0;
15}
Output
Number: 10
Pi: 3.14
Initial: A
Is True: 1

Derived Data Types

Derived data types are built using primitive data types and include:

  • Arrays: Collections of elements of the same type.
  • Pointers: Variables that store memory addresses.
  • References: Aliases for existing variables.

Example: Using Arrays

arrays.cpp
1#include <iostream>
2
3int main() {
4 int numbers[5] = {1, 2, 3, 4, 5};
5
6 for (int i = 0; i < 5; ++i) {
7 std::cout << "Element at index " << i << ": " << numbers[i] << std::endl;
8 }
9
10 return 0;
11}
Output
Element at index 0: 1
Element at index 1: 2
Element at index 2: 3
Element at index 3: 4
Element at index 4: 5

User-Defined Data Types

User-defined data types allow you to create custom data structures using struct or enum.

Example: Using Structs

user_defined_types.cpp
1#include <iostream>
2#include <string>
3
4struct Person {
5 std::string name;
6 int age;
7};
8
9int main() {
10 Person person1 = {"Alice", 30};
11 std::cout << "Name: " << person1.name << ", Age: " << person1.age << std::endl;
12
13 return 0;
14}
Output
Name: Alice, Age: 30

Type Modifiers

Type modifiers alter the behavior or size of data types.

  • Signed/Unsigned: Indicates whether a number can be negative.

    • signed: Can hold both positive and negative values (default for integers).
    • unsigned: Only holds non-negative values, allowing for larger ranges.
  • Short/Long: Modifies the size of integer types.

    • short: Smaller than int.
    • long: Larger than int.

Example: Using Signed/Unsigned and Short/Long

type_modifiers.cpp
1#include <iostream>
2
3int main() {
4 unsigned int positiveNumber = 10;
5 signed int negativeNumber = -5;
6
7 short smallInt = 32767; // Maximum value for a short
8 long largeInt = 9223372036854775807L; // Maximum value for a long
9
10 std::cout << "Positive Number: " << positiveNumber << std::endl;
11 std::cout << "Negative Number: " << negativeNumber << std::endl;
12 std::cout << "Small Int: " << smallInt << std::endl;
13 std::cout << "Large Int: " << largeInt << std::endl;
14
15 return 0;
16}
Output
Positive Number: 10
Negative Number: -5
Small Int: 32767
Large Int: 9223372036854775807

sizeof Operator

The sizeof operator returns the size in bytes of a data type or variable.

Example: Using sizeof

sizeof.cpp
1#include <iostream>
2
3int main() {
4 std::cout << "Size of int: " << sizeof(int) << " bytes" << std::endl;
5 std::cout << "Size of float: " << sizeof(float) << " bytes" << std::endl;
6 std::cout << "Size of char: " << sizeof(char) << " bytes" << std::endl;
7 std::cout << "Size of bool: " << sizeof(bool) << " bytes" << std::endl;
8
9 return 0;
10}
Output
Size of int: 4 bytes
Size of float: 4 bytes
Size of char: 1 byte
Size of bool: 1 byte

Practical Example

Let's create a simple program that calculates the sum of an array of integers and prints each element along with its size in memory.

practical_example.cpp
1#include <iostream>
2
3int main() {
4 int numbers[] = {1, 2, 3, 4, 5};
5 int sum = 0;
6
7 for (int i = 0; i < 5; ++i) {
8 std::cout << "Element: " << numbers[i]
9 << ", Size in memory: " << sizeof(numbers[i]) << " bytes"
10 << std::endl;
11 sum += numbers[i];
12 }
13
14 std::cout << "Sum of elements: " << sum << std::endl;
15
16 return 0;
17}
Output
Element: 1, Size in memory: 4 bytes
Element: 2, Size in memory: 4 bytes
Element: 3, Size in memory: 4 bytes
Element: 4, Size in memory: 4 bytes
Element: 5, Size in memory: 4 bytes
Sum of elements: 15

Summary

Data TypeDescription
PrimitiveBasic types like int, float.
DerivedArrays, pointers, references.
User-definedStructs and enums.
Type ModifiersSigned/unsigned, short/long.

Key Points

  • Primitive Types: The basic building blocks of data.
  • Derived Types: Built using primitive types.
  • User-defined Types: Custom data structures.
  • Type Modifiers: Alter the size or behavior of data types.
  • sizeof Operator: Determines the size of a data type in bytes.

What's Next?

Now that you understand data types and type modifiers, the next step is to learn about type conversion and casting operators. These tools allow you to convert one data type into another, which is essential for performing operations between different types of variables.

We'll explore explicit and implicit conversions, as well as static and dynamic casts in the upcoming tutorial. Stay tuned!


PreviousVariables, Literals, Constants & Storage ClassesNext Type Conversion & Casting Operators

Recommended Gear

Variables, Literals, Constants & Storage ClassesType Conversion & Casting Operators