Curriculum
Course: Complete C++ Programming Course
Login

Curriculum

Complete C++ Programming Course

Text lesson

Understanding the Data Types in C++

In this lesson, you will learn.

  • Data Types in C++
  • Examples

 

Data Types in C++

  • In C++, data types define the type of data that can be stored in a variable.
  • There are several data types in C++, broadly categorized into three groups: primitive data types and derived data types,and userdefined datatypes.

Here’s a pictorial representation of each data types in C++.

 

DataTypes in C++

 

Example: Using Primitive Data Types

#include<iostream>


int main() {
    // Integer type
    int age = 25;
    std::cout << "Age: " << age << std::endl;

    // Floating-point type
    float weight = 70.5;
    std::cout << "Weight: " << weight << " kg" << std::endl;

    // Double-precision floating-point type
    double height = 175.25;
    std::cout << "Height: " << height << " cm" << std::endl;

    // Character type
    char initial = 'A';
    std::cout << "Initial: " << initial << std::endl;

    // Boolean type
    bool isStudent = true;
    std::cout << std::boolalpha; // To print bool values as true/false instead of 1/0
    std::cout << "Is student: " << isStudent << std::endl;

    // Demonstrating arithmetic operations with primitive types
    int num1 = 10, num2 = 20;
    int sum = num1 + num2;
    int difference = num1 - num2;
    int product = num1 * num2;
    double quotient = static_cast(num1) / num2;

    std::cout << "\nArithmetic Operations:" << std::endl;
    std::cout << num1 << " + " << num2 << " = " << sum << std::endl;
    std::cout << num1 << " - " << num2 << " = " << difference << std::endl;
    std::cout << num1 << " * " << num2 << " = " << product << std::endl;
    std::cout << num1 << " / " << num2 << " = " << quotient << std::endl;

    // Demonstrating type casting
    double preciseResult = 10.0 / 3.0;
    int roundedResult = static_cast(preciseResult);

    std::cout << "\nType Casting:" << std::endl;
    std::cout << "Precise result: " << preciseResult << std::endl;
    std::cout << "Rounded result: " << roundedResult << std::endl;

    return 0;
}


Explanation

In C++, static_cast is a type of casting operator used for performing explicit type conversions. It is used to convert one type to another in a way that is checked at compile time.

Syntax

static_cast<new_type>(expression)

Examples: Basic Type Conversion

int main() {
    int a = 10;
    double b = static_cast(a); // Convert int to double
    std::cout << "Value of b: " << b << std::endl; // Output: 10.0
    return 0;
}



 

Summary Table of C++ Data Types

Here’s a summary table of the various data types in C++ including their size, range, example, and default values

Data TypeSize (bytes)RangeExampleDefault Value
Basic Data Types    
int4-2,147,483,648 to 2,147,483,647int x = 42;0
short int2-32,768 to 32,767short int y = 10;0
long int4 or 8-2,147,483,648 to 2,147,483,647 or larger depending on platformlong int z = 100000L;0
long long int8-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807long long int a = 123456789LL;0
unsigned int40 to 4,294,967,295unsigned int b = 50U;0
float41.2E-38 to 3.4E+38float pi = 3.14f;0.0f
double82.3E-308 to 1.7E+308double e = 2.71828;0.0
long double8 or 163.4E-4932 to 1.1E+4932long double ld = 3.141592653589793238L;0.0L
char1-128 to 127char ch = 'A';‘\0’
bool1true or falsebool flag = true;false
Derived Data Types    
int[] (Array)variesdepends on sizeint arr[3] = {1, 2, 3};
int* (Pointer)4 or 8address space dependentint* ptr = &x;nullptr
int& (Reference)same as the referenced typesame as the referenced typeint& ref = x;N/A
User-Defined Data Types    
structvariesdepends on membersstruct Person { string name; int age; };N/A
unionvariesdepends on the largest memberunion Data { int i; float f; };N/A
enumvariestypically 4 bytes, range depends on valuesenum Color { RED, GREEN, BLUE };N/A

 

Important Points!

  1. The size and range of the primitive datatypes may vary based on the processor types and compiler.
  2. However, the character size is 1 byte in all cases.
  3. C++ has added two more primitive datatypes i.e. bool, and wchar_t(wide character).
  4. The compiler specifies the size and range of basic types in the <climits> header files.
  5. The range of the data types is -2(n-1) to 2(n-1) -1, where n is the number of bits.
  6. The void is not considered a primitive data type in C++. Instead, void is a special type that indicates the absence of a value. It is used in different contexts to specify that a function does not return a value or that a pointer is of an unspecified type.

 

The amount of memory occupied by the integer types is system dependent. On a 32-bit system such as Windows, an int occupies 4 bytes (which is 32 bits) of memory.

 

Modifiers in C++

The basic datatypes may have modifiers preceding them to alter the meaning of base type. The following are the modifiers in C++.

Modifiers in C++

Note:

  1. Modifiers like signed, unsigned, long, and short can be applied to integer base types.
  2. You can apply signed and unsigned characters.
  3. You can apply long to double.
  4. If you use the type modifier itself, then int is assumed

Example

S.NSpecifierSame As
1Signedsigned int
2Unsignedunsigned int
3longlong int
4shortshort int

 

The type long always occupies four bytes, which is the same as type int on 32-bit Windows systems. It can also be written as long int; this means the same as long.

 

Why long as it has the same size as of int i.e. 4 bytes?

Answer: If your program may need to run on a 16-bit system such as MS-DOS, or on older versions of Windows, specifying type long will guarantee a four-bit integer type. In 16-bit systems, type int has the same range as type short(i.e.  bytes).

If you want to create a constant of type long, use the letter L following the numerical value, as in

long longvar = 7678L; // assigns long constant 7678 to longvar.

 

Character Variables

  • Type char stores integers that range in value from –128 to 127.
  • Standard C++ provides a larger character type called wchar_t to handle foreign languages as char is not able to handle foreign languages due to small size.

Note:

  • The size of wchar_t in C++ is implementation-defined, meaning it can vary depending on the compiler, operating system, and platform. It may of 2 or 4 byte as per the size of 16 or 32 OS.
     

Example: Size of All Primitive Data Types

Here’s a simple C++ program that displays the size (in bytes) of all primitive data types using the sizeof operator:

#include<iostream>
using namespace std;

int main() {
    cout << "Size of primitive data types in C++ (in bytes):\n";
    cout << "-----------------------------------------------\n";
    cout << "char: " << sizeof(char) << " byte(s)\n";
    cout << "bool: " << sizeof(bool) << " byte(s)\n";
    cout << "short: " << sizeof(short) << " byte(s)\n";
    cout << "int: " << sizeof(int) << " byte(s)\n";
    cout << "long: " << sizeof(long) << " byte(s)\n";
    cout << "long long: " << sizeof(long long) << " byte(s)\n";
    cout << "float: " << sizeof(float) << " byte(s)\n";
    cout << "double: " << sizeof(double) << " byte(s)\n";
    cout << "long double: " << sizeof(long double) << " byte(s)\n";
    wchar_t w;
    cout << "wchar_t: " << sizeof(wchar_t) << " byte(s)\n";

    return 0;
}

Output

Size of primitive data types in C++ (in bytes):
-----------------------------------------------
char: 1 byte(s)
bool: 1 byte(s)
short: 2 byte(s)
int: 4 byte(s)
long: 4 byte(s)
long long: 8 byte(s)
float: 4 byte(s)
double: 8 byte(s)
long double: 16 byte(s)
wchar_t: 4 byte(s)

Note: The size may vary between systems (e.g., 32-bit vs 64-bit). Always test on your system.

 

Example: Range of All Primitive Data Types

#include<iostream>
#include<climits>      // For INT_MIN, INT_MAX, etc.
#include<cfloat>      // For FLT_MIN, FLT_MAX, etc>

using namespace std;

int main() {
    cout << "Range of Primitive Data Types in C++:\n";
    cout << "--------------------------------------\n";

    // Integer types
    cout << "char: " << CHAR_MIN << " to " << CHAR_MAX << endl;
    cout << "unsigned char: 0 to " << UCHAR_MAX << endl;

    cout << "short: " << SHRT_MIN << " to " << SHRT_MAX << endl;
    cout << "unsigned short: 0 to " << USHRT_MAX << endl;

    cout << "int: " << INT_MIN << " to " << INT_MAX << endl;
    cout << "unsigned int: 0 to " << UINT_MAX << endl;

    cout << "long: " << LONG_MIN << " to " << LONG_MAX << endl;
    cout << "unsigned long: 0 to " << ULONG_MAX << endl;

    cout << "long long: " << LLONG_MIN << " to " << LLONG_MAX << endl;
    cout << "unsigned long long: 0 to " << ULLONG_MAX << endl;

    cout << endl;

    // Floating-point types
    cout << "float: " << FLT_MIN << " to " << FLT_MAX << endl;
    cout << "double: " << DBL_MIN << " to " << DBL_MAX << endl;
    cout << "long double: " << LDBL_MIN << " to " << LDBL_MAX << endl;

    cout << endl;

    // Boolean
    cout << "bool: " << false << " (false) to " << true << " (true)" << endl;

    return 0;
}

Output

Range of Primitive Data Types in C++:
--------------------------------------
char: -128 to 127
unsigned char: 0 to 255
short: -32768 to 32767
unsigned short: 0 to 65535
int: -2147483648 to 2147483647
unsigned int: 0 to 4294967295
long: -2147483648 to 2147483647
unsigned long: 0 to 4294967295
long long: -9223372036854775808 to 9223372036854775807
unsigned long long: 0 to 18446744073709551615

float: 1.17549e-38 to 3.40282e+38
double: 2.22507e-308 to 1.79769e+308
long double: 3.3621e-4932 to 1.18973e+4932

bool: 0 (false) to 1 (true)

 

 


End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

5.0
5.0 out of 5 stars (based on 1 review)
Excellent
Very good
Average
Poor
Terrible

 

 

30 September 2024

A great learning platform

 

 

Submit a Review