C
C programing
How to C
Basics
C uses a simple syntax with curly braces , semicolons ;, and main() as the entry point. Variables must be declared before use, and there is no default initialization.
int main() {
int x = 10; // Variable declaration
printf("%d", x); // Output: 10
return 0; // Return statement
}Memory Management
C requires manual memory management. Use malloc or calloc to allocate memory and free to release it. Avoid dangling pointers and memory leaks.
int* arr = (int*)malloc(10 * sizeof(int)); // Allocate memory
if (arr == NULL) { /* Handle allocation failure */ }
free(arr); // Free memoryStrings
Strings in C are arrays of char terminated by \0. Use functions from <string.h> like strlen, strcpy, and strcat.
char str[] = "Hello";
printf("%d", strlen(str)); // Output: 5Pointers
Pointers store memory addresses. Use * to dereference and & to get the address of a variable. Pointer arithmetic is based on the size of the type.
int x = 10;
int* ptr = &x; // Pointer to x
printf("%d", *ptr); // Output: 10Preprocessor Directives
Use #define for macros and #include to include headers. Use include guards to prevent double inclusion.
#ifndef MY_HEADER_H #define MY_HEADER_H // Declarations go here #endif
Functions
Functions must be declared before use. Use function prototypes in headers. Arguments are passed by value; use pointers for pass-by-reference.
int add(int a, int b) {
return a + b;
}Arrays
Arrays in C are fixed-size by default. Use malloc and realloc for dynamic arrays. Be careful with out-of-bounds access.
int arr[10]; // Fixed-size array int* dynArr = (int*)malloc(10 * sizeof(int)); // Dynamic array
Structs
Structs group related data. Use . to access members of instances and -> for pointers to structs.
struct Point {
int x, y;
};
struct Point p1 = {10, 20};
printf("%d", p1.x); // Output: 10Control Flow
Use if, else, switch for conditionals and for, while, do-while for loops.
int x = 10;
if (x > 5) {
printf("x is greater than 5");
}Error Handling
C uses error codes and errno for error handling. Always check return values of functions.
FILE* file = fopen("file.txt", "r");
if (file == NULL) {
perror("Error opening file");
}File I/O
Use FILE* and functions like fopen, fclose, fread, and fwrite for file operations.
FILE* file = fopen("file.txt", "w");
fprintf(file, "Hello, World!");
fclose(file);Libraries
C provides standard libraries like <stdio.h>, <stdlib.h>, and <string.h>. Use header files for custom libraries.
#include <stdio.h> #include "mylib.h"
Quirks and Tweaks
C has no boolean type (pre-C99), no namespaces, and weak const enforcement. Use void* for generics.
#define TRUE 1 #define FALSE 0
Common Pitfalls
Avoid uninitialized variables, memory leaks, buffer overflows, and dangling pointers.
int* ptr = (int*)malloc(sizeof(int));
if (ptr != NULL) {
*ptr = 10;
free(ptr); // Avoid memory leaks
}C Basics
Data Types
// 🔹 Basic Data Types
int num = 10; // Integer
float decimal = 10.5; // Floating point number
char letter = 'A'; // Character
double precise = 10.123456; // Double precision float
_Bool flag = 1; // Boolean (C99)
// 🔹 Void Type
void functionName(); // Function returning nothing
// 🔹 Derived Data Types
int arr[5]; // Array
struct Student { char name[50]; int age; }; // Structure
union Data { int x; float y; }; // Union
typedef unsigned int uint; // Typedef aliasOperators
// 🔹 Arithmetic Operators: +, -, *, /, %
int sum = a + b;
// 🔹 Relational Operators: ==, !=, >, <, >=, <=
if (x == y) { ... }
// 🔹 Logical Operators: &&, ||, !
if (a > 0 && b < 10) { ... }
// 🔹 Bitwise Operators: &, |, ^, ~, <<, >>
int result = a & b;
// 🔹 Assignment Operators: =, +=, -=, *=, /=
x += 10; // x = x + 10;
// 🔹 Ternary Operator
int min = (a < b) ? a : b;Input & Output
// 🔹 Standard Input & Output
printf("Enter a number: ");
scanf("%d", &num);
// 🔹 Character Input & Output
char ch = getchar();
putchar(ch);
// 🔹 String Input & Output
char name[50];
gets(name);
puts(name);Control Flow
Conditionals
// 🔹 If-Else Statement
if (score > 90) {
grade = 'A';
} else if (score > 75) {
grade = 'B';
} else {
grade = 'C';
}
// 🔹 Switch Case
switch(day) {
case 1: printf("Monday"); break;
case 2: printf("Tuesday"); break;
default: printf("Unknown"); break;
}Loops
// 🔹 For Loop
for (int i = 0; i < 5; i++) {
printf("%d ", i);
}
// 🔹 While Loop
int i = 0;
while (i < 5) {
printf("%d ", i);
i++;
}
// 🔹 Do-While Loop
int i = 0;
do {
printf("%d ", i);
i++;
} while (i < 5);Jump Statements
// 🔹 Break and Continue
for (int i = 0; i < 10; i++) {
if (i == 5) break; // Exit loop
if (i == 3) continue; // Skip iteration
printf("%d ", i);
}
// 🔹 Goto Statement
start:
printf("Hello ");
goto end;
printf("This won't be printed.");
end:
printf("World!");Functions
Function Basics
// 🔹 Function Declaration & Definition
void greet() {
printf("Hello, World!");
}
// 🔹 Function Call
greet();
// 🔹 Function with Parameters
int sum(int a, int b) {
return a + b;
}
// 🔹 Call by Value vs Call by Reference
void modify(int *ptr) {
*ptr = 100; // Modifies actual variable
}Recursion
// 🔹 Recursive Function Example
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}Arrays & Strings
Arrays
// 🔹 One-Dimensional Array
int arr[5] = {1, 2, 3, 4, 5};
// 🔹 Multi-Dimensional Array
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};Strings
// 🔹 String Basics
char str[] = "Hello";
printf("%s", str);
// 🔹 String Functions
strlen(str);
strcpy(dest, src);
strcmp(str1, str2);
strcat(str1, str2);Pointers
Pointer Basics
// 🔹 Pointer Declaration int *ptr; ptr = # // Stores address of num // 🔹 Dereferencing Pointer int value = *ptr; // Access value at pointer address // 🔹 Pointer Arithmetic ptr++; // Moves to next memory location
Dynamic Memory
// 🔹 Dynamic Memory Allocation int *ptr = (int*)malloc(sizeof(int)); free(ptr); // Free allocated memory
Structures & Unions
Structures
// 🔹 Defining a Structure
struct Student {
char name[50];
int age;
};
// 🔹 Accessing Structure Members
struct Student s1;
s1.age = 20;Unions
// 🔹 Defining a Union
union Data {
int x;
float y;
};
union Data d;
d.x = 10;File Handling
File Operations
// 🔹 File Handling Basics
FILE *fp;
fp = fopen("file.txt", "w");
fprintf(fp, "Hello, File!");
fclose(fp);Why Solo Dev?
This reference website is not for beginners
Solo Dev is strictly for devs who already know the game and need a beautiful cheat sheet on their second monitor which looks better than GeeksforGeeks
this portion goes away when you shrink the screen ;)
