Page Content

Tutorials

What Are The C++ Data Types With Code Examples?

C++ is tightly typed, thus you must provide variable types to help the compiler produce better code. Types, which define data entity contents and operations, are important to C++ programming. Because C++ is statically typed, the compiler must check each program name’s type during compilation.

C++’s built-in types effectively describe computer hardware capabilities like addresses, characters, and numbers, making them crucial. Another name for these types is primitive built-in types.

C++ Data Types

The built-in data types in C++ fall into the following general categories:

  • Integral Types: Show complete numbers.
  • Floating-Point Types: Use fractional portions to depict numbers.
  • Boolean Type: Symbolizes the values of logical truth.
  • Void Type: Denotes the lack of knowledge.

Integral Types

Whole numbers, or those without fractional components, are stored in integral types. Any fractional portions of the result are usually deleted when calculations are done with integers unless special measures are taken to keep them, like using the remainder operator %.

Typical integral types consist of:

  • char: Usually denotes a single character, which is equivalent to a single memory byte. The character code is its numerical value (for example, the ASCII value of ‘A’ is 65). Although char is frequently used for characters, it can also be used for numerical representation because it is essentially an integer type. There is a guarantee that the char type is at least 8 bits.
  • short / short int: A type of small integer. There is a guarantee that it has at least 16 bits.
  • int: The machine’s most natural integer size. Its size can differ from machine to machine and frequently adjusts to the length of a computer register (e.g., 32 bits for 32-bit computers, 16 bits for 16-bit computers). Int defaults to a 32-bit type on the majority of modern compilers.
  • long / long int: An integer type that is lengthy. It will be at least 32 bits, no matter what.
  • long long: This type, which is guaranteed to be at least 64 bits, was introduced by the new standard.

Signed and Unsigned Variants: Integral types can be signed or unsigned, with the exception of bool and extended character types.

  • Zero, positive numbers, and negative numbers can all be represented using a signed type. Int, short, long, and long long are signed by default.
  • Only values larger than or equal to zero are represented by an unsigned type. Adding unsigned to the type keyword yields the matching unsigned type (e.g., unsigned int, unsigned long). Unsigned is a shorthand for the type unsigned int.

An 8-bit unsigned char, for instance, can store values ranging from 0 to 255. The result is the remainder of that value modulo the amount of values the target type may contain if an out-of-range value (such as -1) is assigned to it (e.g., -1 becomes 255).

Example for Integer Data Types

#include <iostream> // Required for input/output operations
#include <limits>   // Required for numeric_limits to get type info
int main() {
    
    int myInteger = 100;
    short shortInt = 50;
    long longInt = 1234567890L; // 'L' suffix for long literal
    long long veryLongInt = 98765432LL; // 'LL' suffix for long long literal
    unsigned int positiveInteger = 200;
    
    std::cout << "myInteger: " << myInteger << std::endl;
    std::cout << "shortInt: " << shortInt << std::endl;
    std::cout << "longInt: " << longInt << std::endl;
    std::cout << "veryLongInt: " << veryLongInt << std::endl;
    std::cout << "positiveInteger: " << positiveInteger << std::endl;
    std::cout << "Size of int: " << sizeof(int) << " bytes" << std::endl;
    std::cout << "Size of short: " << sizeof(short) << " bytes" << std::endl;
    std::cout << "Size of long: " << sizeof(long) << " bytes" << std::endl;
    std::cout << "Size of long long: " << sizeof(long long) << " bytes" << std::endl;
    std::cout << "Min value of int: " << std::numeric_limits<int>::min() << std::endl;
    std::cout << "Max value of int: " << std::numeric_limits<int>::max() << std::endl;
    std::cout << std::endl;
    return 0; // Indicates successful execution
}

Output

myInteger: 100
shortInt: 50
longInt: 1234567890
veryLongInt: 98765432
positiveInteger: 200
Size of int: 4 bytes
Size of short: 2 bytes
Size of long: 8 bytes
Size of long long: 8 bytes
Min value of int: -2147483648
Max value of int: 2147483647

Code Example for Character Data Types

#include <iostream> // Required for input/output operations
#include <limits>   // Required for numeric_limits to get type info
int main() {
    
 int myInteger = 100;
    short shortInt = 50;
    long longInt = 1234567890L; // 'L' suffix for long literal
    long long veryLongInt = 98765432LL; // 'LL' suffix for long long literal
    unsigned int positiveInteger = 200;
    char myChar = 'A';
    char asciiValue = 65; // ASCII value for 'A'
    signed char signedChar = -100;
    unsigned char unsignedChar = 200;
    std::cout << "myChar: " << myChar << std::endl;
    std::cout << "asciiValue: " << asciiValue << std::endl;
    std::cout << "signedChar (as int): " << static_cast<int>(signedChar) << std::endl; // Cast to int to print numeric value
    std::cout << "unsignedChar (as int): " << static_cast<int>(unsignedChar) << std::endl; // Cast to int to print numeric value
    std::cout << "Size of char: " << sizeof(char) << " bytes" << std::endl;
    std::cout << std::endl;
    return 0; // Indicates successful execution
}

Output

myChar: A
asciiValue: A
signedChar (as int): -10
unsignedChar (as int): 200
Size of char: 1 bytes

Floating-Point Types

Floating-point types are denoted by a decimal point and represent numbers that may have a fractional part. They employ a mantissa-exponent format, which allows them to store extraordinarily large or small numbers. Usually, floating-point figures are kept with a predetermined level of accuracy.

These are the types of floating-point:

  • float: Value in single-precision floating points. At least six significant digits of precision are assured. For calculations, float frequently lacks sufficient precision.
  • double: Value in double-precision floating points. At least ten significant digits of precision are assured. For the majority of floating-point calculations, double is advised because of its accuracy and frequently insignificant or even faster performance as compared to float.
  • long double: Value in extended-precision floating points. It is assured to have at least 10 significant digits, albeit its accuracy is more likely to differ between implementations. Long double precision is typically superfluous and can result in high runtime costs.

It is crucial to avoid utilizing floating-point variables in situations when discrete values are required. Additionally, always test as >= or <= rather than using precise comparisons (== or!=) with floating-point numbers.

Example code for Floating-Point Data Types

#include <iostream> // Required for input/output operations
#include <limits>   // Required for numeric_limits to get type info
int main() {
    // 1. Integer Data Types
    int myInteger = 100;
    short shortInt = 50;
    long longInt = 1234567890L; // 'L' suffix for long literal
    long long veryLongInt = 98765432LL; // 'LL' suffix for long long literal
    unsigned int positiveInteger = 200;
    char myChar = 'A';
    char asciiValue = 65; // ASCII value for 'A'
    signed char signedChar = -100;
    unsigned char unsignedChar = 200;
    float myFloat = 10.5f;   // 'f' suffix for float literal
    double myDouble = 20.75;
    long double myLongDouble = 30.123456789123456789L; // 'L' suffix for long double literal
    std::cout << "myFloat: " << myFloat << std::endl;
    std::cout << "myDouble: " << myDouble << std::endl;
    std::cout << "myLongDouble: " << myLongDouble << std::endl;
    std::cout << "Size of float: " << sizeof(float) << " bytes" << std::endl;
    std::cout << "Size of double: " << sizeof(double) << " bytes" << std::endl;
    std::cout << "Size of long double: " << sizeof(long double) << " bytes" << std::endl;
    std::cout << std::endl;
    return 0; // Indicates successful execution
}

Output

myFloat: 10.5
myDouble: 20.75
myLongDouble: 30.1235
Size of float: 4 bytes
Size of double: 8 bytes
Size of long double: 16 bytes

Boolean Type

Truth values, which can be either true or false, are represented by the bool type. Internally, the numbers 1 and 0 are commonly used to indicate true and false, respectively. Compilers frequently store bool types as bytes for quicker access, even though they only need one bit of storage in theory. Bool is frequently used to store comparison results.

Code example for Boolean Data Type

#include <iostream> // Required for input/output operations
#include <limits>   // Required for numeric_limits to get type info
int main() {
    // 1. Integer Data Types
    int myInteger = 100;
    short shortInt = 50;
    long longInt = 1234567890L; // 'L' suffix for long literal
    long long veryLongInt = 98765432LL; // 'LL' suffix for long long literal
    unsigned int positiveInteger = 200;
    
    bool isTrue = true;
    bool isFalse = false;
    std::cout << "--- Boolean Data Type ---" << std::endl;
    std::cout << "isTrue: " << isTrue << std::endl;  // Prints 1 for true
    std::cout << "isFalse: " << isFalse << std::endl; // Prints 0 for false
    std::cout << "Size of bool: " << sizeof(bool) << " bytes" << std::endl;
    std::cout << std::endl;
    return 0; // Indicates successful execution
}

Output

isTrue: 1
isFalse: 0
Size of bool: 1 bytes

Void Type

A special-purpose type with no operations or associated values is called void. A variable with the type void cannot be defined. It is most frequently used to build generic pointers (void*) or as the return type for functions that do not return a value.

You can also read Simple Output With std::cout And endl With Code Examples

Agarapu Geetha
Agarapu Geetha
My name is Agarapu Geetha, a B.Com graduate with a strong passion for technology and innovation. I work as a content writer at Govindhtech, where I dedicate myself to exploring and publishing the latest updates in the world of tech.
Index