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 types
int x = 10;
float f = 3.14;
char c = 'A';
double d = 3.14159;

Control Flow

If-elseConditional execution
if (x > 0) {
    printf("positive");
} else {
    printf("not positive");
}
For loopIteration
for (int i = 0; i < 10; i++) {
    printf("%d\n", i);
}
While loopCondition-based loop
while (x > 0) {
    x--;
}
SwitchMulti-way branch
switch (n) {
    case 1: break;
    default: break;
}

Functions & Arrays

FunctionFunction definition
int add(int a, int b) {
    return a + b;
}
ArrayFixed-size collection
int arr[5] = {1, 2, 3, 4, 5};
arr[0] = 10;
StringsCharacter arrays
char str[] = "hello";
printf("%s", str);
#include <string.h>

Pointers

Pointer declarationVariable that holds an address
int *ptr;
int x = 10;
ptr = &x;
DereferenceAccess value at address
printf("%d", *ptr);
mallocDynamic memory allocation
int *arr = (int*)malloc(5 * sizeof(int));
free(arr);