
So if we put that in simple words that mean that whenever we create or define a variable in our code we are simply reserving some space in memory for that variable.
So before we take a look at what are data types used in C++ we need to know what is Data Type.
What is Data Type:-
Data Types in any programming language is used to specify what kind of data or what type of value a variable has and what type of mathematical, logical or relational operations can be applied to it without causing any problem in the program, for example, string is used to store text and integer is used to store numbers here is the table for different types:-
Data Type
|
Used for
|
Example
|
| String | Alphanumeric characters | hello world, Alice, Bob123 |
| Integer | Whole numbers | 7, 12, 999 |
| Float (floating point) | Number with a decimal point | 3.15, 9.06, 00.13 |
| Character | Encoding text numerically | 97 (in ASCII, 97 is a lower case ‘a’) |
| Boolean | Representing logical values | TRUE, FALSE |
Primitive Built-in Data Types:-
C++ comes with built-in data types and it also allows the user to define their own data types. Here is the table of Data types in C++ :-| Type | Keyword |
|---|---|
| Boolean | bool |
| Character | char |
| Integer | int |
| Floating point | float |
| Double floating point | double |
| Valueless | void |
| Wide character | wchar_t |
- signed
- unsigned
- short
- long
Variable Types and Memory Used:-
This table shows the variable types and how much memory they take to store the value in memory, and the maximum and minimum values that can be stored in these type of variables.Character Data Types
| Data Type (Keywords) | Description | Size | Typical Range |
|---|---|---|---|
| char | Any single character. It may include a letter, a digit, a punctuation mark, or a space. | 1 byte | -128 to 127 or 0 to 255 |
| signed char | Signed character. | 1 byte | -128 to 127 |
| unsigned char | Unsigned character. | 1 byte | 0 to 255 |
| wchar_t | Wide character. | 2 or 4 bytes | 1 wide character |
Integer Data Types
| Data Type (Keywords) | Description | Size | Typical Range |
|---|---|---|---|
| int | Integer. | 4 bytes | -2147483648 to 2147483647 |
| signed int | Signed integer. Values may be negative, positive, or zero. | 4 bytes | -2147483648 to 2147483647 |
| unsigned int | Unsigned integer. Values are always positive or zero. Never negative. | 4 bytes | 0 to 4294967295 |
| short | Short integer. | 2 bytes | -32768 to 32767 |
| signed short | Signed short integer. Values may be negative, positive, or zero. | 2 bytes | -32768 to 32767 |
| unsigned short | Unsigned short integer. Values are always positive or zero. Never negative. | 2 bytes | 0 to 65535 |
| long | Long integer. | 4 bytes | -2147483648 to 2147483647 |
| signed long | Signed long integer. Values may be negative, positive, or zero. | 4 bytes | -2147483648 to 2147483647 |
| unsigned long | Unsigned long integer. Values are always positive or zero. Never negative. | 4 bytes | 0 to 4294967295 |
Floating-point Data Types
| Data Type (Keywords) | Description | Size | Typical Range |
|---|---|---|---|
| float | Floating point number. There is no fixed number of digits before or after the decimal point. | 4 bytes | +/- 3.4e +/- 38 (~7 digits) |
| double | Double precision floating point number. More accurate compared to float. | 8 bytes | +/- 1.7e +/- 308 (~15 digits) |
| long double | Long double precision floating point number. | 8 bytes | +/- 1.7e +/- 308 (~15 digits) |
Boolean Data Type
| Data Type (Keywords) | Description | Size | Typical Range |
|---|---|---|---|
| bool | Boolean value. It can only take one of two values: true or false. | 1 byte | true or false |
You can also use this table :-
| Type | Typical Bit Width | Typical Range |
|---|---|---|
| char | 1byte | -128 to 127 or 0 to 255 |
| unsigned char | 1byte | 0 to 255 |
| signed char | 1byte | -128 to 127 |
| int | 4bytes | -2147483648 to 2147483647 |
| unsigned int | 4bytes | 0 to 4294967295 |
| signed int | 4bytes | -2147483648 to 2147483647 |
| short int | 2bytes | -32768 to 32767 |
| unsigned short int | 2bytes | 0 to 65,535 |
| signed short int | 2bytes | -32768 to 32767 |
| long int | 8bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
| signed long int | 8bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
| unsigned long int | 8bytes | 0 to 18,446,744,073,709,551,615 |
| float | 4bytes | +/- 3.4e +/- 38 (~7 digits) |
| double | 8bytes | +/- 1.7e +/- 308 (~15 digits) |
| long double | 8bytes | +/- 1.7e +/- 308 (~15 digits) |
| wchar_t | 2 or 4 bytes | 1 wide character |
The size defined above is different for different compilers so to check the correct size of the various data types on your computer use this code.
</>
In this example code we are using endl, which is used to insert new line character after every line, and << operator is used to pass multiple values out to the screen. And the sizeof() operator is used to get the size of data types.#include <iostream>using namespace std;int main() {cout << "Size of char : " << sizeof(char) << endl;cout << "Size of int : " << sizeof(int) << endl;cout << "Size of short int : " << sizeof(short int) << endl;cout << "Size of long int : " << sizeof(long int) << endl;cout << "Size of float : " << sizeof(float) << endl;cout << "Size of double : " << sizeof(double) << endl;cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;return 0;}
This is the output that you are going to see:-
</>
Size of char : 1
Size of int : 4
Size of short int : 2
Size of long int : 8
Size of float : 4
Size of double : 8
Size of wchar_t : 4
typedef Declarations:
typedef is used to create a new name for existing type. This is the simple syntax to define the new type using typedef:-</>
typedef type newname;For example:-
</>
typedef int num;
This above example is telling the compiler that num is another name for int. And you can use it like this and it is now perfectly legal and creates an integer variable called length:num length;
Enumerated Types:
The enumerated type declares an optional type name and a set of zero or more identifier that can be used as values of the type.Here is how we can use it.
</>
enum enum-name {list of names}var-list;
Suppose we have an enum like the following:</>
enum Days {Saturday, Sunday, Tuesday,Wednesday, Thursday, Friday};
I want to create an instance of this enum and initialize it with a proper value, so I do:Days day = Days.Saturday;
Now I want to check my variable or instance with an existing enum value, so I do:if (day == Days.Saturday)
Thanks and this was all for the Fundamental C++ Data
Types so do share and if you want any articles do comment below so we
can help you learn.
Popular
Tags
Videos
0 comments:
Post a Comment