Data Types
What is data type?
- Data types determine the kind of operations that can be performed on the data and the way the data is stored in memory.
- A data type is a classification that specifies the type of data that a variable or object can hold.
- Data types helps the computer understand how to store, use, and manage the data in your programs.
- In simple words, a data type is a label that defines what kind of value a variable can hold.
- Common data types include:
- Integer (int): Whole numbers, e.g., 1, 2, 3, etc.
- Float: Decimal numbers, e.g., 3.14, -0.5, etc.
- String (str): Text, e.g., “hello”, “goodbye”, etc.
- Boolean (bool): True or False values
- Character (char): Single characters, e.g., ‘a’, ‘B’, etc.
- Date and Time: Dates and times, e.g., 2022-01-01, 12:30:00, etc.
- Array: Collections of values of the same type, e.g., [1, 2, 3], [“a”, “b”, “c”], etc.
- Object: Collections of key-value pairs, e.g., {name: “John”, age: 30}, etc.
Types of Data Types:
In programming, data types can be broadly categorized into several types. Here are the main types of data types:
1. Primitive Data Types:
It is the most basic types of data that a programming language provides. They represent single values and are usually built into the language.
- Integer (int): Represents whole numbers.
(e.g., -3, 0, 42)
- Floating Point (float): Represents real numbers with fractional parts.
(e.g., -3.14, 0.0, 42.1)
- Character (char): Represents a single character.
(e.g., ‘a’, ‘1’, ‘$').
- Boolean (bool): Represents true or false values.
(e.g., true, false)
2. User-Defined Data Types:
User-defined data types are custom data types created by programmers to suit specific needs. They are constructed using primitive data types and other user-defined types.
- Structures (struct in C/C++): Groups different types of data under a single name.
- Classes (class in C++, Java, Python): Defines objects with properties (attributes) and behaviors (methods).
- Enumerations (enum): Defines a set of named values.
- Unions: Similar to structs, but all members share the same memory space.
- Typedefs: typedef is a keyword in programming languages such as C and C++ that allows developers to create an alias or a new name for an existing data type.
- Arrays: Collections of values of the same type, defined by the user.
- Objects: Composite data types that combine multiple key-value pairs.
3. Derived Data Types:
Derived data types are formed from primitive or user-defined data types. They often provide more complex data structures for managing collections of data.
- Arrays: Collections of elements of the same type.
- Pointers: Variables that store the memory address of another variable.
- References (in C++): An alias for another variable.
- Classes: In object-oriented programming, classes are derived from existing classes or interfaces.
- Sets: Collections of unique elements
4. Special Data Types:
- Pointer: A variable that stores the memory address of another variable.
- Null: Represents the absence of a value or a null reference.
why we use data types?
- Data types are fundamental in programming because they define the nature of data that variables can hold and how that data can be manipulated.
- By specifying data types, programmers ensure efficient memory management, as each type requires a specific amount of memory.
- They enhance code readability and maintainability, making it clear what kind of data is expected and how it should be used.
- Data types facilitate function and operator overloading, allowing the same function or operator to work with different data types.
- Overall, data types provide a robust framework that enhances the reliability, efficiency, and readability of code, making them necessary in programming.
Data type ranges:
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 |
int | 4 | -2,147,483,648 to 2,147,483,647 |
long int | 4 | -2,147,483,648 to 2,147,483,647 |
unsigned long int | 4 | 0 to 4,294,967,295 |
long long int | 8 | -(2^63) to (2^63)-1 |
unsigned long long int | 8 | 0 to 18,446,744,073,709,551,615 |
signed char | 1 | -128 to 127 |
unsigned char | 1 | 0 to 255 |
float | 4 | -3.4×10^38 to 3.4×10^38 |
double | 8 | -1.7×10^308 to1.7×10^308 |
long double | 12 | -1.1×10^4932 to1.1×10^4932 |
wchar_t | 2 or 4 | 1 wide character |
Integer Types:
-
char:
- Signed: -128 to 127
- Unsigned: 0 to 255
- Typically char stores 1 byte
-
short:
- Signed: -32,768 to 32,767
- Unsigned: 0 to 65,535
- Typically short stores 2 bytes
-
int:
- Signed: -2,147,483,648 to 2,147,483,647
- Unsigned: 0 to 4,294,967,295
- Typically int stores 4 bytes
-
long:
- Signed: -2,147,483,648 to 2,147,483,647 (same as int on many systems)
- Unsigned: 0 to 4,294,967,295 (same as unsigned on many systems)
- Typically stores 4 bytes on 32-bit systems and 8 bytes on 64-bit systems
-
long long:
- Signed: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
- Unsigned: 0 to 18,446,744,073,709,551,615
- Stores 8 bytes
Floating-Point Types:
-
float:
- Typically 4 bytes
-
double:
- Typically 8 bytes
-
long double:
- Typically 8, 12, or 16 bytes
Boolean Type:
- bool:
- It stores 1 byte
- Represents true or false values, typically stored as 1 bit, but often occupies 1 byte for alignment purposes.