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
- 01introHello, World!printf("Hello, World!\n");
- 02introDeclare and print variablesDeclare an int and a float, print them.
- 03introPrint a characterDeclare a char and print it with %c.
- 04introArithmetic operatorsUse +, -, *, /, %.
- 05easyRead and addRead two ints with scanf and print the sum.
- 06easyif / else if / elseClassify a number's sign.
- 07easyswitch statementTranslate 1..3 to one/two/three.
- 08easyFor loop 1..10Print numbers 1 through 10.
- 09easyWhile loopPrint 5..1 with while.
- 10easysquare() functionDefine int square(int n) and call it.
- 11easymax(a, b) functionDefine int max(int a, int b).
- 12mediumSum an arraySum the elements of an int array of size 5.
- 13mediumFind array maxFind the largest element of an array.
- 14mediumReverse an arrayReverse an array in place.
- 15mediumPointer basicsCreate a pointer, modify the value through it.
- 16mediumswap() with pointersDefine void swap(int *a, int *b).
- 17mediumString lengthCompute strlen of a literal manually.
- 18mediumUse <string.h>Use strlen and strcmp.
- 19hardRecursive factorialDefine a recursive factorial(n).
- 20hardIterative fibonacciPrint fibonacci(20) iteratively.
- 21hardDefine a structMake a struct Point with x, y and use it.
- 22hardtypedef a structUse typedef to alias struct Point.
- 23mediumDefine an enumUse an enum for colors.
- 24hardmalloc and freeAllocate an int array of size N and free it.
- 25hardLinked list nodeDefine a self-referential struct Node.
- 26hardFunction pointerUse a function pointer.
- 27mediumBitwise operatorsUse & | ^ ~ << >>.
- 28easydo/while loopRun a body at least once.
- 29easyTernary operatorUse ?: in printf.
- 30hardReverse a C stringReverse a char array in place.
- 31medium#define macroDefine a SQUARE(x) macro.
- 32easyTemperature tablePrint Fahrenheit→Celsius table 0..100 step 20.
- 33easyCount charactersCount input chars with getchar.
- 34mediumWord counter (state machine)Count words via IN/OUT state.
- 35mediumFunction: power(base, n)Define int power(int base, int n).
- 36mediumCount 1-bitsImplement bitcount(unsigned).
- 37mediumImplement itoaConvert int to string.
- 38mediumsqueeze(s, c)Remove all char c from string.
- 39mediumImplement strcat (pointers)void mystrcat(char *s, char *t).
- 40mediumPointer strlenImplement strlen with pointers.
- 41mediumswap(int *, int *)Classic pointer swap.
- 42mediumBubble sortSort int array ascending.
- 43hardBinary searchImplement binsearch.
- 44hardqsort with comparatorUse stdlib qsort.
- 45hardstruct Point + functionDefine struct & makepoint.
- 46hardtypedef a structHide the struct keyword.
- 47hardLinked list nodeSelf-referential struct + push.
- 48hardFunction pointerPass an int(*)(int) to a function.
- 49hardstatic local counterPersistent local with static.
- 50hardBitwise flagsSet/clear/test a flag bit.
- 51hardfopen + fgetsRead first line of a file.
- 52hardDynamic int arrayAllocate, fill, free.
- 53hardCommand-line argsPrint argc and each argv[i].
- 54mediumenum for daysDefine enum day { MON, TUE, ... };
- 55hardconst char * vs char * constPointer to const char.
- 56hardunion basicsDefine a union of int and float.
- 57hard2D array sumSum all elements of int m[3][3].
- 58easysizeof: how big is each type?Print sizeof(char), sizeof(int) and sizeof(double).
- 59easyPrint 255 in hex and octalUse %x and %o format specifiers.
- 60easyBit manipulation: set a bitSet bit n in x using x |= (1u << n).
- 61mediumBit manipulation: clear a bitClear bit n with x &= ~(1u << n).
- 62mediumBit manipulation: toggle a bitToggle bit n with XOR.
- 63mediumBit manipulation: read a bitTest bit n: (x >> n) & 1.
- 64mediumDetect endianness at runtimeUse a union of int and char[4] to find byte order.
- 65mediumMemory-mapped registerDeclare a volatile pointer to address 0x40000000.
- 66mediumGPIO blink patternToggle pin 5 of a GPIO output register.
- 67mediumQ15 fixed-point multiplyMultiply two Q15 fixed-point numbers.
- 68mediumVolatile ISR flagDeclare a volatile flag set by an ISR, polled by main.
- 69hardReimplement memcpyWrite your own byte-by-byte memcpy.
- 70hardReimplement memsetFill n bytes of a buffer with a value.
- 71hardBounded string copyWrite a safe my_strncpy.
- 72hardTiny state machineSwitch-based FSM with enum states.
- 73hardFixed-size stackImplement push/pop on a static array.
- 74hardCircular byte queue (ring buffer)Build a power-of-two ring buffer for UART RX.
- 75hardCRC-8 over a bufferCompute CRC-8 with polynomial 0x07.
- 76hardPWM compare value from duty cycleCompute the timer compare register from a duty %.
- 77hardSoftware debounce counterStable-read a noisy button with N consecutive samples.
- 78hardCapstone: sensor sampling pipelineRead N samples, store in a ring buffer, output a moving average.
