Skip to main content

C++ Data Types in Detail

Data types are means to identify the type of data and associated operations of handling it. C++ provides a predefined set of data types for handling the data it uses. When variables are declared of a particular data type then the variable becomes the place where the data is stored and data types is the type of value(data) stored by that variable. Data can be of may types such as character, integer, real etc. since the data to be dealt with are of may types, a programming language must provide different data types. In C++ data types are of two types:-

  1. Fundamental Data Types: As the name suggests these are the atomic or the fundamental data types in C++. Earlier there was five of these (int, char, float, double and void) but later two new data types namely bool and wchar_t have been added. Int stores integer data or whole numbers such as 10, -340 etc., char stores any of the ASCII characters, float is used to store numbers having fractional part such as 10.097, double stores the same data as float but with higher range and precision, bool can only store true and false values.
        //Program to illustrate various fundamental data type
         #include<iostream.h>
         void main(void)
         {
         int age;
         float salary;
         char code;
         cout<<"Enter age, salary and code:";
         cin>>age;
         cin>>salary;
         cin>>code;
         cout<<endl;//goto next line
         cout<<"DETAILS"<<endl;//short for cout<<"DETAILS";cout<<endl;
    
         cout<<"Age:"<<age<<endl;
         cout<<"Salary:"<<salary<<endl; cout<<"Code:"<<code<<endl;
    
    
         }
  1. Derived Data Types: These are the data types which are derived from the fundamental data types. It is further divide into two categories i)Built-In and ii)User-defined, which are discussed below as seperate topics.

Built-In Derived Data Type

  1. Arrays: Arrays refer to a list of finite number of same data types. The data in the array can be accessed by an index number ranging from 0 to n(where n is the number of data element it can store). Ex- if arr[3] is an array of int(egers) then the different values in the array can be accessed as shown below. arr[0], arr[1],arr[2] when we declare an array such as the one sown above then by arr[3] we mean that we want three elements in the array and hence while accessing arr[2] is the last element.
        //Program to illustrate arrays
         #include<iostream.h>
         void main(void)
         {
         int arr[3];//it will store 3 integer elements
         cout<<"enter 3 numbers:";
         cin>>arr[0]>>arr[1]>>arr[2];//this statement is same as using three cin's
    
         cout<<endl;//goto next line
         cout<<arr[0]<<arr[1]<<arr[2];
    
         }
    
  2. Pointer: A pointer is a variable that holds the memory address of other variable. It is also of different data types, ex- char pointer can store address of only char variables, int pointer can store address of int variables and so on.
  3. Reference: A reference in the simplest sense is an alias or alternate name for a previously defined variable.
         
         //Program to illustrate References
         #include<iostream.h>
         void main(void)
         {
         int var;
         int &refvar=var;//here a reference variable to var is declared remember var was previously declared
    
         var=10;//var is given the value 10
         cout<<var<<endl;
         refvar=100;//reference variable of var is changed
    
         cout<<var;//but var also gets changed
    
         }
    

User-Defined Derived Data Types

  1. Class: A class is a collection of variables and function under one reference name. it is the way of separating and storing similar data together. Member functions are often the means of accessing, modifying and operating the data members (i.e. variables). It is one of the most important features of C++ since OOP is usually implemented through the use of classes.
  2. Structure: In C++ structure and class same except for some very minor differences.
  3. Union: A union is a memory location shared by two or more different variables, generally of different data types. Giving more details here would only confuse you; I’ll leave it for future articles.
  4. Enumerations: It can be used to assign names to integer constants.
         //Program to illustrate Enumerators
         #include<iostream.h>
         void main(void)
         {
         enum type{POOR,GOOD,EXCELLENT};//this is the syntax of enumerator

         int var;
         var=POOR;//this makes programs more understandable
         cout<<var<<endl;
         var=GOOD;
         cout<<var<<endl;
         var=EXCELLENT;
         cout<<var;

         }

Data Types Modifiers

  • signed
  • unsigned
  • short
  • long

Int, char, float, double data types can be preceded with these modifiers to alter the meaning of the base type to fit various situations properly. Every data type has a limit of the larges and smallest value that it can store known as the range. An integer (usually 4 bytes long) can store any value ranging from -2147483648 to 2147483647. Data Type modifiers usually alter the upper and lower limit of a data type. Unsigned modifier makes a variable only to store positive values. Ex- if a data type has a range from –a to a then unsigned variable of that type has a range from 0 to 2a. Preceding any data type with signed is optional because every data type is signed by default. Short integers are 2 bytes long and long integers are 8 bytes long.

Related Articles:

Popular posts from this blog

Fix For Toshiba Satellite "RTC Battery is Low" Error (with Pictures)

RTC Battery is Low Error on a Toshiba Satellite laptop "RTC Battery is Low..." An error message flashing while you try to boot your laptop is enough to panic many people. But worry not! "RTC Battery" stands for Real-Time Clock battery which almost all laptops and PCs have on their motherboard to power the clock and sometimes to also keep the CMOS settings from getting erased while the system is switched off.  It is not uncommon for these batteries to last for years before requiring a replacement as the clock consumes very less power. And contrary to what some people tell you - they are not rechargeable or getting charged while your computer or laptop is running. In this article, we'll learn everything about RTC batteries and how to fix the error on your Toshiba Satellite laptop. What is an RTC Battery? RTC or CMOS batteries are small coin-shaped lithium batteries with a 3-volts output. Most laptops use

The Best Way(s) to Comment out PHP/HTML Code

PHP supports various styles of comments. Please check the following example: <?php // Single line comment code (); # Single line Comment code2 (); /* Multi Line comment code(); The code inside doesn't run */ // /* This doesn NOT start a multi-line comment block /* Multi line comment block The following line still ends the multi-line comment block //*/ The " # " comment style, though, is rarely used. Do note, in the example, that anything (even a multi-block comment /* ) after a " // " or " # " is a comment, and /* */ around any single-line comment overrides it. This information will come in handy when we learn about some neat tricks next. Comment out PHP Code Blocks Check the following code <?php //* Toggle line if ( 1 ) {      // } else {      // } //*/ //* Toggle line if ( 2 ) {      // } else {      // } //*/ Now see how easy it is to toggle a part of PHP code by just removing or adding a single " / " from th

How to Remove Bullet Points in CSS

This will be a short guide on how to remove bullet points from ordered/unordered lists with CSS, you only need two lines of CSS for this. The first removes the actual bullet points and the second one removes the space to the left, as evident from the following video: We'll also be doing some bullet beautification in the last section if that's what you are looking to do. How to Remove Bullet Points in HTML/CSS Now that we know what CSS properties actually accomplish what we want let's see how we can implement this in our HTML code. Using Inline "style" Tag (The quick and dirty way) As the title suggests, this is the quickest way to remove bullet points in which you wouldn't have to edit any CSS files (for example, for Blogger or WordPress). This method would be useful for a one-off case - just add the following " style " attribute to the list you want to remove the bullet points from: <h1>Ordered List</h1> <ol  style="list-styl