42 Piscine/42 Piscine: Preparation Day 1/Variables and Data Types

## Basic Data Types

C supports several basic data types, including:

* `int` *Used for integers (whole numbers), e.g., `int age = 25;`*
* `float` *Used for floating-point numbers (decimal numbers), e.g., `float temperature = 98.6;`*
* `char` *Used for single characters, e.g., `char grade = 'A';`*
* `double` *Used for double-precision floating-point numbers, e.g., `double price = 19.99;`*
* `bool` *(In C99 and later): Used for boolean values, e.g., `bool isTrue = 1;`*

## Modifiers

You can modify basic data types to specify their size and signedness. For example:

* `short int`: Short integer, e.g., `short int value = 10;` (-32,768 to 32,767)
	+ In most C and C++ implementations, a `short int` is typically 2 bytes in size, which means it can represent values in the range of approximately -32,768 to 32,767. The largest positive number that a `short int` can represent is 32,767, and the smallest negative number is -32,768. These limits are based on a 16-bit representation.
	+ However, it's essential to note that the actual range of a `short int` may vary depending on the system and compiler you are using, as C and C++ standards allow for some flexibility in data type sizes. To determine the specific range of a `short int` on your system, you can check the limits defined in the `<limits.h>` header using `SHRT\_MAX` and `SHRT\_MIN` for the maximum and minimum values, respectively.

* `long int`: Long integer, e.g., `long int population = 1000000;`
	+ In most C and C++ implementations, a `long int` is typically 4 or 8 bytes in size, depending on the system and compiler. This allows it to represent a much larger range of integer values compared to `short int` or `int`.
	+ For a standard 4-byte `long int`, it can typically represent values in the range of approximately -2,147,483,648 to 2,147,483,647. This is the same range as a regular `int`.
	+ However, on systems where `long int` is 8 bytes in size, it can represent a much larger range, typically in the range of approximately -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
	+ The specific range of a `long int` on your system can be determined by checking the limits defined in the `<limits.h>` header, using `LONG\_MAX` and `LONG\_MIN` for the maximum and minimum values, respectively.

* `unsigned int`: Unsigned integer (no negative values), e.g., `unsigned int count = 42;`

## Constants

You can create constants using the `const` keyword. Constants are values that don't change during the program's execution. For example:

```
const int maxScore = 100;

```

## Type Casting

You can convert one data type to another using type casting. For example:

```
int num1 = 10;
float num2 = (float)num1; // Casting 'num1' to a float

```

## Sizeof Operator

You can use the `sizeof` operator to determine the size in bytes of a data type or a variable. For example:

```
int size = sizeof(int); // 'size' will store the size of an int in bytes

```

## Enumerations

Enumerations (enums) allow you to create custom data types with a set of named values. For example:

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

```

## Arrays

Arrays allow you to store multiple values of the same data type in a contiguous memory block. For example:

```
int numbers[5] = {1, 2, 3, 4, 5};

```

## Strings

C doesn't have a built-in string data type. Instead, strings are typically represented as arrays of characters terminated with a null character `'\0'`. For example:

```
char greeting[] = "Hello, World!";

```

You can use the `<string.h>` library for string manipulation functions like `strlen`, `strcpy`, and `strcat`.

## Structures (struct)

A `struct` is a composite data type that allows you to group variables of different data types into a single unit. For example:

```
struct Point {
    int x;
    int y;
};
struct Point p1 = {3, 4};

```

## Union

A `union` is similar to a `struct`, but it allows you to define multiple variables that share the same memory location, which can be useful for conserving memory.

```
union Data {
    int i;
    float f;
    char c;
};
union Data value;
value.i = 42;

```

Understanding data types is essential in C because it affects how you allocate memory, perform operations, and interact with different values in your programs. These are the fundamentals of variables and data types in C.