#include <stdio.h>
#include <stdint.h>
// Perform linear search in array
int linear_search(uint8_t *arr, uint8_t n, uint8_t key) {
for (uint8_t i = 0; i < n; i++) {
if (arr[i] == key) {
return i; // Found key, return index
}
}
return -1; // Not found
}
int main() {
uint8_t n, key;
scanf("%hhu", &n);
uint8_t arr[100];
for (uint8_t i = 0; i < n; i++) {
scanf("%hhu", &arr[i]);
}
scanf("%hhu", &key);
int index = linear_search(arr, n, key);
printf("%d", index);
return 0;
}
What’s the goal?
Scan the array one-by-one to find the first matching value of the key. Return its index.
Why it matters in firmware?
Used to search device IDs, sensor lookup tables, ADC conversion zones
Faster to implement in small tables without extra memory
Avoids binary search when sorting is not guaranteed