C++ Programming
Topperworld.in
Data Types
• Data types are used to tell the variables the type of data they can store.
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++ supports a wide variety of data types and the programmer can select
the data type appropriate to the needs of the application.
• Data types specify the size and types of values to be stored. However,
storage representation and machine instructions to manipulate each data
type differ from machine to machine, although C++ instructions are
identical on all machines.
❖ Types of Data Types
©Topperworld
C++ Programming
Types Data Types
Basic Data Type int, char, float, double, etc
Derived Data Type array, pointer, etc
Enumeration Data Type enum
User Defined Data Type structure
❖ Basic Data Type
These data types are built-in or predefined data types and can be used
directly by the user to declare variables. example: int, char, float, bool, etc.
Primitive data types available in C++ are:
• Integer
• Character
• Boolean
• Floating Point
• Double Floating Point
• Valueless or Void
• Wide Character
➢ Integer: The keyword used for integer data types is int. Integers typically
require 4 bytes of memory space and range from -2147483648 to
2147483647.
➢ Character: Character data type is used for storing characters. The
keyword used for the character data type is char. Characters typically
require 1 byte of memory space and range 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. The keyword used for
the Boolean data type is bool.
©Topperworld
C++ Programming
➢ Floating Point: Floating Point data type is used for storing single-
precision floating-point values or decimal values. The keyword used for
the floating-point data type is float. Float variables typically require 4
bytes of memory space.
➢ Double Floating Point: Double Floating Point data type is used for
storing double-precision floating-point values or decimal values. The
keyword used for the double floating-point data type is double. Double
variables typically require 8 bytes of memory space.
➢ void: Void means without any value. void data type represents a
valueless entity. A void data type is used for those function which does
not return a value.
➢ Wide Character: Wide character data type is also a character data type
but this data type has a size greater than the normal 8-bit data type.
Represented by wchar_t. It is generally 2 or 4 bytes long.
Example:
// C++ Program to Demonstrate the correct size
// of various data types on your computer.
#include <iostream>
using namespace std;
int main()
{
cout << "Size of char : " << sizeof(char) << endl;
cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of long : " << sizeof(long) << endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;
return 0;
}
©Topperworld
C++ Programming
Output:
Size of char : 1
Size of int : 4
Size of long : 8
Size of float : 4
Size of double : 8
❖ Derived Data Types
Derived data types that are derived from the primitive or built-in datatypes
are referred to as Derived Data Types. These can be of four types namely:
• Function
• Array
• Pointer
• Reference
❖ Enumerations
Enumeration (or enum) is a user defined data type in C. It is mainly used to
assign names to integral constants, the names make a program easy to read
and maintain
The keyword ‘enum’ is used to declare new enumeration types in C and C++.
Following is an example of enum declaration.
Example:
// An example program to demonstrate working
// of enum in C
#include<stdio.h>
enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};
©Topperworld
C++ Programming
int main()
{
enum week day;
day = Wed;
printf("%d",day);
return 0;
}
Output:
❖ Abstract or User-Defined Data Types
Abstract or User-Defined data types are defined by the user itself. Like,
defining a class in C++ or a structure. C++ provides the following user-defined
datatypes:
• Class
• Structure
• Union
• Typedef defined Datatype
❖ Datatype Modifiers
As the name suggests, datatype modifiers are used with built-in data types to
modify the length of data that a particular data type can hold.
Data type modifiers available in C++ are:
• Signed
• Unsigned
• Short
• Long
©Topperworld
C++ Programming
➢ signed Modifier
Signed variables can store positive, negative integers, and zero.
Example:
signed int a = 45;
signed int b = -67;
signed int c = 0;
➢ unsigned Modifier
Unsigned variables can store only non-negative integer values.
Example:
unsigned int a = 9;
unsigned int b = 0;
➢ short Modifier
The short keyword modifies the minimum values that a data type can
hold. It is used for small integers that lie in the range of −32,767 to
+32,767.
Example:
short int x = 4590;
➢ long Modifier
The long keyword modifies the maximum values that a data type can
hold. It is used for large integers that are in the range of -2147483647 to
2147483647.
Example:
long int y = 26936;
©Topperworld
C++ Programming
❖ Advantages
• Data types provide a way to categorize and organize data in a program,
making it easier to understand and manage.
• Each data type has a specific range of values it can hold, allowing for
more precise control over the type of data being stored.
• Data types help prevent errors and bugs in a program by enforcing strict
rules about how data can be used and manipulated.
• C++ provides a wide range of data types, allowing developers to choose
the best type for a specific task.
❖ Disadvantages
• Using the wrong data type can result in unexpected behavior and errors
in a program.
• Some data types, such as long doubles or char arrays, can take up a large
amount of memory and impact performance if used excessively.
• C++’s complex type system can make it difficult for beginners to learn
and use the language effectively.
• The use of data types can add additional complexity and verbosity to a
program, making it harder to read and understand.
©Topperworld