C++ Data Types
The data type is a category of data. In other words, a data type is a collection of data values with similar characteristics. In the C++ programming language, every variable needs to be created with a data type.
The data type of a variable specifies the following.
- The type of value can be stored in a variable.
- The amount of memory in bytes has to be allocated for a variable.
- The range of values has to be stored in a variable.
The C++ programming language supports the following data types.
- Integer Data type
- Floating-point Data type
- Double Data type
- Character Data type
- Boolean Data type
- Void Data type
- Wide Character Data type
Integer Data type
An integer is a number without a decimal part. For example, the value 10 is an integer. In c++ integer data type has the following characteristics.
- Integer data type is represented using the keyword “int”.
- The integer value is represented using the type specifier “%d” or “%i".
- The integer data type allows storing any value in the range from -32768 to 32767. However, it may change using type modifier.
Floating-point Data type
A floating-point value is a number with a decimal part. For example, the value 10.65 is a floating-point. In c++ floating-point data type has the following characteristics.
- Floating-point data type is represented using the keyword “float”.
- The floating-point value is represented using the type specifier “%f”.
- The floating-point data type allows storing any value in the range from 2-31 to 231.
- The floating-point data type does not allows type modifier.
- The floating-point data type allows upto 6 decimal points.
Double Data type
The double data type is similar to the floating-point data type but it allows a wide range of values. The double data type has the following characteristics.
- The double data type is represented using the keyword “double”.
- The double value is represented using the type specifier “%ld”.
- The double data type allows storing any value in the range from 2-63 to 263.
- The double data type does not allows type modifier except long.
- The double data type allows double precision upto 12 decimal points.
Character Data type
A character is a symbol enclosed in a single quotation. Here, the symbol may be anything like an alphabet, a digit, or a special symbol. In C++, the character data type has the following characteristics.
- The character data type is represented using the keyword “char”.
- The character value is represented using the type specifier “%c” or "%s".
- The character data type allows storing any value in the range from -128 to 127.
- The character data type allows only signed and unsigned type modifiers.
Boolean Data type
The boolean data type contains only two values 0 and 1. Here the value 0 represents false, and 1 represents true. The boolean data type has the following characteristics.
- The boolean data type is represented using the keyword “bool”.
- The boolean value is represented using the type specifier “%d”.
- The boolean data type allows storing only 0 and 1.
- The boolean data type does not allows type modifier.
Void Data type
The void data type is a value less data type, and it represents nothing. The void data type has the following characteristics.
- The void data type is represented using the keyword “void”.
- The void value does not have any type specifier.
- The void data type does not allows storing any value.
- The void data type does not allows type modifier.
Wide character Data type
The wide-character data type is similar to character data type, but it takes 2 bytes of memory. It allows a wide range of character values compare to the character data type. This data type has supported by the latest compilers only. The keyword wchar_t is used to represent wide character data type.
The following table describes the characteristics of each data type briefly.
Data type | Keyword | Type specifier | Memory size | Range |
---|---|---|---|---|
Short Integer | short int | %d or %i | 2 bytes | -32768 to +32767 |
Long Integer | long int or int | %d or %i | 4 bytes | 2-31 to 231 |
Signed Integer | int or short int or long int | %d or %i | 2 or 4 bytes | -32768 to +32767 or 2-31 to 231 |
Unsigned Integer | unsigned int | %u | 2 or 4 bytes | 0 to +65535 or 0 to 232-1 |
Floating-point | float | %f | 4 bytes | +/- 3.4e +/- 38 (~7 digits) |
Double | double or long double | %ld | 8 bytes | +/- 1.7e +/- 308 (~15 digits) |
Signed Character | char or signed char | %c or %s | 1 bytes | -128 to +127 |
Unsigned Character | unsigned char | %c or %s | 1 bytes | 0 to +255 |
Boolean | bool | %d or none | 1 bit | 0 or 1 |
Void | void | none | No memory | No value |
Wide Character | wchar_t | %c or %s | 2 bytes | 2-15 to 215 |