Skip to main content

C++ Built-in Data types.

Before diving into definition of various Data Types, let's first discuss something about memory.

Below is the block diagram of memory(ram) where all the data is temporarily stored before execution.


Memory.


The lower area of this diagram is referred as Low Memory address and upper area of this diagram is referred as High Memory address.

Now let's briefly discuss each of the segments, one by one.

Code Segment - The segment of memory where the text or code or your file is stored temporarily is referred as code segment. For example the main function, the function that we defined etc.

Data Segment - Data segment is of two types 
  1. Initialized data segment or simply data segment - The variables that are declared and initialized in our program are stored here.
  2. Uninitialized data segment.- The variables that are just declared or have value zero and not in use are stored here.
Heap - Heap is a storage where generally dynamic memory allocation takes place with the help of pointers. If we allocate some memory in heap, then it is a good practice to release it when not needed otherwise that will cause memory leak.(How to do it? We will see it in future).

Stack - Stack is a storage and it's functionality is just like a stack in real life (e.g. stack of plates in someone's marriage😋) i.e. LIFO - LAST IN FIRST OUT, 
First let's discuss LIFO - Take the same marriage example when we take a plate from the stack of plates we take the upper most(yeah there are some sh*theads who do not do that, but let's focus on the concept only, we take the upper most plate only and if we have to put something in the stack, we put it at the upper most plate itself, which hints us that we have to pick the plate which is lastly added (uppermost) to the stack.
Stack has various functionalities like storing function calls and storing automatic variables, What are automatic variables? The variables that allocated and de-allocated automatically unlike heap memory allocation where we have to do de-allocation manually. (We discuss heap memory allocation in future.)

Extra things to remember - 
  1. There are other languages like python and Java which has builtin garbage collecter, i.e. these language handle heap memory allocation automatically.
  2. The memory allocation of heap and stack are done in opposite direction,(see arrows in the diagram) like in the above diagram, Heap is allocated from low memory address  towards high memory address while stack in reverse of the following.

DATA TYPES

Data types are an essential part of any programming language. They are used to represent different types of data like integer, floating numbers and characters.

In C++ we can define variables/identifier of some specific data type and can store values of the assigned data type. example

integer a = 10, character b = 'b' etc.



Let's discuss these in this fashion.

  1. Built-in Data Types.
  2. Derived Data Types.
  3. User-Defined Data Types.

Built-in Data Types

Built-in Data types are the data types that C++ provides in a ready to use manner.
List of Built-in Data Types - 
  1. Integral Type.
  2. Floating Type.
  3. Void.
  4. Bool Type.
  5. Wide character Type.
Let's Start in the same order
  1. Integral Type - Integral is name derived from integers in mathematics and integers are the number that does not include any floating number for example 12345 is an integer while 1234.5 is not an integer. Where these integers are used? See below -
    1. To represent Integers (which is just an obvious thing). We use Integers to represent Integers. (Syntax for integers types in C++ int)
    2. To represent Characters, If you read my previous blogs too, then you must be familiar with ASCII (American Standard Code for Information Interchange). It just that, We use Integers to represent ASCII Value of character. (Syntax for character types in C++ is char)
  2. Floating Type - Now, we know we can represent non floating number by using integers but, just like in real life we need floating point number in computers too. With floating type we can represent numbers more precisely like 123.33 is a floating type number with precision up to two digits(i.e. 33). Furthermore we have nested types of the floating type -
    1. Floats - These are simple floats that I discussed already in the above definition. Float has maximum of 7 decimal digits of precision. (Syntax for Float type in C++ is float).
    2. Doubles - The name itself describes that it is doubly precised than floats i.e. doubles has maximum of 15 decimal digits of precision. We use doubles when we require very high amount of precision.(Syntax of Doubles in C++ is doubles)
  3. Void Type - The noun of void is "Completely empty space", The void type is little confusing, let's describe it in layman language. Void means nothing, which means that void don't store any type data and function of void type do not return any data. But, in C++ void pointers (void *) is a important thing(We will discuss it in derived data types).
  4. Bool Type - I know it's not mentioned in diagram, but it has to be discussed due to its importance. Boolean is data type which has only two value. True or False. In C++, you can also think it like if a variable has any value except 0 than it will be considered as true and if the value is 0 then it is False.
  5. Wide Character - We are familiar with character data type already but, what is this now, Let's see. We have discussed that c++ can understand English characters with the help of ASCII values, But, what about other international language like chinese? Yes, we can represent them also, but character data types is not sufficient for this because, it has 256 distinct integer values means we can represent only 256 distinct character, so due to this drawback of character data type we use wide character data type.(Syntax for Wide character in c++ is wchar_t). For ranges click here.
Ok, we discussed all these built-in data types, but, how we use them?

We use them to declare variables of the particular type,
Now, I would like to take your attention to two different terms.
  • Declaration - When we just declare our variable but, do not provide any initial value to the variable. But, do not think that if I did not defined the value than it must be zero, It can be zero but nobody knows what value the variable will have these types of values are called garbage values.
  • Definition or Initialization - When we just not declare the variable but also define its initial value is referred as defining or initializing a variable.
Important note - To use a variable you must declare it first.

Below, program shows declaration and definition in two different steps, It will be same for any other data type, by changing datatype written at the front.

Template for defining a variable - 

#include<iostream>
int main()
{
    int a;  // Declaring variable of type integer.
    a = 10; // Initializing a with value 10. 
    return 0;
}

Below, program shows declaration and definition in one step, It will be same for any other data type.

#include<iostream>
int main()
{
    int a = 10; // Declaration and initialization in one step
    return 0;
}

( Important Note - Don't forget semicolon after every statement you use, I will be notifying when we need or not need semicolon)

Now, this is the perfect time to define the difference between a variable and an identifier.

  • Variable - It is a name that we give to some value in the program. From the name itself it is clear that variable is some kind of variation i.e. values of a variable can change throughout the program at any instant of the program.
  • Identifier - It is a name that we give just only to some value but to anything in our program like functions, classes etc. You can think of Variable is just a Identifier, but identifier is any name that we use to represent any value, function or class etc.
( We will discuss more in later section of this slide.)
Now I will be showing you a program that gives you the size of the datatypes and the ranges of those data types.

#include<iostream>
#include<climits> // limits of integral data types defined in this header file
#include<float.h> // limits of float data types defined in this header file
int main()
{
    /*
    Program to Show size of data types in bytes and
    ranges from minimum value to maximum value.
    */

    std::cout << "Size of integer in bytes : " << sizeof(int) << std::endl;
    std::cout << "Range of integer is : " << INT_MIN << " to " << INT_MAX << std::endl << std::endl;
    
    std::cout << "Size of character in bytes : " << sizeof(char) << std::endl;
    std::cout << "Range of character is : " << CHAR_MIN << " to " << CHAR_MAX << std::endl << std::endl;
    
    std::cout << "Size of float in bytes : " << sizeof(float) << std::endl;
    std::cout << "Range of float is : " << FLT_MIN << " to " << FLT_MAX << std::endl << std::endl;
    
    std::cout << "Size of double in bytes : " << sizeof(double) << std::endl;
    std::cout << "Range of double is : " << DBL_MIN << " to " << DBL_MAX << std::endl << std::endl;

    std::cout << "Size of bool in bytes : " << sizeof(bool) << std::endl;
    std::cout << "No concept of ranges." << std::endl << std::endl; // Bool has only two value True and False, no concept of range.

    std::cout << "size of Wide character in bytes : " << sizeof(wchar_t) << std::endl;
    std::cout << "Range of Wide character is : " << WCHAR_MIN << " to " << WCHAR_MAX << std::endl << std::endl;
    
    return 0;
}

output is - 

Size of integer in bytes : 4
Range of integer is : -2147483648 to 2147483647

Size of character in bytes : 1
Range of character is : -128 to 127

Size of float in bytes : 4
Range of float is : 1.17549e-038 to 3.40282e+038

Size of double in bytes : 8
Range of double is : 2.22507e-308 to 1.79769e+308

Size of bool in bytes : 1
No concept of ranges.

size of Wide character in bytes : 2
Range of Wide character is : 0 to 65535

Extra things to remember is - One byte contains 8 bits of memory.

Now, if you think "std::" every time exhaustive you can replace std::cout with cout and std::endl with endl by writing using namespace std; at the start of your program.

#include<iostream>
#include<climits> // limits of integral data types defined in this header file
#include<float.h> // limits of float data types defined in this header file
using namespace std; // namespaces will be discussed in
int main()
{
    /*
    Program to Show size of data types in bytes and
    ranges from minimum value to maximum value.
    */

    cout << "Size of integer in bytes : " << sizeof(int) << endl;
    cout << "Range of integer is : " << INT_MIN << " to " << INT_MAX << endl << endl;
    
    cout << "Size of character in bytes : " << sizeof(char) << endl;
    cout << "Range of character is : " << CHAR_MIN << " to " << CHAR_MAX << endl << endl;
    
    cout << "Size of float in bytes : " << sizeof(float) << endl;
    cout << "Range of float is : " << FLT_MIN << " to " << FLT_MAX << endl << endl;
    
    cout << "Size of double in bytes : " << sizeof(double) << endl;
    cout << "Range of double is : " << DBL_MIN << " to " << DBL_MAX << endl << endl;

    cout << "Size of bool in bytes : " << sizeof(bool) << endl;
    cout << "No concept of ranges." << endl << endl; // Bool has only two value True and False, no concept of range.

    cout << "size of Wide character in bytes : " << sizeof(wchar_t) << endl;
    cout << "Range of Wide character is : " << WCHAR_MIN << " to " << WCHAR_MAX << endl << endl;
    
    return 0;
}

Output will be same.

( Important Note - I didn't specified sizeof(void) because void has no size but, even if you try to run that you will get a warning/error and output as "1")

There are also Modifiers present in C++ that helps us to modify the values of data types.

Let's discuss all of them.

  1. long - increase the ranges of data types.
  2. short - decrease the ranges of data types.
  3. unsigned - make the value range, ranges from 0 to (max range without modifier - min range without modifier).
  4. signed - almost every data type is signed by default. In signed data types 1 bit is, which is the Most Significant bit(MSB) is reserved to determine the sign of the value. If the value of MSB is 1 then the value is negative and if the MSB is 0 the value if positive.
Important notes -
  1.  Not every modifier can be used with every data types, you should try to find out it by yourself.
For complete ranges of Integers using these modifiers click here.

Links for Derived Datatypes and User-Defined Datatypes.

 Stay tuned for the upcoming content. For any query leave a comment below.


Click the Subscribe button at the top, to follow my every post regarding c++.

References -
  • Wikipedia.
  • hilite.com for code snippets.
  • ideone.com

Comments

Post a Comment

Popular posts from this blog

Introduction to Computer Science.

 Before directly jumping deeply in c++, let's first start by creating the roots of computer science. In this post we will answer the following question. What is Computer and How it works? What is Program and Operating system? Low Level vs High Level Language? Compiler v/s Interpreter? By Knowing All these basic concepts You will be able to understand the upcoming more complex concepts easily. Let's start answering the above questions. What is Computer and How it works? What is computer? If I answer briefly,  what is computer?  then it is just a calculator used for doing simple calculations. If I have to answer where is computer used?, then I could probably say everywhere. We are surrounded by computer, for example mobile, smartwatches, and personal computer (obviously).  The below image is the first mechanical computer that we used for calculation.                                        Abacus How computer works? The computer's internal functionality is a whole separate bra

C-style strings vs String Class.

Here, we are going to learn about, what are C-Style Strings and what is String class . So, let's start by giving you the introduction. C-Style String is nothing but, an array of characters, and from the term array we can surely assume that these are static in size i.e. the size of these C-Style strings cannot be increased or decreased. String Class is a Built-in class that provide us much more functionality than C-Style String. C-STYLE STRINGS Before learning the String class, which is full-fledged feature of C++, we should rather start by taking a look at some important things about C-Style String. C-Style Strings are actually array of characters that are terminated by a null character "\0". If you are confused with, why null character? The reason is that, It helps us to define, upto which index we have some useful data present in our character array, and after null character there may or may not be some garbage values of characters. Let's first start by showing a

Algorithms, Pseduocodes and FlowCharts.

Some Basic Knowledge. C++ is a general purpose programming language, created by Bjarne Stroustrup and was firstly released in  1985. It is an extension to C language, Reason for that is if you are familiar with C language than you must be familiar with the fact that there are no classes, templates etc. present in C language. Due to which when classes are added to C language it is named as "C with Classes", but C++ is considered as default name now.  Extra thing to remember - In "C++" the symbol "++" after C is post increment symbol and therefore, "C++" means that In C additional features are added. Now, let's define, what are algorithms? Algorithms are nothing but set of rules that must be followed to accomplish some task. (Now, What that mean?😕) let's take an example of swapping two numbers, i.e. if a = 10 and b = 20 after swapping a = 20 and b = 10. How will you write the procedure for this?  (There are many such algorithms). Accordin