In this lesson, you will learn about fundamental C++ Data Types like int, float, char, etc., with examples to better understand the topic.
Data types are variables’ declarations in C++. The declaration shows the type and size of data related to variables.
Example
int height= 7;
Height is an int variable in this example. The variable can only hold integers that are either 2 or 4 bytes long.
The essential data kinds, their definitions, and their sizes (in bytes) are displayed in the table below:
Data Type | Syntax | Size (in Bytes) |
---|---|---|
Integer | int | 2 or 4 |
Floating-point | float | 4 |
Double Floating-point | double | 8 |
Character | char | 1 |
Wide Character | wchar_t | 2 |
Boolean | bool | 1 |
Empty | void | 0 |
Let’s now go into more detail about these essential data types.
The int keyword is used to identify integers. Usually, it is 4 bytes in size and can therefore store values between -2147483648 and 2147483647.
Example
int carPrice = 8500000;
Floating-point numbers are stored using the C++ functions float
and double
(decimals and exponentials). Floats are 4 bytes in size, while doubles are 8 bytes, and double thus has twice the precision of float. You can use these two data types for exponentials, as already mentioned.
Example
Float subjectMarks= 36.74; double totalPercentage = 48.64534; double lengthOfTower = 8E10 // 8E10 = 8*10^10
The term char refers to characters, and it has a 1-byte size. In C++, characters are encased in single quotes, or “”. Note that a char variable in C++ stores an integer value rather than a specific character.
Example
char firstName = "s";
Similar to the char data type, the wide-character wchar_t has a size of two bytes as opposed to one. wchar_t represents characters that need more memory than a single char. Two additional fixed-size character types are introduced in C++11: char16_t and char32_t.
Example
wchar firstName = L ‘S’;
The only two valid values for the bool data type are true or false. Loops and conditional statements both employ Booleans.
Example
bool isEmpty = false;
The void
keyword denotes a lack of information and emptiness or lack of worth. When we learn about functions and pointers, we will discuss voids in detail.
To determine the size of different data types, we use the sizeof()
operator. In the example below, endl
is used to add a new line after each line, and the operator sends multiple values to the screen.
// Program displays different datatypes size #include <iostream> using namespace std; int main() { cout << "char Size= " << sizeof(char) << endl; cout << "int Size= " << sizeof(int) << endl; cout << "float Size= " << sizeof(float) << endl; cout << "double Size= " << sizeof(double) << endl; cout << "wchar_t Size= " << sizeof(wchar_t) << endl; return 0; }
The result of running the previous code, which varies from machine to machine, is as follows:
Output
char Size= 1 int Size= 4 float Size= 4 double Size= 8 wchar_t Size= 4 bool Size= 1
We can further alter some core data types by employing type modifiers. To more precisely meet the requirements of various scenarios, a base type’s meaning is modified using a modifier. In C++, there are 4 type modifiers as follows:
With the modifiers mentioned above, the following data types are modifiable:
The short, long, signed, and unsigned are type modifiers in C++. The type modifiers signed, unsigned, long, and short with integer base data types are prefixed and used. Type modifier Long can also be used for double base data types, whereas type modifiers are signed and unsigned for char data types. All can be seen in the list below.
To more precisely meet the requirements of various scenarios, a base type’s meaning is modified using a modifier.
Datatype modifiers | Byte Size |
---|---|
char | 1byte |
unsigned char | 1byte |
signed char | 1byte |
int | 4bytes |
unsigned int | 4bytes |
signed int | 4bytes |
short int | 2bytes |
unsigned short int | 2bytes |
signed short int | 2bytes |
long int | 8bytes |
signed long int | 8bytes |
float | 4bytes |
double | 8bytes |
long double | 12bytes |
wchar_t | 2 or 4 bytes |
Using typedef
, you can give an existing type a new name. The easy syntax for using typedef
to define a new type is as follows:
typedef type name;
The following, for instance, informs the compiler that size is another name for int
.
typedef int size;
The following declaration generates an integer variable called size small, medium, and large and is entirely valid.
size small; size medium; size large;
An enumerated type declares a set of zero or more identifiers that may be used as the type’s values and an optional type name. Each enumerator is a constant with the enumeration as its type. It would help if you used the “enum” term to create an enumeration. An enumeration type’s general form is
enum nameOfEnumeration {collection of enum names} var-list; enum availableSize {small, medium, large} s; s = medium;
Here, the nameOfEnumeration
refers to the type name of the enumeration, and commas separate the names. For instance, the above code defines the variable s of type availableSize
, and the value “medium” is given to s.
The default number for the first name is 0; for the second name, it is 1; for the third name, it is 2, and so on. But if you add an initializer, you may give it a name and a specific value. In the following enumeration, for instance, you can give ‘medium’ the value of 10.
enum availableSize{small, medium =10, large};
Large will have a value of 11 in the enumeration of availableSize
; it’s one number greater than the name in the list preceding it.
This concludes the Syntax of the C++ Data Types lesson. In the next lesson, you will learn about the Variables in C++.