- Basic Syntax
- Structures and Unions
- Preprocessor Directives
- Pointers to Functions
- Command Line Arguments
- Type Casting
- Error Handling
- Common Libraries
- Sorting Algorithms
- Linked Lists
- Memory Management
- File Operations
- String Manipulations
- Pointers and Arrays
- Debugging and Error Handling
- Best Practices
- Code Styling
- Compiling and Building
- Concurrency
- Additional Notes
-
Variables and Types
int age = 30; // Integer float price = 19.99; // Floating point number char grade = 'A'; // Character
-
Conditional Statements
if (condition) { /* code if condition is true */ } else if (another_condition) { /* code if another_condition is true */ } else { /* code if all conditions are false */ }
-
Loops
for (int i = 0; i < 10; i++) { /* code executed 10 times */ } while (condition) { /* code executed while condition is true */ } do { /* code */ } while (condition); // code executed at least once and then while condition is true
-
Functions
returnType functionName(parameters) { /* function body */ }
- Usage
struct Person { char name[50]; int age; float salary; };
- Usage
#define PI 3.14 #include <stdio.h>
- Usage
void (*fun_ptr)(int) = &fun;
- Usage
int main(int argc, char *argv[]) { /* code */ }
- Usage
int x = 10; double y = (double)x;
- Usage
if ((fp = fopen("file.txt", "r")) == NULL) { perror("Error opening file"); return(-1); }
- Usage
#include <stdio.h> // Standard input/output functions #include <stdlib.h> // General purpose functions #include <string.h> // String handling functions #include <math.h> // Mathematical functions
-
Bubble Sort
void swap(int* a, int* b) { int temp = *a; *a = *b; *b = temp; } void bubbleSort(int arr[], int n) { for (int i = 0; i < n-1; i++) for (int j = 0; j < n-i-1; j++) if (arr[j] > arr[j+1]) swap(&arr[j], &arr[j+1]); }
-
Insertion Sort
void insertionSort(int arr[], int n) { for (int i = 1; i < n; i++) { int key = arr[i], j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j = j - 1; } arr[j + 1] = key; } }
-
Definition
struct Node { int data; struct Node* next; };
-
Creating a Node
struct Node* createNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = NULL; return newNode; }
-
Inserting a Node
void insertNode(struct Node** head, int data) { struct Node* newNode = createNode(data); newNode->next = *head; *head = newNode; }
-
Deleting a Node
void deleteNode(struct Node** head, int key) { struct Node* temp = *head, *prev; if (temp != NULL && temp->data == key) { *head = temp->next; free(temp); return; } while (temp != NULL && temp->data != key) { prev = temp; temp = temp->next; } if (temp == NULL) return; prev->next = temp->next; free(temp); }
-
Traversing a Linked List
void printList(struct Node* node) { while (node != NULL) { printf(" %d ", node->data); node = node->next; } }
-
Reversing a Linked List
struct Node* reverse(struct Node* head) { struct Node* prev = NULL; struct Node* current = head; struct Node* next = NULL; while (current != NULL) { next = current->next; current->next = prev; prev = current; current = next; } head = prev; return head; }
-
Dynamic Allocation
int* ptr = malloc(sizeof(int) * n); // Allocate memory if (ptr == NULL) { /* handle allocation failure */ } free(ptr); // Free memory ptr = NULL; // Good practice after freeing memory
-
Memory Copy
memcpy(dest, src, n); // Copies n bytes from src to dest
- Reading and Writing Files
FILE *fp = fopen("file.txt", "r"); // Open for reading if (fp == NULL) { /* handle file open error */ } char buffer[100]; fgets(buffer, 100, fp); // Read from file fclose(fp); // Close file fp = fopen("file.txt", "w"); // Open for writing if (fp == NULL) { /* handle file open error */ } fprintf(fp, "Hello, world!\n"); // Write to file fclose(fp); // Close file
- Common Functions
size_t len = strlen(s); // Get string length strcpy(dst, src); // Copy string strcat(s1, s2); // Concatenate strings int cmp = strcmp(s1, s2); // Compare strings
- Usage
int arr[10]; // Declare an array int *p = &arr[0]; // Pointer to the array p[2] = 5; // Set 3rd element of the array to 5
-
Check Return Values
if (fopen("file.txt", "r") == NULL) { /* handle file open error */ }
-
Asserts
#include <assert.h> assert(ptr != NULL); // Assert that ptr is not NULL
- Avoid Memory Leaks: Always
free
allocated memory and set the pointer to NULL. - Buffer Overflows: Prefer
strncpy
overstrcpy
to avoid buffer overflow. - Use
const
Keyword: For non-modifiable parameters and variables. - Modular Programming: Use functions and split code across multiple files for better organization.
- Comments: Document the "why", not the "what". Use comments to explain complex code sections.
- Be consistent with naming conventions (snake_case, camelCase).
- Use consistent indentation (e.g., 4 spaces) and spacing.
- Choose a consistent style for curly braces.
- Compiler Warnings: Use
-Wall
to enable all compiler warnings. - Makefiles: Use for managing larger projects.
- Thread Safety: Use mutexes for protecting shared resources.
- Atomic Operations: Use for certain operations on shared variables to prevent race conditions.
- Error Handling: Always validate the return values from library functions for potential errors.
- Dynamic Memory: Check for
NULL
frommalloc
indicating allocation failure. - File Operations: Ensure
fclose
is called after finishing operations on a file to avoid resource leaks.