Data type in C++:
While writing the program in any language, we need to use various variables to store various information. Variables are Nothing but reserved memory locations to store values. this means that we create a variable we reserve some space in Memory.
We may like to store information of various data types like character, wide character, integer, floating point, double floating point, Boolean etc. based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory.
In computer Programming, information is stored in a computer memory with different data types. We must know what it is to be stored in a Computer Memory, whether it is just a digit or character, a small Number, or decimal number. Computer Memory is organized in bytes, and for these variables, with varying information, a data type is associated. A byte is the minimum amount of memory in computer memory that can store a small amount of data and managed easily. Every variable is declared with two entities, its type, and its name. There are several data types available in C++. The basic built-in data types are char, int, float, double, bool, etc.
C++ provides various data types and each type is represented differently within the computer's Memory. The various data types provided by C++ are built-in data types, derived data types and user-defined data types.
Integer Data Type:
Data Type (keywords) |
Description | Size | Typical Range |
---|---|---|---|
int | Integer. | 4 bytes | -2147483648 to 2147483647 |
Signed int | Signed integer. Values may be Negative, positive, or Zero. | 4 bytes | -2147483648 to 2147483647 |
Unsigned int | Unsigned integer. value are always positive or zero. Never Negative value. | 4 bytes | 0 to 4294967295 |
Short | short Integer. | 2 bytes | -32768 to 32767 |
Signed short | Signed short integer. values may be positive, Negative, or zero. | 2 bytes | -32768 to 32767 |
Unsigned short | Unsigned short integer. Values are always positive or zero. Never Negative value. | 2 bytes | 0 to 65535 |
Long | Long integer. | 4 bytes. | -2147483648 to 2147483647 |
Signed long | Signed long integer. values may be Negative, Positive, or zero. | 4 bytes | -2147483648 to 2147483647 |
Unsigned long | Unsigned long integer. values are always positive or zero. Never Negative value. | 4 bytes | 0 to 4,294,967,295 |
Character Data Type:
Data Type (keywords) |
Description | Size | Typical Range |
---|---|---|---|
char | Any single character. it may include a letter, a digit, a punctuation mark, or a space. | 1 byte | -128 to 127 or 0 to 255 |
Signed char | Single character. | 1 byte | -128 to 127 |
unsigned char | Unsigned character | 1 byte | 0 to 255 |
wchar-t | Wide character | 2 or 4 bytes | 1 wide character |
Floating-point Data Type:
Data Type (keywords) |
Description | Size | Typical Range |
---|---|---|---|
float | Floating point number. there is no fixed number of digits before or after the decimal point. | 4 bytes | +/- 3.4e +/- 38 (~7 Digits) |
Double | Double precision floating point number. More accurate compared to float | 8 bytes | +/- 1.7e +/- 308 (~15 Digits) |
long double | Long double precision floating point number. | 8 bytes | +/- 1.7e +/- 308 (~15 Digits) |
Boolean Data Type:
Data Type (keywords) |
Description | Size | Typical Range |
---|---|---|---|
bool | Boolean value. it can only take one of two values: true or False. | 1 byte | true or false |
“
Above Mentioned Variable data sizes might be different from your PC. because it depends on the compiler and computer structure. the following example will produce correct size of various data type. copy and paste this code in your compiler to know about your data size.
/*Data type in C++ Programming Language - InfoBrother*/
#include<iostream>
using namespace std;
main()
{
cout<<"Integer data type"<<endl;
cout<<"size of int is "<<sizeof(int)<<endl;
cout<<"size of signed int is "<<sizeof(signed int)<<endl;
cout<<"size of unsigned int is "<<sizeof(unsigned int)<<endl;
cout<<"size of short is "<<sizeof(short)<<endl;
cout<<"size of signed short is "<<sizeof(signed short)<<endl;
cout<<"size of unsigned short is "<<sizeof(unsigned short)<<endl;
cout<<"size of long is "<<sizeof(long)<<endl;
cout<<"size of signed long is "<<sizeof(signed long)<<endl;
cout<<"size of unsigned long is "<<sizeof(unsigned long)<<endl;
cout<<"\n\n character data type"<<endl;
cout<<"size of char is "<<sizeof(char)<<endl;
cout<<"size of signed char is "<<sizeof(signed char)<<endl;
cout<<"size of unsigned char is "<<sizeof(unsigned char)<<endl;
cout<<"size of wchar_t is "<<sizeof(wchar_t)<<endl;
cout<<"\n\n Floating point data type:"<<endl;
cout<<"size of float is "<<sizeof(float)<<endl;
cout<<"size of Double is "<<sizeof(double)<<endl;
cout<<"size of long double is "<<sizeof(long double)<<endl;
return 0;
}
Data type in C++
Integer data type
size of int is 4
size of signed int is 4
size of unsigned int is 4
size of short is 2
size of signed short is 2
size of unsigned short is 2
size of long is 4
size of signed long is 4
size of unsigned long is 4
character data type
size of char is 1
size of signed char is 1
size of unsigned char is 1
size of wchar_t is 2
Floating point data type:
size of float is 4
size of Double is 8
size of long double is 16
String Data Type:
There is another data type know as String, String is not the part of C++ Programming Language, it is defined in a header file < string > to use this data type we Need to include < string > Header File. A variable of type String can contain a string like "Hello World" or "InfoBrother" The Length of the string can change during the execution of the program. String is an simply Arrays of Character, and in C++ it can Represented as "C-Style Character String, and String Class:
Void Data Type:
The void data type is used for specifying an empty parameter list to a function and return type for a function. when a void is used to specify an empty parameter list, it indicates that a function does not take any arguments and when it is used as a return type for a function, it indicates that a function does not return any value. For void, no Memory is allocated and hence, it can't store anything. As a result, void cannot be used to declare simple variables, however, it can be used to declare generic pointers.
Derived Data Type:
Data type that are derived from the built-in data types are known as derived data types. Array, Function, References and Pointers are various derived data types provided by C++.
Array:
Is set of Elements of the same data type that are referred to by the same name. All the Elements in an Array are stored at contiguous memory locations and each element is accessed by a unique address. (Read more...)
Function:
Is a Self-contained program segment that carries out a specific well-defined task. In C++, every Program contains one or more function which can be invoked from other parts of a program, if required. (Read more...)
Reference:
Is an alternative name for a variable. That is, a reference is an alias for a variable in a program. A variable and its reference can be used interchangeably in a program as both refer to the same memory location. hence, changes made to any of them are reflected in the other. (Read more...)
Pointer:
Is a variable that can store the memory address of another variable. Pointers allow to use the memory dynamically. That is, with the help of pointers, memory can be allocated or de-allocated to the variables at run-time, thus making a program more efficient. (Read more...)
User-Defined Data Type:
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. C++ Provide various user-defined data types, like Structures, Unions, enumerations and classes.
Structure & Union:
Structure and Union are the significant features of C language, Structure and Union provide a way to group similar or dissimilar data types referred to by a single name. However, C++ has extended the concept of structure and Union by incorporating some new features in these data types to support object-oriented programming. (Read more...)
Class:
C++ offers a new user-defined data type known as class, which forms the basis of object-oriented programming. A class acts as a template which defines the data functions that are included in an object of a class. Classes are declared using the keyword class. Once a class has been declared, its object can be easily created. (Read more...)
Enumeration:
This is an User defined data type having finite set of enumeration constants. the keyword "enum" is used to create enumerated data type.