file-handling · beginner · ~10 min
Recognise BOM byte signatures with correct ordering.
#include <stdint.h>
#include <stddef.h>
int detect_bom(const uint8_t *buf, size_t n);
Return: 1 UTF-8 (EF BB BF), 2 UTF-16LE (FF FE), 3 UTF-16BE (FE FF),
or 0 for no BOM (including NULL or too-short input).
A leading BOM silently corrupts parsing if you don't skip it; detecting one is the first step in robust text loading.
#include <stdint.h>
#include <stddef.h>
int detect_bom(const uint8_t *buf, size_t n) {
/* TODO */
(void)buf; (void)n;
return 0;
}
Swapping LE/BE. Checking the 2-byte BOM before the 3-byte one. Reading past a short buffer.
No BOM. Only one byte present. NULL.
O(1).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.