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.- Initialized data segment or simply data segment - The variables that are declared and initialized in our program are stored here.
- Uninitialized data segment.- The variables that are just declared or have value zero and not in use are stored here.
- There are other languages like python and Java which has builtin garbage collecter, i.e. these language handle heap memory allocation automatically.
- 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.
- Built-in Data Types.
- Derived Data Types.
- User-Defined Data Types.
Built-in Data Types
- Integral Type.
- Floating Type.
- Void.
- Bool Type.
- Wide character Type.
- 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 -
- To represent Integers (which is just an obvious thing). We use Integers to represent Integers. (Syntax for integers types in C++ int)
- 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)
- 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 -
- 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).
- 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)
- 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).
- 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.
- 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.
- 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.
#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.
#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.
- long - increase the ranges of data types.
- short - decrease the ranges of data types.
- unsigned - make the value range, ranges from 0 to (max range without modifier - min range without modifier).
- 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.
- Not every modifier can be used with every data types, you should try to find out it by yourself.
Stay tuned for the upcoming content. For any query leave a comment below.
- Wikipedia.
- hilite.com for code snippets.
- ideone.com
Grt work bro. keep it up
ReplyDeleteGreat explanation...
ReplyDelete