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.
Table of Contents
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
, andlong long
types store whole numbers with varying ranges. - The
float
,double
, andlong double
types store real numbers with different levels of precision. - The
char
type stores individual characters, andwchar_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 Type | Size (bytes) | Range | Description |
---|---|---|---|
int | 4 | -2,147,483,648 to 2,147,483,647 | Integer type for whole numbers |
short | 2 | -32,768 to 32,767 | Shorter integer type |
long | 4 | -2,147,483,648 to 2,147,483,647 | Longer integer type |
long long | 8 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | Extended integer type |
float | 4 | System-dependent | Single-precision floating-point type |
double | 8 | System-dependent | Double-precision floating-point type |
long double | System-dependent | System-dependent | Extended precision floating-point type |
char | 1 | -128 to 127 | Character type |
wchar_t | System-dependent | System-dependent | Wide character type |
bool | 1 | false or true (0 or non-zero) | Boolean type |