Data Types

 Data Types in C++

Data types in C++ define the kind of data that a variable can hold. They determine the size of the data (in memory) and the operations that can be performed on the data. C++ provides a wide range of built-in data types that can be categorised into:

  1. Basic (Primitive) Data Types
  2. Derived Data Types
  3. User-Defined Data Types

1. Basic (Primitive) Data Types

These are the most fundamental data types in C++ and are used to define variables that hold single values.

i) Integer Data Types

  • int: Used to store integer (whole) numbers.
    • Size: Typically 4 bytes (32 bits), but it can vary based on the system.
    • Range: Depends on system (usually -2,147,483,648 to 2,147,483,647 for 4 bytes).

Example:

int num = 10;

  • short: Used to store smaller integer values.
    • Size: Typically 2 bytes (16 bits).
    • Range: -32,768 to 32,767.

Example:

short num = 1000;

  • long: Used for storing larger integer values.
    • Size: Typically 4 bytes (32 bits), but it can be 8 bytes (64 bits) on some systems.
    • Range: Depends on system (usually -2,147,483,648 to 2,147,483,647 for 4 bytes).

Example:

long num = 1000000;

  • long long: Used to store even larger integer values.
    • Size: Typically 8 bytes (64 bits).
    • Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Example:

long long num = 100000000000;

ii) Floating-Point Data Types

Floating-point types are used to store numbers with fractional parts (decimals).

  • float: Used to store single-precision floating-point numbers.
    • Size: Typically 4 bytes (32 bits).
    • Range: Approx ±3.4E−38 to ±3.4E+38.

Example:

float pi = 3.14f;

  • double: Used to store double-precision floating-point numbers.
    • Size: Typically 8 bytes (64 bits).
    • Range: Approx ±1.7E−308 to ±1.7E+308.

Example:

double pi = 3.141592653589793;

  • long double: Used to store extended precision floating-point numbers.
    • Size: Typically 12 or 16 bytes (depends on the system).
    • Range: More precise than double.

Example:

long double pi = 3.14159265358979323846L;

iii) Character Data Type

  • char: Used to store a single character.
    • Size: Typically 1 byte (8 bits).
    • Range: -128 to 127 (signed) or 0 to 255 (unsigned), depending on the system.

Example:

char grade = 'A';

  • unsigned char: Stores non-negative values (0 to 255).
    • Size: 1 byte (8 bits).

Example:

unsigned char byteValue = 255;

iv) Boolean Data Type

  • bool: Used to store boolean values true or false.
    • Size: Typically 1 byte.
    • Range: true or false.

Example:

bool isActive = true;


2. Derived Data Types

Derived data types are based on primitive types but provide additional functionality.

  • Array: A collection of elements of the same data type stored in contiguous memory locations.

Example:

int arr[5] = {1, 2, 3, 4, 5};

Pointer

  • A variable that holds the memory address of another variable.

Example:

int num = 10;

int* ptr = #  // Pointer to integer variable


Reference

A reference variable is an alias for another variable. Once created, a reference cannot be changed to refer to a different variable.

Example:

int num = 10;

int& ref = num;  // Reference to num. 


Function

A function is a block of code that performs a specific task and can return a value.

Example:

int add(int a, int b) {

    return a + b;

}


3. User-Defined Data Types

C++ allows users to create new data types using existing ones, such as structures, classes, unions, and enumerations.

i) Structure (struct)

A structure is a user-defined data type that allows grouping different data types under one name.

Example:

struct Person {

    string name;

    int age;

    float height;

};

 

Person p1 = {"John", 25, 5.9};

ii) Class

A class is similar to a structure but with the added ability to define functions (methods) that operate on the data members.

Example:

class Rectangle {

public:

    int width, height;

 

    void area() {

        cout << "Area: " << width * height << endl;

    }

};

 

Rectangle rect;

rect.width = 5;

rect.height = 10;

rect.area();

iii) Union

A union is a data structure where all members share the same memory location. Only one member can store a value at a time.

Example:

union Data {

    int i;

    float f;

    char c;

};

 

Data data;

data.i = 10;

data.f = 3.14;

iv) Enum (Enumeration)

An enum is a user-defined type consisting of a set of named integer constants.

Example:

enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };

 

Day today = Wednesday;


4. Type Modifiers

C++ allows you to modify the behavior of some of the basic data types using type modifiers like signed, unsigned, short, and long.

  • signed: Specifies that a variable can store both negative and positive values.
  • unsigned: Specifies that a variable can only store non-negative values (0 or positive).
  • short: Modifies an integer to take less memory.
  • long: Modifies an integer to take more memory (usually 4 bytes to 8 bytes).

Example:

unsigned int positiveNumber = 1000;

signed int negativeNumber = -1000;


Size and Range of Data Types

Data Type

Size (Typical)

Range

char

1 byte

-128 to 127 (signed) or 0 to 255 (unsigned)

int

4 bytes

-2,147,483,648 to 2,147,483,647

short

2 bytes

-32,768 to 32,767

long

4 bytes

-2,147,483,648 to 2,147,483,647

long long

8 bytes

-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

float

4 bytes

±3.4E−38 to ±3.4E+38

double

8 bytes

±1.7E−308 to ±1.7E+308

long double

12 or 16 bytes

Extended precision (system dependent)

bool

1 byte

true or false

string

Variable

Depends on the size of the string


Post a Comment

0 Comments