$ cd ../ (all courses)

C

The portable assembly language. Foundation of modern computing.

⚠ pattern-based checks (code is analyzed, not compiled)

About C

C is a low-level, statically typed language created in 1972. Operating systems, databases, languages — most of what you use is built on it. NOTE: In this course, your code is checked by pattern matching, not compiled. Focus on writing correct, idiomatic C.

Quick-reference cheat sheet
// C cheat-sheet
#include <stdio.h>      // for printf, scanf
#include <string.h>     // for strlen, strcmp
#include <stdlib.h>     // for malloc, free

int main(void) {
    int x = 42;
    float pi = 3.14f;
    char c = 'A';
    char name[] = "ada";

    printf("Hello, %s!\n", name);   // %d int, %f float, %s string, %c char
    printf("x = %d\n", x);

    // Conditions
    if (x > 0) printf("positive\n");
    else if (x == 0) printf("zero\n");
    else printf("negative\n");

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

    int n;
    scanf("%d", &n);     // read an int (note the &)

    return 0;
}

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

// Arrays
int nums[5] = {1, 2, 3, 4, 5};

// Pointers
int v = 10;
int *p = &v;     // p holds address of v
*p = 20;         // dereference: now v == 20

// Dynamic allocation
int *arr = (int*) malloc(10 * sizeof(int));
free(arr);

Tasks

  1. 01
    Hello, World!
    printf("Hello, World!\n");
    intro
  2. 02
    Declare and print variables
    Declare an int and a float, print them.
    intro
  3. 03
    Print a character
    Declare a char and print it with %c.
    intro
  4. 04
    Arithmetic operators
    Use +, -, *, /, %.
    intro
  5. 05
    Read and add
    Read two ints with scanf and print the sum.
    easy
  6. 06
    if / else if / else
    Classify a number's sign.
    easy
  7. 07
    switch statement
    Translate 1..3 to one/two/three.
    easy
  8. 08
    For loop 1..10
    Print numbers 1 through 10.
    easy
  9. 09
    While loop
    Print 5..1 with while.
    easy
  10. 10
    square() function
    Define int square(int n) and call it.
    easy
  11. 11
    max(a, b) function
    Define int max(int a, int b).
    easy
  12. 12
    Sum an array
    Sum the elements of an int array of size 5.
    medium
  13. 13
    Find array max
    Find the largest element of an array.
    medium
  14. 14
    Reverse an array
    Reverse an array in place.
    medium
  15. 15
    Pointer basics
    Create a pointer, modify the value through it.
    medium
  16. 16
    swap() with pointers
    Define void swap(int *a, int *b).
    medium
  17. 17
    String length
    Compute strlen of a literal manually.
    medium
  18. 18
    Use <string.h>
    Use strlen and strcmp.
    medium
  19. 19
    Recursive factorial
    Define a recursive factorial(n).
    hard
  20. 20
    Iterative fibonacci
    Print fibonacci(20) iteratively.
    hard
  21. 21
    Define a struct
    Make a struct Point with x, y and use it.
    hard
  22. 22
    typedef a struct
    Use typedef to alias struct Point.
    hard
  23. 23
    Define an enum
    Use an enum for colors.
    medium
  24. 24
    malloc and free
    Allocate an int array of size N and free it.
    hard
  25. 25
    Linked list node
    Define a self-referential struct Node.
    hard
  26. 26
    Function pointer
    Use a function pointer.
    hard
  27. 27
    Bitwise operators
    Use & | ^ ~ << >>.
    medium
  28. 28
    do/while loop
    Run a body at least once.
    easy
  29. 29
    Ternary operator
    Use ?: in printf.
    easy
  30. 30
    Reverse a C string
    Reverse a char array in place.
    hard
  31. 31
    #define macro
    Define a SQUARE(x) macro.
    medium
  32. 32
    Temperature table
    Print Fahrenheit→Celsius table 0..100 step 20.
    easy
  33. 33
    Count characters
    Count input chars with getchar.
    easy
  34. 34
    Word counter (state machine)
    Count words via IN/OUT state.
    medium
  35. 35
    Function: power(base, n)
    Define int power(int base, int n).
    medium
  36. 36
    Count 1-bits
    Implement bitcount(unsigned).
    medium
  37. 37
    Implement itoa
    Convert int to string.
    medium
  38. 38
    squeeze(s, c)
    Remove all char c from string.
    medium
  39. 39
    Implement strcat (pointers)
    void mystrcat(char *s, char *t).
    medium
  40. 40
    Pointer strlen
    Implement strlen with pointers.
    medium
  41. 41
    swap(int *, int *)
    Classic pointer swap.
    medium
  42. 42
    Bubble sort
    Sort int array ascending.
    medium
  43. 43
    Binary search
    Implement binsearch.
    hard
  44. 44
    qsort with comparator
    Use stdlib qsort.
    hard
  45. 45
    struct Point + function
    Define struct & makepoint.
    hard
  46. 46
    typedef a struct
    Hide the struct keyword.
    hard
  47. 47
    Linked list node
    Self-referential struct + push.
    hard
  48. 48
    Function pointer
    Pass an int(*)(int) to a function.
    hard
  49. 49
    static local counter
    Persistent local with static.
    hard
  50. 50
    Bitwise flags
    Set/clear/test a flag bit.
    hard
  51. 51
    fopen + fgets
    Read first line of a file.
    hard
  52. 52
    Dynamic int array
    Allocate, fill, free.
    hard
  53. 53
    Command-line args
    Print argc and each argv[i].
    hard
  54. 54
    enum for days
    Define enum day { MON, TUE, ... };
    medium
  55. 55
    const char * vs char * const
    Pointer to const char.
    hard
  56. 56
    union basics
    Define a union of int and float.
    hard
  57. 57
    2D array sum
    Sum all elements of int m[3][3].
    hard
  58. 58
    sizeof: how big is each type?
    Print sizeof(char), sizeof(int) and sizeof(double).
    easy
  59. 59
    Print 255 in hex and octal
    Use %x and %o format specifiers.
    easy
  60. 60
    Bit manipulation: set a bit
    Set bit n in x using x |= (1u << n).
    easy
  61. 61
    Bit manipulation: clear a bit
    Clear bit n with x &= ~(1u << n).
    medium
  62. 62
    Bit manipulation: toggle a bit
    Toggle bit n with XOR.
    medium
  63. 63
    Bit manipulation: read a bit
    Test bit n: (x >> n) & 1.
    medium
  64. 64
    Detect endianness at runtime
    Use a union of int and char[4] to find byte order.
    medium
  65. 65
    Memory-mapped register
    Declare a volatile pointer to address 0x40000000.
    medium
  66. 66
    GPIO blink pattern
    Toggle pin 5 of a GPIO output register.
    medium
  67. 67
    Q15 fixed-point multiply
    Multiply two Q15 fixed-point numbers.
    medium
  68. 68
    Volatile ISR flag
    Declare a volatile flag set by an ISR, polled by main.
    medium
  69. 69
    Reimplement memcpy
    Write your own byte-by-byte memcpy.
    hard
  70. 70
    Reimplement memset
    Fill n bytes of a buffer with a value.
    hard
  71. 71
    Bounded string copy
    Write a safe my_strncpy.
    hard
  72. 72
    Tiny state machine
    Switch-based FSM with enum states.
    hard
  73. 73
    Fixed-size stack
    Implement push/pop on a static array.
    hard
  74. 74
    Circular byte queue (ring buffer)
    Build a power-of-two ring buffer for UART RX.
    hard
  75. 75
    CRC-8 over a buffer
    Compute CRC-8 with polynomial 0x07.
    hard
  76. 76
    PWM compare value from duty cycle
    Compute the timer compare register from a duty %.
    hard
  77. 77
    Software debounce counter
    Stable-read a noisy button with N consecutive samples.
    hard
  78. 78
    Capstone: sensor sampling pipeline
    Read N samples, store in a ring buffer, output a moving average.
    hard