Data Type in C++





  • C++ language is rich in its data type.

  • Data type tells the type of data, that you are going to store in memory.

  • It gives the information to compiler that how much memory ( No. of bytes ) will be stored by the data.

I. Primitive Data Types :

TypeSizeDescription
char1 byteUsed for characters or integer variables.
int2 or 4 bytesUsed for integer values.
float4 bytesSingle precision floating point values.
double8 bytesDouble precision floating point values.
wchar_t2 or 4 bytesWide character.
bool1 bytesBoolean ( True or False ).

In addition to these data types, some of them may be used with a modifier that affects the characteristics of the data object. These modifiers are listed in Table.

ModifierDescription
longForces a type int to be 4 bytes (32 bits) long and forces a type double to be larger than a double (but the actual size is implementation defined). Cannot be used with short.
shortForces a type int to be 2 bytes (16 bits) long. Cannot be used with long.
unsignedCauses the compiler (and CPU) to treat the number as containing only positive values. Because a 16-bit signed integer can hold values between – 32,768 and 32,767, an unsigned integer can hold values between 0 and 65,535. The unsigned modifier can be used with char, long, and short (integer) types.

The essence of all the data types that we have learned so far has been captured in Table.

Data TypeRangeBytes
signed char-128 to +1271
unsigned char0 to 2551
short signed int-32768 to +327672
short unsigned int0 to 655352
signed int-32768 to +327672
unsigned int0 to 655352
long signed int-2147483648 to +21474836474
long unsigned int0 to +42949672954
float-3.4e38 to +3.4e384
double-1.7e308 to +1.7e3088
long double-1.7e4932 to +1.7e493210

NOTE :
The sizes and ranges of int, short and long are compiler dependent.Sizes in above table are for 16-bit compiler.

#include < iostream >
using namespace std;
int main() {
 cout << "Size of char is " << sizeof(char) << endl;
 cout << "Size of int is " << sizeof(int) << endl;
 cout << "Size of float is " << sizeof(float) << endl;
 cout << "Size of short int is " << sizeof(short int) << endl;
 cout << "Size of long int is " << sizeof(long int) << endl;
 cout << "Size of double is " << sizeof(double) << endl;
 cout << "Size of wchar_t is " << sizeof(wchar_t) << endl;
 cout << "Size of bool is " << sizeof(bool) << endl;
 return 0;
}                 
OUTPUT :
Size of char is  1
Size of int is  4
Size of float is  4
Size of short int is  2
Size of long int is  4
Size of double is  8
Size of wchar_t is  2
Size of bool is  1    


II. Derived Data Types :

  • 1. Array Discuss later.
  • 2. Pointer Discuss later.
  • 3. Function Discuss later.

III. User Defined Data Types :

1. Enum

An enumeration is a set of named integer constants. Enumerations are common in everyday life. For example, an enumeration of the day is Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday.

To create an enumeration we use the keyword enum.

Syntax :


example:
#include< iostream >
int main()
{
 int i;
 enum day {MON,TUE,WED,THU,FRI,SAT,SUN};
  for(i=MON;i<=SUN;i++) {
      cout << i;
  }
return 0;
}
OUTPUT
0 1 2 3 4 5 6
2. Typedef

The C++ programming language provides a keyword called typedef, which you can use to give a type, a new name.

Typedef can be used to simplify the real commands as per our need.

Syntax :


example:
Now, the following declaration is perfectly legal and creates an integer variable called distance:


#include< iostream >
typedef char* string;
int main()
{
 int i;
 string name;
 cout <<"Enter Name: ";
 cin >>name;
 cout <<"----------" << endl;
 cout << name << "!!";
 return 0;
}
  
OUTPUT
Enter Name: Prowessapps
----------------
Hi Prowessapps!!

Next topic is variable in C++





 





Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved