C++ Data types

Data types in C++ determine the kind of data a variable can store and how much memory it will occupy. C++ supports various data types, which can be broadly classified into four categories:

1. Basic Data Types

2. Derived Data Types

3. Enumeration Data Types

4. User-Defined Data Types

1. Basic Data Types

Basic data types are the foundational types in C++. They include:

Integer Types: Used to store whole numbers.

Floating-Point Types: Used to store numbers with decimal points.

Character Types: Used to store individual characters.

Integer Types:

Type Size Range
char 1 byte -128 to 127
signed char 1 byte -128 to 127
unsigned char 1 byte 0 to 255
short 2 bytes -32,768 to 32,767
signed short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
int 4 bytes -2,147,483,648 to 2,147,483,647
signed int 4 bytes -2,147,483,648 to 2,147,483,647
unsigned int 4 bytes 0 to 4,294,967,295
long 4 bytes -2,147,483,648 to 2,147,483,647
signed long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295

Floating-Point Types:

Type Size Range
float 4 bytes Approx. ±3.4 × 10^38 (6-7 decimal places)
double 8 bytes Approx. ±1.7 × 10^308 (15-16 decimal places)
long double 10 bytes Approx. ±1.1 × 10^4932 (19-20 decimal places)

Character Types:

Type Size Range
char 1 byte -128 to 127 (signed) or 0 to 255 (unsigned)
wchar_t 2 or 4 bytes Represents wide characters (varies by implementation)

2. Derived Data Types

Derived data types are built from the basic data types:


Arrays: Collections of elements of the same type.

Pointers: Variables that store memory addresses of other variables.

References: Aliases for other variables.

Functions: Defined to perform specific tasks and can return values.

3. Enumeration Data Types

Enumerations (enums) are user-defined types consisting of a set of named integral constants. They enhance code readability by giving meaningful names to integral values.

Example:

enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };

4. User-Defined Data Types

User-defined types allow for creating complex data structures:


Structures (struct): Allows grouping of variables under a single name. Each variable in the structure is called a member.

Example:

struct Person {
std::string name;
int age;
float height;
};

Classes: An extension of structures that includes methods and access control. Classes are fundamental in object-oriented programming.

Example:

class Car {
private:
std::string model;
public:
void setModel(std::string m) { model = m; }
std::string getModel() { return model; }
};

Unions: Allows storing different data types in the same memory location. Only one member can hold a value at a time.

Example:

cpp
Copy code
union Data {
int i;
float f;
char c;
};

Typedefs and using: Allows creating new names (aliases) for existing data types.

Example:

typedef unsigned long ulong;
using uint = unsigned int;

Additional Concepts

Fixed-Width Integer Types: Introduced in C++11, these types ensure consistent size across platforms (int8_t, uint16_t, int32_t, etc.).

Sizeof Operator: Determines the size of a data type or variable in bytes.

Example:

std::cout << "Size of int: " << sizeof(int) << " bytes" << std::endl;

Character and String Types: std::string and char arrays are used for text. Wide characters (wchar_t) and Unicode support are also available.