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
         void main(void)
         int age;
         float salary;
         char code;
         cout<<"Enter age, salary and code:";
         cout<<endl;//goto next line
         cout<<"DETAILS"<<endl;//short for cout<<"DETAILS";cout<<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
         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
  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
         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
         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
         void main(void)
         enum type{POOR,GOOD,EXCELLENT};//this is the syntax of enumerator

         int var;
         var=POOR;//this makes programs more understandable


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

Designing a Simple Order Form Application in PHP

Ok guys, for this post we’re going to create a simple yet complete order form page. Order forms are used on many sites to take customers order online. Order forms should have the capability to take orders from visitors regarding what items they want to purchase and store the information for further processing. For this post’s example, we are going to create an order form for a Book Seller. The form will be designed to take order of five different items (books). Our order form application should be able to take order of five different items in any separate quantities tht user wants, it should also ask for shipping address and name of the customer. It should then store the information provided in a file along with the date and time order was placed. The application should also be able to take any number of orders and store them all linearly for further human processing. For this, we need a front end of a HTML form to which the user would interact a