Contents
Data Types
Data Types in C++
In C++, data types define the type of data that variables can hold, helping ensure that the stored values match the variable’s purpose. During variable declaration, data types restrict the kind of data that the variable can store, and the compiler allocates memory for that variable based on its declared type. Different data types require different amounts of memory, which may vary between machines due to hardware differences, even though the C++ code remains the same.
C++ offers a wide variety of data types, allowing programmers to choose the most suitable type for their specific needs. These types are categorized as follows:
1. Primitive Data Types: These are built-in data types available directly in C++, such as int, char, float, and bool.
- Integer (int): Typically requires 4 bytes of memory and stores whole numbers.
- Character (char): Uses 1 byte, stores single characters or ASCII values.
- Boolean (bool): Holds true or false values.
- Float (float): Stores single-precision decimal values, typically using 4 bytes.
- Double (double): For double-precision decimal values, using around 8 bytes.
- Void (void): Represents no value and is often used for functions that return nothing.
2. Derived Data Types: These are derived from primitive types, including arrays, pointers, and references.
3. User-Defined Data Types: Created by the programmer, these include classes, structures, and enumerations.
Example:
#include
using namespace std;
int main() {
cout << "Size of char: " << sizeof(char) << " byte" << endl;
cout << "Size of int: " << sizeof(int) << " bytes" << endl;
cout << "Size of float: " << sizeof(float) << " bytes" << endl;
cout << "Size of double: " << sizeof(double) << " bytes" << endl;
return 0;
}
Output:
Size of char: 1 byte
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Data type sizes may vary depending on your compiler and system configuration.
Datatype Modifiers
Modifiers in C++ allow adjustments to the length of data a specific data type can hold. Modifiers include signed
, unsigned
, short
, and long
.
Table of Common Data Types with Modifiers:
Data Type | Size (in bytes) | Range |
---|---|---|
short int | 2 | -32,768 to 32,767 |
unsigned short int | 2 | 0 to 65,535 |
unsigned int | 4 | 0 to 4,294,967,295 |
long int | 4 or 8 | Depends on system architecture |
unsigned long int | 4 or 8 | 0 to larger positive values |
long long int | 8 | Large range |
unsigned long long | 8 | Larger positive values |
float | 4 | Decimal values, single-precision |
double | 8 | Decimal values, double-precision |
We can display the range of each data type by using constants from <limits.h>
.
Example:
#include
#include
using namespace std;
int main() {
cout << "Integer type range:\n";
cout << "Minimum int: " << INT_MIN << endl;
cout << "Maximum int: " << INT_MAX << endl;
cout << "Floating type example:\n";
cout << "Size of float: " << sizeof(float) << " bytes" << endl;
cout << "Size of double: " << sizeof(double) << " bytes" << endl;
cout << "Character type example:\n";
cout << "Minimum char: " << CHAR_MIN << endl;
cout << "Maximum char: " << CHAR_MAX << endl;
return 0;
}
Output:
Integer type range:
Minimum int: -2147483648
Maximum int: 2147483647
Floating type example:
Size of float: 4 bytes
Size of double: 8 bytes
Character type example:
Minimum char: -128
Maximum char: 127
Advantages and Disadvantages
Advantages:
- Data types organize data effectively in a program.
- They provide control over data size and range, reducing errors.
- C++ offers a wide range of data types, which aids in precise programming.
Disadvantages:
- Incorrect data type usage can lead to unexpected behavior.
- Some data types consume more memory, potentially affecting performance.
- The complex type system in C++ can challenge beginners.
Derived Data Types in C++
In C++, data types are crucial for defining how data is stored, manipulated, and understood by the compiler. There are three main categories:
- Pre-defined (Primitive) Data Types
- User-defined Data Types
- Derived Data Types
Derived Data Types in C++ are created from pre-existing data types and include:
- Function
- Array
- Pointers
- References
1. Function: A function is a reusable block of code designed to perform a specific task. It prevents code duplication by encapsulating instructions that can be invoked multiple times.
Syntax:
FunctionType FunctionName(parameters);
Example:
#include
using namespace std;
int max(int x, int y) {
return (x > y) ? x : y;
}
int main() {
int a = 10, b = 20;
cout << "Max is " << max(a, b);
return 0;
}
Output:
Max is 20
2. Arrays: An array is a collection of elements stored in contiguous memory locations, all of which are of the same type. Arrays allow multiple data items to be represented with a single variable name.
Syntax:
DataType ArrayName[size_of_array];
Example:
#include
using namespace std;
int main() {
int arr[5];
arr[0] = 5;
arr[2] = -10;
arr[1] = 2;
arr[3] = arr[0];
cout << arr[0] << " " << arr[1] << " " << arr[2] << " " << arr[3];
return 0;
}
Output:
5 2 -10 5
3. Pointers: Pointers hold the memory address of another variable. They are useful for dynamic memory allocation, manipulating data structures, and enabling functions to simulate call-by-reference.
Example:
#include
using namespace std;
void displayPointerExample() {
int var = 20;
int* ptr = &var;
cout << "Address stored in ptr = " << ptr << endl;
cout << "Value of var = " << var << endl;
cout << "Value pointed by ptr = " << *ptr << endl;
}
int main() {
displayPointerExample();
return 0;
}
Output:
Address stored in ptr = 0x7ffc04f3f894
Value of var = 20
Value pointed by ptr = 20
4. References: A reference is an alternative name for an existing variable. References are often used to avoid copying data and enable efficient parameter passing in functions.
#include
using namespace std;
int main() {
int x = 10;
int& ref = x;
ref = 20;
cout << "x = " << x << endl;
x = 30;
cout << "ref = " << ref << endl;
return 0;
}
Output:
x = 20
ref = 30
User Defined Data Types in C++
User-defined data types are types that are created by the programmer, extending the built-in data types. These types include:
- Class
- Structure
- Union
- Enumeration (enum)
- Typedef
1. Class: A Class in C++ serves as the blueprint for creating objects, encapsulating data and functions that operate on the data. It is the foundation of Object-Oriented Programming in C++.
Syntax:
class ClassName {
// Access specifier
public:
// Data Members and Member Functions
};
Example:
#include
using namespace std;
class Student {
public:
string name;
void display() {
cout << "Student's name: " << name << endl;
}
};
int main() {
Student s1;
s1.name = "Alice";
s1.display();
return 0;
}
Output:
Student's name: Alice
2. Structure : A Structure is a collection of variables (possibly of different types) under a single name. It is commonly used to group related data.
Example:
#include
using namespace std;
struct Point {
int x, y;
};
int main() {
Point p1;
p1.x = 5;
p1.y = 10;
cout << "Point: (" << p1.x << ", " << p1.y << ")" << endl;
return 0;
}
Output:
Point: (5, 10)
3. Union: A Union is similar to a structure, but all members share the same memory location, meaning it can store only one of its members’ values at any given time.
Example:
#include
using namespace std;
union Data {
int integer;
char character;
};
int main() {
Data d;
d.integer = 65;
cout << "Integer: " << d.integer << endl;
d.character = 'B';
cout << "Character: " << d.character << endl;
return 0;
}
Output:
Integer: 65
Character: B
4. Enumeration (enum) : An Enumeration is a user-defined type consisting of a set of named integral constants. It helps improve code readability and maintainability by providing meaningful names to numeric values.
Examples:
#include
using namespace std;
enum Week { Sun = 1, Mon, Tue, Wed, Thu, Fri, Sat };
int main() {
Week day = Tue;
cout << "Day value: " << day << endl;
return 0;
}
Output:
Day value: 3
5. Typedef : The typedef keyword in C++ allows you to create an alias for existing data types, making the code more readable and maintainable.
Example:
#include
using namespace std;
typedef unsigned int UINT;
int main() {
UINT age = 25;
cout << "Age: " << age << endl;
return 0;
}
Output:
Age: 25
C++ Type Modifiers
Modifiers in C++ provide additional context to basic data types, helping them better suit specific needs by changing their range or behavior. Modifiers are prefixed to primitive data types and include:
- signed
- unsigned
- short
- long
These modifiers apply to the following built-in data types in C++:
1. signed Modifier : The signed modifier allows a variable to hold positive and negative values, as well as zero.
Example:
#include
using namespace std;
int main() {
cout << "Size of signed int: " << sizeof(signed int) << " bytes" << endl;
cout << "Size of signed short: " << sizeof(signed short) << " bytes" << endl;
return 0;
}
Output:
Size of signed int: 4 bytes
Size of signed short: 2 bytes
2. unsigned Modifier : The unsigned modifier restricts a variable to non-negative values, allowing it to only hold positive integers and zero.
Example:
#include
using namespace std;
int main() {
cout << "Size of unsigned int: " << sizeof(unsigned int) << " bytes" << endl;
cout << "Size of unsigned short: " << sizeof(unsigned short) << " bytes" << endl;
return 0;
}
Output:
Size of unsigned int: 4 bytes
Size of unsigned short: 2 bytes
3. short Modifier : The short modifier reduces the storage size of an integer, usually used for small values in the range from −32,767 to +32,767.
Example:
#include
using namespace std;
int main() {
cout << "Size of short: " << sizeof(short) << " bytes" << endl;
return 0;
}
Output:
Size of short: 2 bytes
4. long Modifier : The long modifier increases the storage size for a data type, allowing it to store larger integer values.
Example:
#include
using namespace std;
int main() {
cout << "Size of long int: " << sizeof(long int) << " bytes" << endl;
cout << "Size of long double: " << sizeof(long double) << " bytes" << endl;
return 0;
}
Output:
Size of long int: 8 bytes
Size of long double: 16 bytes