Lesson 11 of 60 – Data Types in C++
18%

Data Types in C++

Data types specify what kind of value a variable can store. C++ provides different data types for integers, decimal numbers, characters, Boolean values, text, and more.

Note: Choosing an appropriate data type helps the compiler understand how a value should be represented and used.

1. What is a Data Type?

A data type tells C++ what kind of data a variable is intended to store.

int age = 20;

Here, int is the data type and age is the variable.

2. Why Are Data Types Important?

Data types help the compiler determine how values are represented and what operations are appropriate for them.

  • Define the kind of data
  • Help determine storage requirements
  • Support appropriate operations
  • Improve type safety
  • Make programs easier to understand

3. Main Categories of C++ Data Types

C++ data types can be broadly grouped into several categories.

  • Fundamental types
  • Derived types
  • User-defined types

Examples include int, double, arrays, pointers, classes, structures, and enumerations.

4. Integer Data Type – int

The int type is commonly used for whole-number values.

int age = 20;
int marks = 85;
int count = 100;

The exact range depends on the implementation, but an int is commonly at least 16 bits and often 32 bits.

5. Short Integer – short

The short type is an integer type that is typically smaller than or equal to an int.

short age = 20;

Its minimum guaranteed range is smaller than that of int.

6. Long Integer – long

The long type is an integer type that can provide at least as much range as int.

long population = 1000000L;

The size of long depends on the C++ implementation and platform.

7. Long Long Integer

The long long type provides an integer type with a large range.

long long distance = 9000000000LL;

It is useful when ordinary integer types are not large enough for the required values.

8. Signed Integer Types

Signed integer types can represent both negative and positive values.

int temperature = -10;
int balance = 500;

For ordinary integer types, signed is the default unless otherwise specified.

9. Unsigned Integer Types

Unsigned integer types represent non-negative values and can provide a larger positive range than the corresponding signed type.

unsigned int age = 25;

An unsigned integer cannot represent negative values.

10. Floating-Point Type – float

The float type is used for floating-point values.

float price = 99.50f;
float temperature = 36.5f;

The f suffix can be used to make a floating-point literal a float.

11. Double Data Type

The double type is commonly used when more floating-point precision is needed than a typical float.

double salary = 25000.75;
double percentage = 87.65;

12. Long Double

The long double type is another floating-point type that can provide at least as much precision as double.

long double value = 12345.6789L;

Its size and precision are implementation-dependent.

13. Character Data Type – char

The char type is used to store a single character.

char grade = 'A';
char symbol = '#';

Character literals are normally written using single quotation marks.

14. Boolean Data Type – bool

The bool type represents a logical value: true or false.

bool passed = true;
bool available = false;

15. String Data Type

C++ provides std::string in the standard library for storing text.

#include <string>

std::string name = "Rahul";

The <string> header should be included when using std::string.

16. sizeof Operator

The sizeof operator can be used to determine the size, in bytes, of an object or type.

#include <iostream>

int main() {

    int age = 20;

    std::cout << sizeof(age);

    return 0;
}

The result depends on the implementation and platform.

17. sizeof with Data Types

#include <iostream>

int main() {

    std::cout << sizeof(int) << std::endl;
    std::cout << sizeof(double) << std::endl;
    std::cout << sizeof(char) << std::endl;
    std::cout << sizeof(bool) << std::endl;

    return 0;
}

This program displays the sizes of several types on the current implementation.

18. Type Modifiers

C++ provides type specifiers and modifiers such as:

  • signed
  • unsigned
  • short
  • long

These can be used with integer types to affect their range and representation.

unsigned int count = 100;
long long population = 1000000000LL;

19. Integer Type Examples

short smallNumber = 10;
int number = 1000;
long largeNumber = 100000L;
long long veryLargeNumber = 9000000000LL;

Different integer types can be selected according to the range required by the program.

20. Floating-Point Examples

float price = 99.50f;
double salary = 25000.75;
long double measurement = 12345.6789L;

Floating-point types are useful for values that may contain a fractional part.

21. Data Type and Variable

A declaration combines a type with a variable name.

int age = 20;
double price = 99.99;
char grade = 'A';
bool passed = true;

The data type tells the compiler what kind of value each variable represents.

22. Data Type Conversion

A value can sometimes be converted from one type to another.

int number = 10;
double result = number;

Here, the integer value is converted to a double value when it is assigned to result.

23. Automatic Type Deduction

Modern C++ provides the auto keyword to let the compiler deduce the type from the initializer.

auto age = 20;
auto price = 99.50;

The compiler deduces an appropriate type from each initializer.

24. Data Types and Memory

Different types can have different sizes and alignment requirements. The exact size of many fundamental types is implementation-dependent.

std::cout << sizeof(int) << std::endl;
std::cout << sizeof(double) << std::endl;

Use sizeof when your program needs to know the size on the current implementation.

25. Choosing a Data Type

Choose a data type based on the kind of value and range your program requires.

  • Use int for common whole numbers.
  • Use double for many decimal calculations.
  • Use char for a single character.
  • Use bool for true/false information.
  • Use std::string for text.

26. Common Data Type Mistake

One common mistake is selecting a type that cannot represent the required value correctly.

int age = 25;
double price = 99.50;

For a value containing a fractional part, an appropriate floating-point type should normally be considered instead of an integer type.

27. Data Types in a Student Program

#include <iostream>
#include <string>

int main() {

    std::string name = "Amit";
    int age = 20;
    double marks = 87.5;
    char grade = 'A';
    bool passed = true;

    std::cout << "Name: " << name << std::endl;
    std::cout << "Age: " << age << std::endl;
    std::cout << "Marks: " << marks << std::endl;
    std::cout << "Grade: " << grade << std::endl;
    std::cout << "Passed: " << passed << std::endl;

    return 0;
}

28. Fundamental Data Type Examples

Data Type Example Used For
int int age = 20; Whole numbers
float float price = 10.5f; Floating-point values
double double salary = 25000.75; More precise floating-point values
char char grade = 'A'; Single character
bool bool passed = true; True/false values

29. Complete Data Types Example

#include <iostream>
#include <string>

int main() {

    int age = 21;
    float height = 5.8f;
    double salary = 25000.50;
    char grade = 'A';
    bool student = true;
    std::string name = "Rahul";

    std::cout << "Name: " << name << std::endl;
    std::cout << "Age: " << age << std::endl;
    std::cout << "Height: " << height << std::endl;
    std::cout << "Salary: " << salary << std::endl;
    std::cout << "Grade: " << grade << std::endl;
    std::cout << "Student: " << student << std::endl;

    return 0;
}

This example demonstrates several commonly used C++ data types in one program.

30. Data Types – Final Summary

C++ provides many data types so that programs can represent different kinds of information.

Type Example Purpose
short short n = 10; Short integer
int int age = 20; Common integer values
long long n = 100000L; Large integer values
long long long long n = 9000000000LL; Very large integer values
float float price = 10.5f; Floating-point values
double double price = 10.5; Floating-point values with more precision
char char grade = 'A'; Single character
bool bool passed = true; Boolean values
std::string std::string name = "Amit"; Text

📌 Key Points

  • A data type specifies the kind of value an object or variable can represent.
  • int is commonly used for whole numbers.
  • float, double, and long double are floating-point types.
  • char is used for a single character.
  • bool represents true or false.
  • std::string is commonly used for text.
  • short, long, and long long provide different integer ranges.
  • signed and unsigned can be used with integer types.
  • The exact size of many C++ types depends on the implementation.
  • The sizeof operator can be used to check the size of a type or object.
  • auto allows the compiler to deduce a variable's type from its initializer.

🧠 Quick Quiz

Question: Which data type is commonly used to store whole numbers in C++?