Ad

Monday, May 28, 2007

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:

11 comments:

  1. Anonymous1:52 AM

    im brand new to this i do not now enything about this please help
    HACKER722

    ReplyDelete
  2. Hi,

    You're welcome to ask questions.

    ReplyDelete
  3. data types is of 2 types or three types???????

    ReplyDelete
  4. some good concept of file handling.......

    ReplyDelete
  5. Anonymous3:10 PM

    thx arvind:)

    ReplyDelete
  6. Hi Anonymous,

    My pleasure!

    ReplyDelete
  7. Anonymous1:01 PM

    js confsd bwt e data typ thng in c++

    ReplyDelete
    Replies
    1. Anonymous10:22 AM

      go through the text twice or thrice and u'll be clear

      Delete
  8. Anonymous2:28 PM

    please explain the meaning of void

    ReplyDelete
  9. Is long float also a data type???
    It is written in my book that some ANSI Standards eliminate long float which is nothing but double...
    m confused....pls help.

    ReplyDelete

You are free to comment anything, although you can comment as 'Anonymous' it is strongly recommended that you supply your name. Thank You.

Please don't use abusive language.