Basics

Hello World & Structure
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

// printf format specifiers
// %d  int        %f  float
// %c  char       %s  string
// %ld long       %p  pointer
// %x  hex        %o  octal
// %.2f 2 decimal places

printf("Name: %s, Age: %d, GPA: %.2f\n", "Alice", 25, 3.85);
Input
int age;
float price;
char name[50];

scanf("%d", &age);          // read int
scanf("%f", &price);        // read float
scanf("%s", name);          // read word
scanf("%49s", name);        // safe read
fgets(name, 50, stdin);     // read line (safer)

// After scanf, clear buffer
while (getchar() != '\n');  // flush newline

Data Types

Types & Constants
char    c = 'A';     // 1 byte  -128 to 127
int     i = 42;      // usually 4 bytes
short   s = 100;     // 2 bytes
long    l = 123456L; // 4-8 bytes
float   f = 3.14f;   // 4 bytes (6-7 digits)
double  d = 3.14;    // 8 bytes (15-16 digits)

unsigned int ui = 4294967295; // no negatives

// Constants
#define PI 3.14159
#define MAX 100
const int SIZE = 50;

// sizeof
sizeof(int)    // 4
sizeof(double) // 8
sizeof(char)   // 1
Operators
// Arithmetic
+ - * /  %   (modulo)
++x  x++  --x  x--

// Comparison
==  !=  >  >=  <  <=

// Logical
&&  ||  !

// Bitwise
&  |  ^  ~  <<  >>

// Assignment
=  +=  -=  *=  /=  %=  &=  |=  ^=

// Ternary
int max = (a > b) ? a : b;

// Type cast
float f = (float)5 / 2;  // 2.5

Control Flow

if / switch / loops
// if-else
if (x > 0) {
    printf("positive");
} else if (x < 0) {
    printf("negative");
} else {
    printf("zero");
}

// switch
switch (grade) {
    case 'A': printf("Excellent"); break;
    case 'B': printf("Good"); break;
    default:  printf("Other");
}

// for
for (int i = 0; i < 5; i++) printf("%d\n", i);

// while
int i = 0;
while (i < 5) { printf("%d\n", i); i++; }

// do-while
do { printf("%d\n", i); i++; } while (i < 5);

// break / continue
for (int i = 0; i < 10; i++) {
    if (i == 5) break;
    if (i % 2 == 0) continue;
    printf("%d\n", i);
}

Functions

Function Syntax
// Function prototype (declaration)
int add(int a, int b);
void greet(char name[]);

// Function definition
int add(int a, int b) {
    return a + b;
}

void greet(char name[]) {
    printf("Hello, %s!\n", name);
}

// Recursive function
int factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}

// Pass by reference using pointers
void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}
// Call: swap(&x, &y);

Arrays & Strings

Arrays
// 1D array
int nums[5] = {1, 2, 3, 4, 5};
int zeros[10] = {0};   // all zeros
nums[0] = 10;

int len = sizeof(nums) / sizeof(nums[0]); // 5

// 2D array
int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
matrix[1][2]  // 6

// Pass array to function
void printArr(int arr[], int size) {
    for (int i = 0; i < size; i++)
        printf("%d ", arr[i]);
}
Strings (char arrays)
#include <string.h>

char name[50] = "Alice";
char greeting[] = "Hello";

strlen(name)           // 5 (no null terminator)
strcpy(dest, src)      // copy string
strncpy(dest, src, n)  // safer copy
strcat(dest, src)      // concatenate
strncat(dest, src, n)  // safer concat
strcmp(s1, s2)         // 0 if equal
strncmp(s1, s2, n)
strchr(s, 'A')         // pointer to first 'A'
strstr(s, "ell")       // pointer to substring

// Char functions (ctype.h)
#include <ctype.h>
isalpha(c); isdigit(c); isspace(c);
toupper(c); tolower(c);

Pointers

Pointer Basics
int x = 10;
int *p = &x;    // pointer to x

printf("%d\n", x);   // value: 10
printf("%p\n", p);   // address of x
printf("%d\n", *p);  // dereference: 10

*p = 20;         // change x via pointer
// x is now 20

// Pointer arithmetic
int arr[] = {10, 20, 30};
int *ptr = arr;
printf("%d\n", *ptr);     // 10
printf("%d\n", *(ptr+1)); // 20
ptr++;
printf("%d\n", *ptr);     // 20

// NULL pointer
int *null_ptr = NULL;
if (null_ptr == NULL)
    printf("Null pointer\n");

Structs

Struct Syntax
// Define struct
struct Person {
    char name[50];
    int age;
    float gpa;
};

// typedef for convenience
typedef struct {
    char name[50];
    int age;
} Student;

// Create and use
struct Person p1 = {"Alice", 25, 3.9};
Student s1 = {"Bob", 20};

printf("%s is %d\n", p1.name, p1.age);
p1.age = 26;

// Pointer to struct
struct Person *ptr = &p1;
printf("%s\n", ptr->name);  // arrow operator

File I/O

Read & Write Files
#include <stdio.h>

// Write to file
FILE *fp = fopen("out.txt", "w");
if (fp == NULL) { perror("Error"); return 1; }
fprintf(fp, "Hello, World!\n");
fputs("Another line\n", fp);
fclose(fp);

// Read from file
FILE *fr = fopen("out.txt", "r");
char line[256];
while (fgets(line, sizeof(line), fr) != NULL) {
    printf("%s", line);
}
fclose(fr);

// File modes
// "r"  read      "w"  write (overwrite)
// "a"  append    "r+" read+write
// "rb" read binary  "wb" write binary

Dynamic Memory

malloc / calloc / free
#include <stdlib.h>

// malloc — allocate uninitialized
int *arr = (int*) malloc(5 * sizeof(int));
if (arr == NULL) { perror("malloc failed"); exit(1); }
arr[0] = 10; arr[1] = 20;

// calloc — allocate & zero-initialize
int *arr2 = (int*) calloc(5, sizeof(int));

// realloc — resize allocation
arr = (int*) realloc(arr, 10 * sizeof(int));

// free — ALWAYS free when done
free(arr);
free(arr2);
arr = NULL;  // avoid dangling pointer

// Dynamic struct
Student *s = (Student*) malloc(sizeof(Student));
strcpy(s->name, "Alice");
s->age = 20;
free(s);