C++ Language
Data Types
Data types are used to specify the type of data that a variable can hold. C++ provides several built-in data types, and here is the list of all the fundamental C++ data types:
- Integer Types:
- 'int': Integer type, typically 32 bits. The range is from -2,147,483,648 to 2,147,483,647.
- 'short': Short integer, typically 16 bits. The range is from -32,768 to 32,767.
- 'long': Long integer, typically 32 bits. The range is the same as that of int.
- 'long long': Long long integer, typically 64 bits. Larger range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
- Floating-point Types:
- 'float': Single-precision floating-point. Approximate range: 1.2e-38 to 3.4e38.
- 'double': Double-precision floating-point. Larger range: 2.3e-308 to 1.7e308.
- 'long double': Extended precision floating-point. The range and size depend on the implementation.
- Character Types:
- 'char': Character type, typically 8 bits. Represents a single character.
- 'wchar_t': Wide character type, used for handling larger character sets. Size is platform-dependent.
- 'char16_t' and 'char32_t': Unicode character types, used for handling 16-bit and 32-bit Unicode characters, respectively.
- Boolean Type:
- 'bool': Represents boolean values. It can take on the values 'true' or 'false'.
Data Type | Occupied Size | Range | Format Specifier |
---|---|---|---|
int | 4 bytes | -2,147,483,648 to 2,147,483,647 | %d or %i |
short | 2 bytes | -32,768 to 32,767 | %hd |
long | 4 bytes | -2,147,483,648 to 2,147,483,647 | %ld |
long long | 8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | %lld |
float | 4 bytes | 3.4e-38 to 3.4e38 | %f |
double | 8 bytes | 1.7e-308 to 1.7e308 | %lf |
long double | 12 or 16 bytes | (platform-dependent) | %Lf or %Le |
char | 1 byte | 0 to 255 | %c |
wchar_t | 2 or 4 bytes | 0 to 65,535 (or larger) | %lc |
char16_t | 2 bytes | 0 to 65,535 | %c |
char32_t | 4 bytes | 0 to 4,294,967,295 | %c |
bool | 1 byte | true or false | %d or %s |
Note: Please note that the sizes and ranges mentioned here are typical for most systems, but they can vary depending on the compiler and platform.
Here's a simple example demonstrating the use of these data types:
#include<iostream> using namespace std; int main() { int integerNumber = 42; float floatingPointNumber = 3.14; char character = 'A'; bool isTrue = true; cout << "Integer: " << integerNumber << endl; cout << "Float: " << floatingPointNumber << endl; cout << "Character: " << character << endl; cout << "Boolean: " << isTrue << endl; return 0; }
Integer: 42 Float: 3.14 Character: A Boolean: 1
* In this example, variables of different fundamental data types are declared and assigned values, and their values are printed to the console.
Escape Sequence
escape sequences are special sequences of characters that represent non-printable or special characters. Escape sequences are preceded by a backslash \. Here are some commonly used escape sequences in C++:
Escape Sequence | Non-graphic Character |
---|---|
\a | Audible bell (alert) |
\b | Backspace |
\f | Formfeed |
\n | Newline or linefeed |
\r | Carriage Return |
\t | Horizontal tab |
\v | Vertical tab |
\\ | Backslash |
\' | Single quote |
\" | Double quote |
\? | Question mark |
\On | Octal number |
\xHn | Hexadecimal number |
\0 | Null |
What's Next?
We've now entered the finance section on this platform, where you can enhance your financial literacy.