Primitive Data Types
Primitive types are the basic data types serve as the building blocks of data manipulation in any programming language including Java.
In a programming language, a variable has a type such as Integer, String, Decimal, … whether that programming language is a Static Typing such as C/C++ and Java or Dynamic Typing like PHP, javascript and Python.
In C++ like other programming languages we have built-in primitive data types. A primitive date type is a basic data type provided by a programming language as a basic building block.
Here is the list of primitive date types in C++:
Group | Keyword | Size |
Boolean Types | bool | 8 bits |
Character Types | char | 8 bits |
Character Types | char16_t | 16 bits |
Character Types | char32_t | 32 bits |
Character Types | wchar_t | gt 16 bits and lt 32 bits |
Integer Types | char | 8 bits |
Integer Types | int | gt 16 bits and lt 32 bits |
Integer Types | short | 16 bits |
Integer Types | long | 32 bits |
Integer Types | long long | 64 bits |
Floating-point Types | float | 32 bits |
Floating-point Types | double | gt 32 bits and lt 64 bits |
Floating-point Types | long double | 64 bits, lt double |
Void Type | void | no storage |
I created couple of samples in
https://github.com/mycpptutorial/primitive-data-types.git to play with each type.
In the first sample in trybool.cpp, you will learn how to define a boolean variable and how to set its value:
/** trybool.cpp**/
#include <iostream>
int main (void){
bool try_me = false;
std::cout << "try_me value: ";
std::cout << try_me;
std::cout << "\n";
try_me = true;
std::cout << "try_me value: ";
std::cout << try_me;
std::cout << "\n";
std::cout << "The size of try_me is ";
std::cout << sizeof try_me;
std::cout << " byte(s)\n";
}
You start all C++ executable application with a starter function called main(). Refer to Build You First Application to have more information about it. As you see in the above sample, in the first line of main() function block, I defined a boolean variable simply by have bool type at the beginning of the line, then the variable name try_me right after that with a space character and assigned its value immediately on its declaration (definition) line with “= false“. Notice that all lines are ended by ; (semicolon) character.
bool try_me = false;
Next, I have the text “The size of try_me is “ printed with a cout function and belongs to std namesapce as std::cout.
std::cout << "try_me value: ";