Data Types in C++

Data Types in C++:-

            Data type is used to determine what type of value a variable or a constant can contain throughout the program. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data-type with which it is declared. Every data type requires a different amount of memory. C++ language is rich on its data types. The variety of data types available to allow the programmer to select the type appropriate to the needs of the application as well as the machine. In C++ language, different variables contain different data types e.g. Roll No contains an ‘integer’ value whereas percentage contains a 'float' value.

            Data types are divided into three groups:

A.    Primary (or Fundamental) data types.

B.     Derived data types.

C.    User-defined data types.

 


















A.    Primary (or Fundamental) data types :-

All C++ compilers supports five fundamental data types, namely integer(int), character(char), floating point(float), double-precision(double) and void.

Character type:

            A single character can be defined as char data type. e.g. 'a', 'z', 'e', etc. Keyword used for character data type is char. Characters typically requires 1 byte of memory space. A variable declared as 'char' can store only single character e.g. Yes or No Choice requires only 'y' or 'n' as an answer.

Integer data types:

 Integer data type is used to store numeric values without any decimal point e.g. 7, -101, 107, etc. A variable declared as 'int' must contain whole numbers e.g. age is always in number. Keyword used for integer data types is int. Integers typically requires 4 bytes of memory. Data type modifiers available in C++ are signed, unsigned, short, long.

Floating point types:

            The float data type is used to store numeric values with decimal point i.e. up to 6 precision points. In other words, float data type is used to store real values, e.g. 3.14, 7.67 etc. Keyword used for floating point data type is float. Float variables typically requires 4 byte of memory space. A variable declared as float must contain decimal values e.g. percentage, price, pi, area etc.

Double-precision type:

When accuracy provided by float is not sufficient, the type double can be used to define number. These are known as double precision. The double data type represents the same data type that float represent but with greater precision. To extend the precision, we may use long double. Keyword used for double floating point data type is double. A double variable typically requires 8 byte of memory space.

Boolean Data Type:

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.

Wide Character Data Type:

Wide character data type is also a character data type but this data type has size greater than the normal 8-bit data type. Represented by wchar_t. It is generally 2 or 4 bytes long.

Void type:

            The void type has no value. This is usually used to specify the type of functions. The type of a function said to be void when it does not return any value. It is used to represent an empty value.

 

B.      Derived data types:-

Data types that are derived from fundamental data types are called derived data types. Derived data types don't create a new data type; instead, they add some functionality to the basic data types. Two derived data type are - Array & Pointer.

Array: An array is a collection of variables of same type i.e. collection of homogeneous data referred by a common name. In memory, array elements are stored in a continuous location.

Pointer: A pointer is a special variable that holds a memory address (location in memory) of another variable.

Function: Function is group of statements together perform a particular task.

C.    User-defined data types:-

User defined data type is used to create new data types. The new data types formed are fundamental data types. Different user defined data types are: class, struct, union, enum, typedef.

Class: Class is collection of data member and member function which represent real world object.

Struct: A struct is a user defined data type that stores multiple values of same or different data types under a single name. In memory, the entire structure variable is stored in sequence.

Union: A union is a user defined data type that stores multiple values of same or different data types under a single name. In memory, union variables are stored in a common memory location.

             Enum: An enumeration is a data type similar to a struct or a union. Its members are constants that are written as variables, though they have signed integer values. These constant represent values that can be assigned to corresponding enumeration variables. 

Comments

Popular posts from this blog

Tokens in C++

Variables in C++