codescracker


c++

C++ Data Types



Previous Page Next Page


C++ Data Types Concept

Data can be of many types, for example, character, integer, real, string etc. Any thing enclosed in single quotes represents character data in C++. Numbers without fractions represents integer data. Numbers with fraction represents real data and anything enclosed in double quotes represents a string. So, C++ like any other language provides ways and facilities to handle different types of data by providing data types.

Data Types - Data types are means to identify the type of data and associated operations of handling it. C++ data types are of following two types :

  1. Fundamental Types
  2. Derived Types

There are five fundamental data types in C++: char, int, float, double, and void that represent character, integer, floating-point, double floating-point, and valueless data respectively.

Derived types constructed from the fundamental types are: array, functions, pointers, references, constants, classes, structures, unions and enumerations. Let's take an example program, using fundamental and derived types in C++.

/* C++ Data Types */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int i;
	char ch;
	char str[20];
	int arr[20];
	float fl;
	cout<<"Enter a character: ";
	cin>>ch;
	cout<<"\nYou entered: "<<ch;
	cout<<"\n\nEnter a floating-point number: ";
	cin>>fl;
	cout<<"\nYou entered: "<<fl;
	cout<<"\n\nEnter your name: ";
	cin>>str;
	cout<<"\nEnter any 5 number: ";
	for(i=0; i<5; i++)
	{
		cin>>arr[i];
	}
	cout<<"\n\n"<<str<<", You entered these five numbers:\n";
	for(i=0; i<5; i++)
	{
		cout<<arr[i]<<"  ";
	}
	getch();
}

Here is the sample run of this C++ program:

c++ concept of data types

As already discussed that there are following five fundamental data types in C++ :

  1. char
  2. int
  3. float
  4. double
  5. void

All other data types in C++ are based upon one of these fundamental types. Such data types are known as derived data types. The size and range of these data types vary with each processor type and with the implementation of the C++ compiler. A character is generally 1 byte and an integer often 2 bytes, but you cannot make this assumption if you want your programs to be portable or adaptable to the widest range of computers. Let's discuss each type of data types in details.

C++ Fundamental Data Types

Fundamental data types are those that are not composed of other data types. There are the following five fundamental data types :

  1. int Data type (for integers)
  2. char Data type (for characters)
  3. float Data type (for floating-point numbers)
  4. double Data type (for double precision floating-point numbers)
  5. void Data type (for empty set of values and non-returning functions)

C++ Data Types Size and Range

Before discussing all the fundamental data types of C++, let's look at the following table to know the C++ data type sizes, and ranges:

Data Type Storage Size (in Bytes) Range
char 1 -127 to 127
unsigned char 1 0 to 255
signed char 1 -127 to 127
int 2 or 4 -32,767 to 32,767
unsigned int 2 or 4 0 to 65,535
signed int 2 or 4 -32,767 to 32,767
short int 2 -32,767 to 32,767
unsigned short int 2 0 to 65,535
signed short int 2 -32,767 to 32,767
long int 4 -2,147,483,647 to 2,147,483,647
signed long int 4 -2,147,483,647 to 2,147,483,647
unsigned long int 4 0 to 4,294,967,295
float 4 1E-37 to 1E+37(six digits of precision)
double 8 1E-37 to 1E+37(ten digits of precision)
long double 10 1E-37 to 1E+37(ten digits of precision)

C++ int Data Type

Integers are whole numbers such as 4, 56, -234, 0 etc. They have no fractional parts. Integers are represented in C++ by int data type. An identifier declared as int cannot have fractional part. Integers can be positive or negative but whether or not integers can have negative values, it depends upon the integer types which will be discussed in the separate chapter on C++ Data Type Modifier which is given in the next chapter. Let's take an example program.

/* C++ Data Types */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int num1, num2, sum=0;
	cout<<"Enter any two number to find sum:\n";
	cin>>num1>>num2;
	sum=num1+num2;
	cout<<"\nThe sum of the two number is "<<sum;
	getch();
}

Here is the sample run of the above C++ program:

c++ data types

C++ char Data Type

Characters can store any member of the C++ implementation's basic character set. If a character from this set is stored in a character variable, its value is equivalent to the integer code of that character. An identifier declared as char becomes a character variable. Characters can also be of different types which will discussed in the separate chapter on C++ Data Type Modifier which is given in the next chapter. Let's take an example program demonstrating the use of char type in C++

/* C++ Data Types */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	char ch;
	cout<<"Enter a character: ";
	cin>>ch;
	cout<<"\nYou entered: "<<ch;
	getch();
}

Here is the sample run of the above C++ program:

data types in c++

In C++, char type is often said to be an integer type. It is said so because letters, symbols etc. are represented in memory by associated number codes. It is very easy to code numbers into binary. Thus even letters and symbols are also represented by their associated number codes (e.g., ASCII code), because of which char data type is called another integer type. ASCII code stores number codes for 256 known characters (letters, digits, symbols, nongraphic characters etc.). Following C++ program illustrates char as int type. This C++ program simply display the ASCII code of a character and vice-versa :

/* C++ Data Types */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	char ch;
	int i;
	cout<<"Enter a character: ";
	cin>>ch;
	i = ch;
	cout<<"\nThe ASCII code for "<<ch<<" is "<<i<<"\n";
	cout<<"\nEnter a number: ";
	cin>>i;
	ch = i;
	cout<<"\nThe character for the ASCII code "<<i<<" is "<<ch;
	getch();
}

Here are the two sample runs of the above C++ program:

c++ basic data types

c++ fundamental data types

The ASCII value for A is 65 and for Z is 90. And the ASCII value for a is 97 and for z is 122.

C++ float Data Type

A number having fractional part is a floating-point number. For example, 3.14159 is a floating-point number. The decimal point signals that it is a floating-point number not an integer. The number 12 is an integer, but 12.0 is a floating-point number. This number is in normal decimal notation (fractional form). Floating-point numbers can also be written in exponent notation. For example, 147.9101 would be written as 1.479101E02.

An identifier declared as float becomes a floating-point variable and can hold floating-point numbers. Floating point variables represent real numbers, which are used for measurable quantities like distance, area, temperature etc. and typically have a fractional part. Floating-point variables can also be of different types. For detail, follow the separate tutorial on C++ Data Type Modifier which is given in the next chapter. Let's take an example program demonstrating the use of float data type in C++

/* C++ Data Types */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	float marks[5];
	float sum=0, avg=0;
	cout<<"Enter marks obtained in five subjects:\n";
	for(int i=0; i<5; i++)
	{
		cin>>marks[i];
		sum=sum+marks[i];
	}
	avg=sum/5;
	cout<<"\nYou got "<<sum<<" marks in 5 subjects\n";
	cout<<"with an average of "<<avg;
	getch();
}

Here is the sample run of the above C++ program:

c++ data types list

Floating-point numbers have two advantages over integers. First, they can represent values between the integers. Second, they can represent a much greater range of values. But floating-point numbers suffer from one disadvantage also. Floating-point operations are usually slower than integer operations.

C++ double Data type

The data type double is also used for handling floating-point numbers. But it is treated as a distinct data type because, it (double data type) occupies twice as much memory as type float, and stores floating-point numbers with much longer range and precision (significant numbers after decimal point). It stands for double precision floating-point. It is used when type float is too small or insufficiently precise.

C++ void Data type

The void type specifies an empty set of values. It is used as the return type for functions that do not return a value. No object of type void may be declared. You can see from the above C++ programs, that the function main() has return type as void. This tells the compiler that the main() function will not return any value.

Rules for Assigning Data Types

There are the following rules for assigning data types :

Check Actual Size of Variable

The sizes of variables might be different depending on the computer you are using. Following is the example, which will produce correct size of various data types on your computer.

/* C++ Data Types */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	
	cout<<"Here are the actual size of these data types:\n\n";
	cout<<"char: "<<sizeof(char)<<"\n";
	cout<<"int: "<<sizeof(int)<<"\n";
	cout<<"short int: "<<sizeof(short int)<<"\n";
	cout<<"long int: "<<sizeof(long int)<<"\n";
	cout<<"float: "<<sizeof(float)<<"\n";
	cout<<"double: "<<sizeof(double)<<"\n";
	
	getch();
}

Using the sizeof() function/operator, you can get the actual size of various data types on various platform. Here is the sample output of the above C++ program.

c++ data types size range

C++ Derived Data Types

From the fundamental data types, other types can be derived by using the declaration operators. Here are some of the most important C++ derived data types.

More Examples

Here are some more examples listed, that you can go for:


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test