# stdlib

***

**1. Mathematical Operations:**

```c
#include <stdlib.h>
#include <stdio.h>

int main() {
  int a = 5, b = 6;
  printf("Sum: %d\n", a + b);
  printf("Difference: %d\n", a - b);
  printf("Product: %d\n", a * b);
  printf("Quotient: %d\n", a / b);
  printf("Remainder: %d\n", a % b);
  return 0;
}
```

**2. Random Number Generation:**

```c
#include <stdlib.h>
#include <stdio.h>

int main() {
  int i;
  srand(time(NULL));  // Seed random generator
  for (i = 0; i < 10; i++) {
    printf("Random number: %d\n", rand());
  }
  return 0;
}
```

**3. Memory Allocation and Deallocation:**

```c
#include <stdlib.h>

int main() {
  int *ptr;
  ptr = (int *)malloc(sizeof(int));  // Allocate memory
  *ptr = 10;
  printf("Value stored at pointer: %d\n", *ptr);
  free(ptr);  // Deallocate memory
  return 0;
}
```

**4. Casting:**

```c
#include <stdlib.h>

int main() {
  int a = 10;
  double b = (double)a;  // Cast integer to double
  printf("Double value: %.1f\n", b);
  return 0;
}
```

**5. Converting Strings to Numbers:**

```c
#include <stdlib.h>
#include <stdio.h>

int main() {
  char *str = "123";
  int number = atoi(str);  // Convert string to integer
  printf("Integer: %d\n", number);
  return 0;
}
```

**6. Sorting an Array:**

```c
#include <stdlib.h>

int main() {
  int arr[] = {10, 5, 8, 2, 6};
  int n = sizeof(arr) / sizeof(arr[0]);
  qsort(arr, n, sizeof(int), compare);  // Sort array using quicksort
  for (int i = 0; i < n; i++) {
    printf("%d ", arr[i]);
  }
  return 0;

  // Comparison function for quicksort
  int compare(const void *a, const void *b) {
    return *(int *)a - *(int *)b;
  }
}
```

**7. Searching in an Array:**

```c
#include <stdlib.h>

int main() {
  int arr[] = {10, 5, 8, 2, 6};
  int n = sizeof(arr) / sizeof(arr[0]);
  int target = 8;
  int found = bsearch(&target, arr, n, sizeof(int), compare);  // Search using binary search
  if (found != NULL) {
    printf("Target found at index: %ld\n", found - arr);
  } else {
    printf("Target not found\n");
  }
  return 0;

  // Comparison function for binary search
  int compare(const void *a, const void *b) {
    return *(int *)a - *(int *)b;
  }
}
```

**8. String Manipulation:**

```c
#include <stdlib.h>
#include <string.h>

int main() {
  char str1[] = "Hello";
  char str2[] = "World";
  char *newStr = (char *)malloc(strlen(str1) + strlen(str2) + 1);  // Allocate memory for new string
  strcpy(newStr, str1);  // Copy str1 to new string
  strcat(newStr, str2);  // Concatenate str2 to new string
  printf("New string: %s\n", newStr);
  free(newStr);  // Deallocate memory
  return 0;
}
```

**9. File Reading:**

```c
#include <stdlib.h>
#include <stdio.h>

int main() {
  FILE *file = fopen("test.txt", "r");
  if (file != NULL) {
    char line[100];
    while (fgets(line, sizeof(line), file) != NULL) {
      printf("%s", line);
    }
    fclose(file);
  }
  return 0;
}
```

**10. File Writing:**

```c
#include <stdlib.h>
#include <stdio.h>

int main() {
  FILE *file = fopen("test.txt", "w");
  if (file != NULL) {
    fprintf(file, "Hello World!\n");
    fclose(file);
  }
  return 0;
}
```

**11. Environment Variables:**

```c
#include <stdlib.h>
#include <stdio.h>

int main() {
  char *username = getenv("USER");
  printf("Current user: %s\n", username);
  return 0;
}
```

**12. Command Line Arguments:**

```c
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char *argv[]) {
  for (int i = 0; i < argc; i++) {
    printf("Argument %d: %s\n", i, argv[i]);
  }
  return 0;
}
```

**13. Exit Status:**

```c
#include <stdlib.h>

int main() {
  exit(EXIT_SUCCESS);  // Exit program with success status
}
```

**14. Error Handling:**

```c
#include <stdlib.h>
#include <stdio.h>

int main() {
  FILE *file = fopen("test.txt", "r");
  if (file == NULL) {
    perror("Error opening file");
    exit(EXIT_FAILURE);
  }
  fclose(file);
  return 0;
}
```

**15. Dynamic Array:**

```c
#include <stdlib.h>

int main() {
  int *arr = NULL;
  int size = 0;
  int capacity = 10;

  // Allocate memory for the array
  arr = (int *)malloc(sizeof(int) * capacity);

  // Add elements to the array
  for (int i = 0; i < capacity; i++) {
    arr[i] = i;
    size++;
  }

  // Increase the capacity of the array
  capacity *= 2;
  arr = (int *)realloc(arr, sizeof(int) * capacity);

  // Add more elements to the array
  for (int i = size; i < capacity; i++) {
    arr[i] = i;
    size++;
  }

  // Print the elements of the array
  for (int i = 0; i < size; i++) {
    printf("%d ", arr[i]);
  }

  // Free the memory allocated to the array
  free(arr);
  return 0;
}
```

**16. Linked List:**

```c
#include <stdlib.h>

typedef struct Node {
  int data;
  struct Node *next;
} Node;

int main() {
  Node *head = NULL;  // Pointer to the head of the linked list
  Node *current = NULL;  // Pointer to current node

  // Create a new node and add it to the linked list
  Node *newNode = (Node *)malloc(sizeof(Node));
  newNode->data = 10;
  newNode->next = NULL;
  if (head == NULL) {
    head = newNode;
  } else {
    current = head;
    while (current->next != NULL) {
      current = current->next;
    }
    current->next = newNode;
  }

  // Print the elements of the linked list
  current = head;
  while (current != NULL) {
    printf("%d ", current->data);
    current = current->next;
  }

  // Free the memory allocated to the linked list
  current = head;
  while (head != NULL) {
    current = head->next;
    free(head);
    head = current;
  }
  return 0;
}
```

**17. Binary Tree:**

```c
#include <stdlib.h>

typedef struct Node {
  int data;
  struct Node *left;
  struct Node *right;
} Node;

int main() {
  Node *root = NULL;  // Pointer to the root of the binary tree

  // Create a new node and add it to the binary tree
  Node *newNode = (Node *)malloc(sizeof(Node));
  newNode->data = 10;
  newNode->left = NULL;
  newNode->right = NULL;
  if (root == NULL) {
    root = newNode;
  } else {
    Node *current = root;
    while (current != NULL) {
      if (newNode->data < current->data) {
        if (current->left == NULL) {
          current->left = newNode;
          break;
        } else {
          current = current->left;
        }
      } else {
        if (current->right == NULL) {

```
