# cstdio

***

**1. Basic Input and Output**

```cpp
#include <cstdio>

int main() {
  int x;
  printf("Enter a number: ");
  scanf("%d", &x);
  printf("You entered: %d\n", x);
}
```

**2. Formatted Output**

```cpp
#include <cstdio>

int main() {
  double x = 3.14159;
  printf("Pi to 5 decimal places: %.5f\n", x);
}
```

**3. File Input and Output**

```cpp
#include <cstdio>

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

**4. Formatted File Input and Output**

```cpp
#include <cstdio>

int main() {
  FILE *file = fopen("test.txt", "r");
  char buffer[100];
  fscanf(file, "%s", buffer);
  printf("Read from file: %s\n", buffer);
  fclose(file);
}
```

**5. Reading and Writing Binary Data**

```cpp
#include <cstdio>

int main() {
  FILE *file = fopen("test.bin", "wb");
  int x = 123;
  fwrite(&x, sizeof(x), 1, file);
  fclose(file);
}
```

**6. Random Access File I/O**

```cpp
#include <cstdio>

int main() {
  FILE *file = fopen("test.txt", "r+");
  fseek(file, 10, SEEK_SET);
  char buffer[100];
  fread(buffer, 1, 100, file);
  printf("Read from file: %s\n", buffer);
  fclose(file);
}
```

**7. Standard Input, Output, and Error Streams**

```cpp
#include <cstdio>

int main() {
  printf("Hello, world!\n");
  int x;
  scanf("%d", &x);
  fprintf(stderr, "Error: %d\n", x);
}
```

**8. Terminal Control**

```cpp
#include <cstdio>

int main() {
  system("clear");  // Clear the screen
  printf("Enter your name: ");
}
```

**9. Input Validation**

```cpp
#include <cstdio>

int main() {
  int x;
  while (scanf("%d", &x) != 1 || x < 0) {
    printf("Invalid input. Enter a positive integer: ");
  }
}
```

**10. Menu-Driven Programs**

```cpp
#include <cstdio>

int main() {
  int choice;
  do {
    printf("Menu:\n");
    printf("1. Option 1\n");
    printf("2. Option 2\n");
    printf("3. Exit\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);
    switch (choice) {
      case 1:
        // Option 1 code
        break;
      case 2:
        // Option 2 code
        break;
      case 3:
        // Exit
        break;
      default:
        printf("Invalid choice. Try again.\n");
    }
  } while (choice != 3);
}
```

**11. String Input and Output**

```cpp
#include <cstdio>

int main() {
  char name[100];
  printf("Enter your name: ");
  scanf("%s", name);
  printf("Hello, %s!\n", name);
}
```

**12. Character Input and Output**

```cpp
#include <cstdio>

int main() {
  char ch;
  printf("Enter a character: ");
  scanf("%c", &ch);
  printf("You entered: %c\n", ch);
}
```

**13. Escape Sequences**

```cpp
#include <cstdio>

int main() {
  printf("Newline:\n");
  printf("Tab:\t");
  printf("Backspace:\b");
}
```

**14. Formatted String Output**

```cpp
#include <cstdio>

int main() {
  char name[] = "John Doe";
  int age = 30;
  printf("Name: %s, Age: %d\n", name, age);
}
```

**15. Hexadecimal and Octal Input and Output**

```cpp
#include <cstdio>

int main() {
  int x = 0x1234;
  int y = 0675;
  printf("Hex: %x, Octal: %o", x, y);
}
```

**16. Binary Input and Output**

```cpp
#include <cstdio>

int main() {
  int x = 10101;
  printf("Binary: %b", x);
}
```

**17. File Inclusion**

```cpp
#include <cstdio>

#include "header.h"

int main() {
  // Code that uses the functions and variables declared in header.h
}
```

**18. Variable-Length Argument Lists**

```cpp
#include <cstdio>
#include <stdarg.h>

int sum(int num_args, ...) {
  int sum = 0;
  va_list args;
  va_start(args, num_args);
  for (int i = 0; i < num_args; i++) {
    sum += va_arg(args, int);
  }
  va_end(args);
  return sum;
}

int main() {
  int total = sum(3, 1, 2, 3);
  printf("Total: %d\n", total);
}
```

**19. Command-Line Arguments**

```cpp
#include <cstdio>

int main(int argc, char **argv) {
  // argc is the number of command-line arguments
  // argv is an array of pointers to the command-line arguments
  for (int i = 0; i < argc; i++) {
    printf("Argument %d: %s\n", i, argv[i]);
  }
}
```

**20. Error Handling**

```cpp
#include <cstdio>

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

  // ... Code to read from the file ...

  fclose(file);
}
```

**21. Dynamic Memory Allocation**

```cpp
#include <cstdio>
#include <stdlib.h>

int main() {
  int *array = (int *)malloc(10 * sizeof(int));
  // ... Code to use the array ...
  free(array);
}
```

**22. Pointer Arithmetic**

```cpp
#include <cstdio>

int main() {
  int array[] = {1, 2, 3, 4, 5};
  int *ptr = array;
  for (int i = 0; i < 5; i++) {
    printf("%d ", *ptr);
    ptr++;  // Increment the pointer to point to the next element
  }
}
```

**23. Structure Declarations and Initialization**

```cpp
#include <cstdio>

struct Point {
  int x;
  int y;
};

int main() {
  struct Point pt = {10, 20};
  printf("Point: (%d, %d)\n", pt.x, pt.y);
}
```

**24. Union Declarations and Initialization**

```cpp
#include <cstdio>

union Data {
  int i;
  float f;
};

int main() {
  union Data data;
  data.i = 10;
  printf("Integer: %d\n", data.i);
  data.f = 3.14f;
  printf("Float: %.2f\n", data.f);
}
```

**25. Bitwise Operators**

```cpp
#include <cstdio>

int main() {
  int x = 5;  // 0101
  int y = 3;  // 0011
  printf("x & y: %d\n", x & y);  // 0001
  printf("x | y: %d\n", x | y);  // 0111
  printf("x ^ y: %d\n", x ^ y);  // 0110
  printf("~x: %d\n", ~x);  // 1010
}
```

**26. Shift Operators**

```cpp
#include <cstdio>

int main() {
  int x = 10;  // 1010
  printf("x >> 2: %d\n", x >> 2);  // 0010
  printf("x << 1: %d\n", x << 1);  // 10100
}
```

**27. Conditional Statements**

```cpp
#include <cstdio>

int main() {
  int x = 10;
  if (x > 0) {
    printf("x is positive.\n");
  } else if (x < 0) {
    printf("x is negative.\n");
  } else {
    printf("x is zero.\n");
  }
}
```

**28. Switch Statements**

```cpp
#include <cstdio>

int main() {
  char grade = 'A';
  switch (grade) {
    case 'A':
      printf("Excellent\n");
      break;
    case 'B':
      printf("Very good\n");
      break;
    case 'C':
      printf("Good\n");
      break;
    default:
      printf("Invalid grade\n");
  }
}
```

**29. Loops**

```cpp
#include <cstdio>

int main() {
  int i;
  for (i = 0; i < 10; i++) {
    printf("%d\n", i);
  }
}
```

**30. While Loops**

```cpp
#include <cstdio>

int main() {
  int i = 0;
  while (i < 10) {
    printf("%d\n", i);
    i++;
  }
}
```

**31. Do-While Loops**

```cpp
#include <cstdio>

int main() {
  int i = 0;
  do {
    printf("%d\n", i);
    i++;
  } while (i < 10);
}
```

**32. Break and Continue Statements**

```cpp
#include <cstdio>

int main() {
  int i;
  for (i = 0; i < 10; i++) {
    if (i == 5) {
      break;  // Exit the loop
    }
    printf("%d\n", i);
  }
}
```

**33. Arrays**

```cpp
#include <cstdio>

int main() {
  int array[] = {1, 2, 3, 4, 5};
  for (int i = 0; i < 5; i++) {
    printf("%d\n", array[i]);
  }
}
```

**34. Pointers**

```cpp
#include <cstdio>

int main() {
  int x = 10;
  int *ptr = &x;
  printf("Value of x: %d\n", *ptr);
}
```

**35. Functions**

```cpp
#include <cstdio>

int sum(int a, int b) {
  return a + b;
}

int main() {
  int result = sum(10, 20);
  printf("Sum: %d\n", result);
}
```

**36. Recursion**

```cpp
#include <cstdio>

int factorial(int n) {
  if (n == 1) {
    return 1;
  } else {
    return n * factorial(n - 1);
  }
}

int main() {
  int result = factorial(5);
  printf("Factorial: %d\n", result);
}
```

**37. Macros**

```cpp
#include <cstdio>

#define MAX_SIZE 100

int main() {
  int array[MAX_SIZE];
  // ... Code to use the array ...
}
```

**38. Inline Functions**

```cpp
#include <cstdio>

inline int sum(int a, int b) {
  return a + b;
}

int main() {
  int result = sum(10, 20);
  printf("Sum: %d\n", result);
}
```

**39. Typedef**

```cpp
#include <cstdio>

typedef int MyInt;

int main() {
  MyInt x = 10;
  printf("Value of x: %d\n", x);
}
```

**40. Structs**

```cpp
#include <cstdio>

struct Point {
  int x;
  int y;
};

int main() {
  struct Point pt = {10, 20};
  printf("Point: (%d, %d)\n", pt.x, pt.y);
}
```

**41. Unions**

```cpp
#include <cstdio>

union Data {
  int i;
  float f;
};

int main() {
  union Data data;
  data.i = 10;
  printf("Integer: %d\n", data.i);
  data.f = 3.14f;
  printf("Float: %.2f\n", data.f);
}
```

**42. Enums**

```cpp
#include <cstdio>

enum Months {
  JANUARY = 1,
  FEBRUARY,
  MARCH,
  // ...
  DECEMBER
};

int main() {
  enum Months month = MARCH;
  printf("Month: %d\n", month);
}
```

**43. Bit Fields**

```cpp
#include <cstdio>

struct Flags {
  unsigned int flag1 : 1;
  unsigned int flag2 : 1;
  unsigned int flag3 : 1;
  // ...
};

int main() {
  struct Flags flags = {1, 1, 0};
  printf("Flags: %d%d%d\n", flags.flag1, flags.flag2, flags.flag3);
}
```

**44. Variable-Length Arrays**

```cpp
#include <cstdio>

int main() {
  int n;
  printf("Enter the number of elements: ");
  scanf("%d", &n);
  int array[n];
  // ... Code to use the array ...
}
```

**45. Function Pointers**

```cpp
#include <cstdio>

typedef int (*FunctionPointer)(int, int);

int sum(int a, int b) {
  return a + b;
}

int main() {
  FunctionPointer func = sum;
  int result = func(10, 20);
  printf("Sum: %d\n", result);
}
```

**46. Dynamic Arrays**

```cpp
#include <cstdio>
#include <stdlib.h>

int main() {
  int n;
  printf("Enter the number of elements: ");
  scanf("%d", &n);
  int *array = malloc(n * sizeof(int));
  // ... Code to use the array ...
  free(array);
}
```

**47. Linked Lists**

```cpp
#include <cstdio>
#include <stdlib.h>

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

int main() {
  struct Node *head = NULL;
  // ... Code to manipulate the linked list ...
}
```

**48. Stacks**

```cpp
#include <cstdio>
#include <stdlib.h>

struct Stack {
  int size;
  int top;
  int *array;
};

int main() {
  struct Stack stack;
  // ... Code to manipulate the stack ...
}
```

**49. Queues**

```cpp
#include <cstdio>
#include <stdlib.h>

struct Queue {
  int size;
  int front;
  int rear;
  int *array;
};

int main() {
  struct Queue queue;
  // ... Code to manipulate the queue ...
}
```

**50. Binary Search Trees**

```cpp
#include <cstdio>
#include <stdlib.h>

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

int main() {
  struct Node *root = NULL;
  // ... Code to manipulate the binary search tree ...
}
```
