Basic Data type in C++


Data type means to identify the type of data and associated operations of handling it.C++ supports a large number of data types. When we declare a variable with data type then we know that variable can hold which type of data int or float .There are two types of data type.

1)      Primitive Data type

2)   Non-primitive Data type

1)      Primitive Data type: It is also known as built-in or System define data type.A built-in type is a data type for which the programming language provides built-in support.example int,float,char,string

1) int: is used for integer value.for example

{

int a;

a = 5;

}

Following table gives you details about standard integer types with its storage sizes and value ranges:

Type

Storage size

Value range

int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295

2) float: is used for float value.for example

{

float per;

per = 70.2;

    }

Following table gives you details about standard float types with its storage sizes and value ranges:

Type

Storage size

Value range

Precision

float 4 byte 1.2E-38 to 3.4E+38 6 decimal places
double 8 byte 2.3E-308 to 1.7E+308 15 decimal places
long double 10 byte 3.4E-4932 to 1.1E+4932 19 decimal places

3) double:is used for double value.for example

{

double Atoms;

Atoms = 2700000;

    }

4) char:is used for character value.for example

{

char Letter;

Letter = ‘x’;

    }

Following table gives you details about standard character types with its storage sizes and value ranges:

Type

Storage size

Value range

char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127

 

Here is an example to check size of memory taken by various datatypes.

#include<iostream.h>

#include<conio.h>

void main()

{

cout<<“sizeof(char)=”<<cout<<” \n”<<cout<< sizeof(char);

cout<<“sizeof(short)=”<<cout<<” \n”<<cout<< sizeof(short);

cout<<“sizeof(int)=”<<cout<<” \n”<<cout<< sizeof(int);

cout<<“sizeof(long)=”<<cout<<” \n”<<cout<< sizeof(long);

cout<<“sizeof(float)=”<<cout<<” \n”<<cout<< sizeof(float);

cout<<“sizeof(double)=”<<cout<<” \n”<<cout<< sizeof(double);

cout<<“sizeof(long double)=”<<cout<<” \n”<<cout<< sizeof(long double);

cout<<“sizeof(long long)=”<<cout<<” \n”<<cout<< sizeof(long long);

}}

2)      Non-primitive Data type:

It is also known as user define data type.example array ,pointer ,eunm ,structure

1) array: which is used to stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data.An array as a collection of variables of the same type. To declare an array in C++, the programmer specifies the type of the elements and the number of elements required by an array as follows:

type arrayName [ arraySize ];

2)  structure: C++ arrays allow us to define variables that combine several data items of the same kind but structure is another user defined data type which allows you to combine data items of different kinds.

Structures are used to represent a record, suppose you want to keep track of student in a lSchool. You might want to track the following attributes about each student:

  • Name
  • Rollno
  • Class
  • address

Defining a Structure:

To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member, for your program. The format of the struct statement is this:

struct student

{

char  name[50];

int  rollno[50];

char  class[50];

char  address[100];

}

3) enum:C++ allows programmers to create their own data type  doing by an enumerated type. An enumerated type is a data type where every possible value is defined as a symbolic constant which is also called an enumerator. Enumerated types are declared via the enum keyword. example:

#include <iostream.h>

void main()

{

enum Fruits{orange, guava, apple};

Fruits myFruit;

int i;

cout << “Please enter the fruit of your choice(0 to 2)::”;

cin >> i;

switch(i)

{

case orange:

cout << “Your fruit is orange”;

break;

case guava:

cout << “Your fruit is guava”;

break;

case apple:

cout << “Your fruit is apple”;

break;

}  }

4) pointer:The pointer data type allows  a variable to hold an address or a pointer. The concept of an address and a pointer are one in the same. A pointer points to the location in memory because the value of a pointer is the address were the data item resides in the memory.for example

int x = 47;

In above example integer variable named x. We can create a pointer variable and establish its value which would be the done using the address operator ,which is the ampersand or & .

int * int_pointer = &x;

The asterisk(*) is used to designate that the variable int_pointer is an integer pointer [int *]. This means that whenever we use the variable int_pointer that the compiler will know that it is a pointer that points to an integer.

Comments are closed.