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++:

GroupKeywordSize
Boolean Typesbool8 bits
Character Typeschar8 bits
Character Typeschar16_t16 bits
Character Typeschar32_t32 bits
Character Typeswchar_tgt 16 bits and lt 32 bits
Integer Typeschar8 bits
Integer Typesintgt 16 bits and lt 32 bits
Integer Typesshort16 bits
Integer Typeslong32 bits
Integer Typeslong long64 bits
Floating-point Typesfloat32 bits
Floating-point Typesdoublegt 32 bits and lt 64 bits
Floating-point Typeslong double64 bits, lt double
Void Typevoidno 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: ";

Leave a Reply