What are Data Types in C++?
C++ provides a rich set of data types. Based on nature, size, and associated operations, they are classified. Basically, they are classified into fundamental or built-in data types, derived data types, and user-defined data types.
We used variables to refer data in algorithms. Variables are also used in programs for referencing data. When we write programs in the C + + language, variables are to be declared before their use. Data types are necessary to declare these variables.
Data Types in C++
C + + provides facilities to handle different types of data by providing data type names. Data types are the means to identify the nature of the data and the set of operations that can be performed on the data. Various data types are defined in C + + to differentiate these data characteristics.
- User Defined Data Types
- Derived Data Types
- Fundamental Data Types
User Defined Data Types
User Defined Data type in c++ is a type by which the data can be represented. The type of data will inform the interpreter how the programmer will use the data. A data type can be pre-defined or user-defined. Examples of pre-defined data types are char, int, float, etc. We will discuss user-defined data types in detail.
C++ is flexible enough to allow programmers to define their own data types. Structure (struct), enumeration (enum), union, class, etc. are examples of such data types.
Derived Data Types
A derived data type is a convenient way to group together all the information about a particular item. Derived data types are constructed from fundamental data types through some grouping or alteration in the size. Arrays, pointers, functions, etc. are examples of derived data types.
- A derived data type is like an array because it can have many components.
- But, a derived data type is quite different in that the components that make up the derived type do not have to be all the same type.
- Can you imagine an array in which each element is a different data type?
Fundamental Data Types
Fundamental data types are basic in nature. They cannot be further broken into small units. Since these are defined in the compiler, the size (memory space allocated) depends on the compiler. We use the compiler available in GCC and hence the size, as well as the range of data supported by the data type, are given accordingly.
It may be different if you use other compilers like Turbo C++ IDE. The five fundamental data types are described below:
- Integral Data Types
- Floating Point Data Types
- Void Data Type
Integral Data Types
- Int Data Type (for Integer Numbers)
- Char Data Type (for Character Constants)
Int Data Type (for Integer Numbers)
Integers are whole numbers without a fractional part. They can be positive, zero or negative. The keyword int represents integer numbers within a specific range. GCC allows 4 bytes of memory for integers belonging to int data type.
So the range of values that can be represented by int data type is from -2147483648 to +2147483647. The data items 6900100, 0, -112, 17, -32768, +32767, etc. are examples of int data type. The numbers 2200000000 and -2147483649 do not belong to the int data type, as they are out of the allowed range.
Char Data Type (for Character Constants)
Characters are the symbols covered by the character set of the C++ language. All letters, digits, special symbols, punctuations, etc. come under this category. When these characters are used as data, they are considered as char type data in C++.
We can say that the keyword char represents character literals of C++. Each char type of data is allowed one byte of memory. The data items W, ‘+’, ‘\t’,’0′, etc. belong to char data type. The char data type is internally treated as integers because the computer recognizes the character through its ASCII code.
Character data is stored in the memory with the corresponding ASCII code. As ASCII codes are integers and need to be stored in one byte (8 bits), the range of char data type is from -128 to +127.
Floating Point Data Types
- Float Data Type (for Floating Point Numbers)
- Double Data Type (for Double Precision Floating Point Numbers)
Float Data Type (for Floating Point Numbers)
Numbers with a fractional part are called floating-point numbers. Internally, floating point numbers are stored in a manner similar to scientific notation. The number 47281.97 is expressed as 0.4728197X105 in scientific notation.
The first part of the number, 0.4 728197 is called the mantissa. The power 5 of 10 is called the exponent. Computers typically use exponent form (E notation) to represent floating-point values. Innovation, the number 47281.97 would be 0.4728197ES.
The part of the number before the E is the mantissa, and the part after the E is the exponent. In C++, the keyword float is used to denote such numbers. GCC allows 4 bytes of memory for numbers belonging to float data type. The numbers of this data type have normally a precision of 7 digits.
Double Data Type (for Double Precision Floating Point Numbers)
In some cases, floating-point numbers require more precision. Such numbers are represented by double data type. The range of numbers that can be handled by float type is extended by this data type because it consumes double the size of float data type.
In C ++, it is assured that the range and precision of double will be at least as big as the float. GCC reserves 8 bytes for storing a double-precision value. The precision of double data type is generally 15 digits.
Void Data Type (for Null or Empty Set of Values)
The data type void is a keyword and it indicates an empty set of data. Obviously, it does not require any memory space. The use of this data type will be discussed in detail in Chapter 10. The size of fundamental data types decreases in the order double, float, int, and char.
FAQs About Data Types in C
What are the data types in C++?
Various data types are defined in C + + to differentiate these data characteristics:
1. User Defined Data Types
2. Derived Data Types
3. Fundamental Data Types.