All Cheat Sheets
C
C Cheat Sheet
Basics
Hello WorldBasic C program structure#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}CommentsSingle and multi-line// Single line
/* Multi-line
comment */Data typesBasic primitive typesint x = 10;
float f = 3.14;
char c = 'A';
double d = 3.14159;Control Flow
If-elseConditional executionif (x > 0) {
printf("positive");
} else {
printf("not positive");
}For loopIterationfor (int i = 0; i < 10; i++) {
printf("%d\n", i);
}While loopCondition-based loopwhile (x > 0) {
x--;
}SwitchMulti-way branchswitch (n) {
case 1: break;
default: break;
}Functions & Arrays
FunctionFunction definitionint add(int a, int b) {
return a + b;
}ArrayFixed-size collectionint arr[5] = {1, 2, 3, 4, 5};
arr[0] = 10;StringsCharacter arrayschar str[] = "hello";
printf("%s", str);
#include <string.h>Pointers
Pointer declarationVariable that holds an addressint *ptr;
int x = 10;
ptr = &x;DereferenceAccess value at addressprintf("%d", *ptr);mallocDynamic memory allocationint *arr = (int*)malloc(5 * sizeof(int));
free(arr);