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 :
Type | Size | Description |
---|---|---|
char | 1 byte | Used for characters or integer variables. |
int | 2 or 4 bytes | Used for integer values. |
float | 4 bytes | Single precision floating point values. |
double | 8 bytes | Double precision floating point values. |
wchar_t | 2 or 4 bytes | Wide character. |
bool | 1 bytes | Boolean ( 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.
Modifier | Description |
---|---|
long | Forces 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. |
short | Forces a type int to be 2 bytes (16 bits) long. Cannot be used with long. |
unsigned | Causes 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 Type | Range | Bytes |
---|---|---|
signed char | -128 to +127 | 1 |
unsigned char | 0 to 255 | 1 |
short signed int | -32768 to +32767 | 2 |
short unsigned int | 0 to 65535 | 2 |
signed int | -32768 to +32767 | 2 |
unsigned int | 0 to 65535 | 2 |
long signed int | -2147483648 to +2147483647 | 4 |
long unsigned int | 0 to +4294967295 | 4 |
float | -3.4e38 to +3.4e38 | 4 |
double | -1.7e308 to +1.7e308 | 8 |
long double | -1.7e4932 to +1.7e4932 | 10 |
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. EnumAn 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 62. 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