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

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 {   ...

How to Create an HTML Form Linked with MySQL Database in PHP

If you're looking for example code and detailed discussion on how to create an HTML form that stores data in a MySQL database using PHP then this post might be what you're looking for. I assume that you're familiar with basic HTML, CSS, PHP coding, and  MySQL. I am going to divide this small project into two parts: The HTML form itself that takes input from the user and the PHP script that saves it into the database A table that displays the user-added data that has been saved in the database. We'll be dealing with the first part in this tutorial. Again I'd like to break this problem into a few parts so that it's easier for you to understand and probably gives you an insight into how breaking up a problem into smaller chunks can help make things clearer in your mind. Let's think about it, there is an HTML form (that is HTML code), then there is the PHP code that deals with the user-input data, and the MySQL database itself. For this tutorial, we'll b...

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 ...