2.1) Fundamental data types in C++

Fundamental data types in C++ represent the basic building blocks for storing different types of data, such as integers, floating-point numbers, characters, and boolean values.

Let’s explore these data types in detail with code examples and explanations.

1. Integer Types

C++ provides several integer data types with varying ranges:

  • int: Typically 4 bytes, capable of storing whole numbers.
  • short: Shorter integer type, usually 2 bytes.
  • long: Longer integer type, usually 4 bytes.
  • long long: Extended integer type, usually 8 bytes.
#include <iostream>
using namespace std;

int main() {
    int myInt = 42;
    short myShort = 12345;
    long myLong = 1234567890;
    long long myLongLong = 123456789012345;

    cout << "int: " << myInt << endl;
    cout << "short: " << myShort << endl;
    cout << "long: " << myLong << endl;
    cout << "long long: " << myLongLong << endl;

    return 0;
}

Output:

int: 42
short: 12345
long: 1234567890
long long: 123456789012345

2. Floating-Point Types

Floating-point types are used to store real numbers with decimal points:

  • float: Single-precision floating-point type.
  • double: Double-precision floating-point type.
  • long double: Extended precision floating-point type.
#include <iostream>
using namespace std;

int main() {
    float myFloat = 3.14f;
    double myDouble = 1234.56789;
    long double myLongDouble = 123456789.123456789;

    cout << "float: " << myFloat << endl;
    cout << "double: " << myDouble << endl;
    cout << "long double: " << myLongDouble << endl;

    return 0;
}

Output:

float: 3.14
double: 1234.57
long double: 1.23457e+08

3. Character Types

Character types represent individual characters:

  • char: Represents a single character.
  • wchar_t: Represents a wide character.
#include <iostream>
using namespace std;

int main() {
    char myChar = 'A';
    wchar_t myWideChar = L'ß';

    cout << "char: " << myChar << endl;
    wcout << "wchar_t: " << myWideChar << endl;

    return 0;
}

Output:

char: A
wchar_t: ß

4. Boolean Type

The bool type represents boolean values true and false:

#include <iostream>
using namespace std;

int main() {
    bool isTrue = true;
    bool isFalse = false;

    cout << "isTrue: " << isTrue << endl;
    cout << "isFalse: " << isFalse << endl;

    return 0;
}

Output:

isTrue: 1
isFalse: 0

Explanation:

  • The int, short, long, and long long types store whole numbers with varying ranges.
  • The float, double, and long double types store real numbers with different levels of precision.
  • The char type stores individual characters, and wchar_t stores wide characters.
  • The bool type represents boolean values.

Understanding these fundamental data types is crucial for effectively working with various types of data in C++ programs.

Comparison of fundamental data types in C++

Data TypeSize (bytes)RangeDescription
int4-2,147,483,648 to 2,147,483,647Integer type for whole numbers
short2-32,768 to 32,767Shorter integer type
long4-2,147,483,648 to 2,147,483,647Longer integer type
long long8-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807Extended integer type
float4System-dependentSingle-precision floating-point type
double8System-dependentDouble-precision floating-point type
long doubleSystem-dependentSystem-dependentExtended precision floating-point type
char1-128 to 127Character type
wchar_tSystem-dependentSystem-dependentWide character type
bool1false or true (0 or non-zero)Boolean type

Leave a Reply