DATA TYPES IN C++
Whenever you
create a variable to store a value, you also define the data type of that
variable as well. From data type, the compiler knows which type of data will be
stored in this variable.
Also, from
compiler data type, it also decides how much space is to be assigned in memory
to a variable. The data types in C ++ are divided into 3 categories.
1. BASIC/PRIMARY DATA TYPE : As I told you earlier basic data types are almost common in all
programming languages. Basic/primary
data types are in the 6 categories given below.
- Integer
- Character
- Boolean
- Void
- Floating point
- Double floating point
These
are being given in detail below.
Integer : The data types of Integer category are used to store the whole number. Whole numbers are numbers that do not contain decimals and numbers after it. These are whole numbers. Keyword used for integer data types is int. Integers typically requires 4 bytes of memory space and ranges from -2147483648 to 2147483647.
Character :
If you want to store a character
instead of a number, for this you can use the data types of this category. Keyword used for character data type is char. Characters typically requires 1 byte
of memory space and ranges from -128 to
127 or 0 to 255.
Boolean : Boolean data type is used for storing boolean
or logical values.A boolean
variable can store either true or false. Keyword
used for boolean data type is bool.
Void : Void means without any value. void datatype represents a valueless entity. Void data type is used for those function which does not returns a value.
Floating
point : Floating Point data
type is used for storing single precision floating point values or decimal values. Keyword used for floating point data type is float. Float
variables typically requires 4 byte of memory space.
Double
floating point : Double
Floating Point data type is used
for storing double precision floating point values or decimal values. Keyword used for double floating point data type is double. Double
variables typically requires 8 byte of memory space.
2. DERIVED
DATA TYPE : Derived data types are those derived from basic data types.
Derived data types do not create any new data types, instead add some
functionality to the basic data types itself. There are 4 types of derived data
types found in C ++.
- FUNCTION
- ARRAY
- POINTER
- REFERENCE
You will read all these in detail
in further tutorials.
3. USER DEFINE DATA TYPE : User
defined data types are the data types that you define as programmer. You have
already read about user defined data types in C Language. In C Language you
were told about Struct and Union which are data types of the same category.
These data types are also fully
allowed in C ++. Also C ++ provides you the capabilities to create some new
user define data types that are suitable for object oriented programming. They
are being told below.
CLASS : Class is also like struct. But of
struct you cannot create object and classes is the base of object oriented
programming. Like struct you can also create variables in classes and at the
same time you can also create functions in classes. You will be told about the
class in detail in classes and objects tutorial.
STRUCTURE : Structure This is a collection of different data types. If the structure is to be used, then use the keyword 'struct'. Structure is similar to array. Array is a collection of similar data types and Structure is a collection of different data types. Structure allocates different memory for each member.
UNION : Union is a collection of different data types. If you want to use union, then use the keyword 'union'. Union is similar to this structure. Union does not allocate different memory for each member. Union members share the same memory location. If a member in a union is larger in size, then it is the size of the entire union.
ENUM : An enum data type is a user define data type that attaches numbers with names. This makes it easier to use code. To create an enum data type, you use the enum keyword. After the enum keyword, you give the name of the variable. All values are written inside curly braces separated by (,). These names are automatically assigned values ranging from 0 to values using the enum keyword.
STRUCTURE : Structure This is a collection of different data types. If the structure is to be used, then use the keyword 'struct'. Structure is similar to array. Array is a collection of similar data types and Structure is a collection of different data types. Structure allocates different memory for each member.
UNION : Union is a collection of different data types. If you want to use union, then use the keyword 'union'. Union is similar to this structure. Union does not allocate different memory for each member. Union members share the same memory location. If a member in a union is larger in size, then it is the size of the entire union.
ENUM : An enum data type is a user define data type that attaches numbers with names. This makes it easier to use code. To create an enum data type, you use the enum keyword. After the enum keyword, you give the name of the variable. All values are written inside curly braces separated by (,). These names are automatically assigned values ranging from 0 to values using the enum keyword.
No comments:
Post a Comment
If you have any query, please let me know