Initial setup for MkDocs website

This commit is contained in:
Mohit Mishra
2024-12-21 01:23:20 +05:30
parent 5ba245faaf
commit faffd2b2e5
16566 changed files with 658919 additions and 101 deletions

View File

@@ -0,0 +1,316 @@
# C Programming Language Cheatsheet
## Table of Contents
- [Basic Structure](#basic-structure)
- [Variables and Data Types](#variables-and-data-types)
- [Memory Management](#memory-management)
- [Binary Operators](#binary-operators)
- [Assignment Shortcuts](#assignment-shortcuts)
- [Standard Input/Output Functions](#standard-inputoutput-functions)
- [Standard Library Functions](#standard-library-functions)
- [Best Practices](#best-practices)
- [Advanced Topics](#advanced-topics)
## Basic Structure
### Program Template
```c
/* comment to describe the program */
#include <stdio.h>
/* definitions */
int main(int argc, char **argv) {
/* variable declarations */
/* program statements */
}
```
### Naming Conventions
Variable names must follow these rules:
- Can contain uppercase letters (A to Z)
- Can contain lowercase letters (a to z)
- Can contain numbers (0 to 9)
- Can contain underscores (_)
- Cannot start with a number
- Are case-sensitive
## Variables and Data Types
### Basic Data Types
| Data Type | Description | Example Usage |
|-----------|-------------|---------------|
| int | Integer values | `int count = -1;` |
| char | Character values | `char letter = 'A';` |
| float | Floating point numbers | `float pi = 3.141f;` |
| double | Double precision numbers | `double precise_pi = 3.14159265359;` |
### Variable Declaration Examples
```c
int age;
char initial;
float temperature;
double precise_measurement;
int *pointer_to_int;
char string_array[50];
```
## Memory Management
### Dynamic Memory Allocation
```c
// Allocating memory
int *array = (int *) malloc(sizeof(int) * 10);
if (array == NULL) {
/* handle allocation failure */
}
// Resizing memory
int *newarray = (int *) realloc(array, sizeof(int) * 20);
if (newarray == NULL) {
/* handle reallocation failure */
}
array = newarray;
// Freeing memory
free(array);
```
### Memory Management Best Practices
1. Always check for NULL after malloc/realloc
2. Always free allocated memory when no longer needed
3. Never free memory more than once
4. Update pointers after reallocation
5. Initialize pointers to NULL when declaring
## Binary Operators
### Bitwise Operations
| Operator | Description | Example |
|----------|-------------|----------|
| a & b | Bitwise AND | `result = 5 & 3;` |
| a \| b | Bitwise OR | `result = 5 \| 3;` |
| a ^ b | Bitwise XOR | `result = 5 ^ 3;` |
| a << n | Left shift | `result = 5 << 2;` |
| a >> n | Right shift | `result = 5 >> 2;` |
## Assignment Shortcuts
### Compound Assignment Operators
| Operator | Equivalent | Example |
|----------|------------|---------|
| += | a = a + b | `a += b;` |
| -= | a = a - b | `a -= b;` |
| *= | a = a * b | `a *= b;` |
| /= | a = a / b | `a /= b;` |
| %= | a = a % b | `a %= b;` |
## Standard Input/Output Functions
### Standard Streams
| Stream | Description | Usage |
|--------|-------------|-------|
| stdin | Standard input | Reading user input |
| stdout | Standard output | Normal program output |
| stderr | Standard error | Error messages |
### File Operations
```c
FILE *fopen(char *filename, char *mode);
size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
int fclose(FILE *stream);
```
### Input/Output Functions
```c
// String output
int puts(char *string);
int printf(char *format, ...);
int fprintf(FILE *stream, char *format);
int sprintf(char *string, char *format);
// Character input/output
int getc(FILE *stream);
int putc(int ch, FILE *stream);
int getchar(void);
int putchar(int ch);
```
## Standard Library Functions
### Memory Management Functions
```c
void *malloc(size_t size);
void *realloc(void *ptr, size_t newsize);
void free(void *ptr);
```
### Sorting and Searching
```c
void qsort(void *array, size_t nitems, size_t size,
int (*compar)(void *a, void *b));
void *bsearch(void *key, void *array, size_t nitems,
size_t size, int (*compar)(void *a, void *b));
```
### Random Number Generation
```c
void srand(unsigned int seed);
int rand(void);
```
## Best Practices
### Memory Management Guidelines
1. Always initialize variables before use
2. Check return values from memory allocation functions
3. Free dynamically allocated memory
4. Avoid memory leaks
5. Use appropriate data types for variables
### Code Organization
1. Include necessary header files at the start
2. Define constants and macros after headers
3. Declare global variables (if needed) after definitions
4. Define functions before use or provide prototypes
5. Use meaningful variable and function names
### Error Handling
1. Always check return values of system calls
2. Use stderr for error messages
3. Implement proper error recovery mechanisms
4. Clean up resources in error conditions
5. Use appropriate error codes
## Advanced Topics
### Function Pointers
```c
// Declaration of a function pointer
int (*operation)(int, int);
// Example usage
int add(int a, int b) {
return a + b;
}
operation = &add;
int result = (*operation)(5, 3);
```
### Structures and Unions
```c
struct Point {
int x;
int y;
};
union Data {
int i;
float f;
char str[20];
};
```
### Preprocessor Directives
```c
#define MAX_SIZE 100
#define SQUARE(x) ((x) * (x))
#ifdef DEBUG
// Debug code
#endif
#ifndef HEADER_H
#define HEADER_H
// Header content
#endif
```
### Type Definitions
```c
typedef unsigned long ulong;
typedef struct Point Point;
typedef void (*CallbackFunc)(void *data);
```
### Bit Fields
```c
struct Flags {
unsigned int is_active : 1;
unsigned int is_urgent : 1;
unsigned int priority : 3;
};
```
## Common Programming Patterns
### Buffer Management
```c
#define BUFFER_SIZE 1024
char buffer[BUFFER_SIZE];
size_t bytes_read;
// Safe reading
if ((bytes_read = fread(buffer, 1, BUFFER_SIZE - 1, file)) > 0) {
buffer[bytes_read] = '\0';
}
```
### String Manipulation
```c
// String copy with bounds checking
char dest[50];
const char *src = "Hello, World!";
strncpy(dest, src, sizeof(dest) - 1);
dest[sizeof(dest) - 1] = '\0';
```
### Command Line Arguments
```c
int main(int argc, char **argv) {
for (int i = 1; i < argc; i++) {
printf("Argument %d: %s\n", i, argv[i]);
}
return 0;
}
```
## Debugging Tips
### Common Debugging Techniques
1. Use printf debugging
2. Check return values
3. Validate pointer operations
4. Monitor memory usage
5. Use assert statements
### Assert Usage
```c
#include <assert.h>
void process_data(int *data, size_t size) {
assert(data != NULL);
assert(size > 0);
// Process data
}
```
## Performance Optimization
### Optimization Techniques
1. Use appropriate data structures
2. Minimize memory allocation
3. Optimize loops
4. Use bitwise operations when applicable
5. Consider cache efficiency
### Example Optimizations
```c
// Optimized loop
for (int i = 0; i < size; i++) {
// Cache the value to avoid multiple array accesses
int temp = array[i];
if (temp > max) {
max = temp;
}
}
```

View File

@@ -0,0 +1,173 @@
# GNU Emacs Cheatsheet
## Emacs Commands Relation Diagram
[![](https://mermaid.ink/img/pako:eNqNll2PmzgUQP-KRdWqK000JSRth4eVWqZVqi2UbdI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ghkRIbodcItUZYVRHu1pFxtw5Z_MD2HA4RBfxTG41BNZL0d7wnO7x2T9oOca-SDWb-iWTwa1bpoeMft1Dt3XEtdlwPSbq7y7DxUQ79UMiGwQFDDerYmdCNEzZDZTNEmyHaDI05uuJZRdZnKJvcJA8R2uN61la6fVLZyqb_8LOKhMlwIA-jC1jN6VvIGDj7SY3tJta5Uutc4TpXc9MS1RkzLotCf2ll7wymjwh4bho24hd82AZfuWET7tvDNkj6YRPm1rANZVg3Vs7rnIkEDsffkdhaUDg531ou_Ex4ytqs2Vrb4gegrG3K9bmILbepW35j1WW7P1xu2ioB7feCwUvLLTdlmYTWihX_lWWuoR__A-hhZFE)
## Key Notation
- `C-` means hold the Control key
- `M-` means hold the Alt key (Meta) or press and release Escape
- `C-x` means press Control and 'x' together
- `M-x` means press Alt and 'x' together or press Escape then 'x'
## File Operations
| Command | Description |
|---------|-------------|
| `C-x C-f` | Open a file |
| `C-x C-s` | Save current file |
| `C-x C-w` | Save as... |
| `C-x C-k` | Close file |
| `C-x C-c` | Quit Emacs |
## Text Selection
| Command | Description |
|---------|-------------|
| `C-<space>` or `C-@` | Start selection |
| `C-g` | Deselect |
## Navigation Commands
### Line Navigation
| Command | Description |
|---------|-------------|
| `C-p` | Move to previous line |
| `C-n` | Move to next line |
### Character Navigation
| Command | Description |
|---------|-------------|
| `C-b` | Move back one character |
| `C-f` | Move forward one character |
### Word Navigation
| Command | Description |
|---------|-------------|
| `M-b` | Move back one word |
| `M-f` | Move forward one word |
### Line Boundaries
| Command | Description |
|---------|-------------|
| `C-a` | Go to start of line |
| `C-e` | Go to end of line |
## Copy & Paste Operations
### Traditional Emacs Style
| Command | Description |
|---------|-------------|
| `C-w` | Cut (Kill) |
| `M-w` | Copy |
| `C-y` | Paste (Yank) |
| `C-k` | Cut line after cursor |
| `M-d` | Cut word after cursor |
### Modern Style (CUA Mode)
| Command | Description |
|---------|-------------|
| `M-x cua-mode` | Activate modern copy & paste |
| `C-x` | Cut |
| `C-c` | Copy |
| `C-v` | Paste |
## Undo Operations
| Command | Description |
|---------|-------------|
| `C-/` or `C-x u` | Undo last operation |
## Windows, Frames, and Buffers
### Split Operations
| Command | Description |
|---------|-------------|
| `C-3` | Split window vertically |
| `C-2` | Split window horizontally |
| `C-1` | Remove all splits (single window) |
| `C-0` | Remove current window |
### Buffer Management
| Command | Description |
|---------|-------------|
| `C-x <arrow>` | Cycle through active buffers |
| `C-o` | Visit other split window |
### Frame Management
| Command | Description |
|---------|-------------|
| `C-x 52` | Open a new frame |
## Emergency Commands
| Command | Description |
|---------|-------------|
| `C-g` | Cancel current operation/command |
## Help System
| Command | Description |
|---------|-------------|
| `C-h k <key combo>` | Show key combination binding |
| `C-h b` | Display all key bindings |
## Package Management
| Command | Description |
|---------|-------------|
| `M-x list-packages` | List all available packages |
| `M-x describe-package` | Describe package at cursor |
| `M-x package-menu-mark-install` | Mark package for installation |
| `M-x package-install-selected-packages` | Install marked packages |
## Important Concepts
1. **Buffer**: Holds a process or file content
2. **Window**: Your view of a buffer
3. **Frame**: The desktop window containing Emacs
## Additional Navigation Methods
- Use arrow keys for cursor movement if available
- Use mouse to click and drag for text selection
## Tips for New Users
1. Start with basic file operations and navigation
2. Learn the help system commands early
3. Consider using CUA mode if coming from other editors
4. Practice emergency cancel (`C-g`) for when things go wrong
5. Use package management to extend functionality
## Customization Basics
```elisp
;; Add to your .emacs file or init.el
;; Enable CUA mode for familiar copy-paste
(cua-mode t)
;; Enable line numbers
(global-display-line-numbers-mode)
;; Enable syntax highlighting
(global-font-lock-mode t)
```
## Common Workflows
### File Management Workflow
1. Open Emacs
2. Use `C-x C-f` to find file
3. Edit content
4. Save with `C-x C-s`
5. Close with `C-x C-k` or quit Emacs with `C-x C-c`
### Text Editing Workflow
1. Navigate to location
2. Start selection with `C-<space>`
3. Move cursor to select text
4. Cut/copy as needed
5. Navigate to new location
6. Paste content
### Window Management Workflow
1. Split window as needed (`C-2` or `C-3`)
2. Open different files in splits
3. Use `C-o` to switch between splits
4. Close splits when done (`C-0` or `C-1`)
## Best Practices
1. Learn the help system first
2. Practice navigation commands
3. Use package management for extensions
4. Learn buffer management
5. Master window splitting
6. Understand the difference between kill/yank and modern copy/paste
7. Keep emergency commands in mind