Data Types in C++ Assignment
Data type is a keyword used to identify type of data. It is used for storing the input of the program into the main memory (RAM) of the computer by allocating sufficient amount of memory space in the main memory of the computer.
In general every programming language is containing three categories of data types. They are
- Fundamental or primitive data types
- Derived data types
- User defined data types
Primitive data types
These are the data types whose variable can hold maximum one value at a time, in C++ language it can be achieve by int, float, double, char.
There are several basic types of type modifier
- Signed
- Unsigned
- Short
- long
The table summarizes the modified size and range of built-in datatypes when combined with the type modifiers:
DataData Type |
Size |
Range |
short int |
2 |
-32,768 to 32,767 |
unsigned short int |
2 |
0 to 65,535 |
unsigned int |
4 |
0 to 4,294,967,295 |
Int |
4 |
-2,147,483,648 to 2,147,483,647 |
long int |
4 |
-2,147,483,648 to 2,147,483,647 |
unsigned long int |
4 |
0 to 4,294,967,295 |
long long int |
8 |
-(2^63) to (2^63)-1 |
unsigned long long int |
8 |
0 to 18,446,744,073,709,551,615 |
signed char |
1 |
-128 to 127 |
unsigned char |
1 |
0 to 255 |
Float |
4 | |
Double |
8 | |
long double |
12 | |
wchar_t |
2 or 4 |
1 wide character |
There are several basic types of type modifier
- Signed
- Unsigned
- Short
- long
Derived data types
These data type are derived from fundamental data type. Variables of derived data type allow user to store multiple values of same type in one variable but never allows storing multiple values of different types. These are the data type whose variable can hold more than one value of similar type.
In C++ language it can be achieve by array.
Example
int a[] = 10, 20, 30; //valid
int a[] = 10, ‘K’, “Hanvi”; // not valid
User defined data types
User defined data types related variables allows us to store multiple values either of same type or different type or both. This is a data type whose variable can hold more than one value of dissimilar type, in C++ language it is achieved by structure.
In C++ language, user defined data types can be developed by using struct, union, enum etc.
Example
Typedef declaration
The typedef declaration provides a way to declare an identifier as a type alias, to be used to replace a possibly complex type name. The keyword typedef is used in a declaration, in the grammatical position of a storage-classNamespecifier, except that it does not affect storage or linkage.
Syntax
typedef int distance;
Enumerated types
C++ contains quite a few built in data types. But these types aren’t always sufficient for the many kinds of things. So C++ contains capabilities that allow programmers to create their own data types. These data types are called user-defined data types.
An enumerated type (also called an enumeration or enum) is a data type where every possible value is defined as a symbolic constant (called an enumerator). Enumerations are defined via the enum keyword.
Syntax
enum enum-name list of attribute variable type ;
Defining an enumeration (or any user-defined data type) does not allocate any memory. When a variable of the enumerated type is defined (such as variable paint in the example above), memory is allocated for that variable at that time.