Simvik

Compare firmware versions

Order two semantic versions for an update check. An easy embedded C problem (Structs, unions and memory layout), run and graded in the browser.

easy · Structs, unions and memory layout · structs · comparison

A bootloader must decide whether an image is newer than the one installed.

Return -1 if version a is older than b, 1 if it is newer and 0 if they are the same. Compare major first, then minor, then patch.

Starter code

int version_compare(version_t a, version_t b) {
  // -1 if a < b, 0 if equal, 1 if a > b
  return 0;
}

Example tests

Older minor version

printf("%d\n", version_compare((version_t){ 1, 2, 3 }, (version_t){ 1, 3, 0 }));

Expected output:

-1

Same version

printf("%d\n", version_compare((version_t){ 4, 0, 12 }, (version_t){ 4, 0, 12 }));

Expected output:

0

Learn it first

More Structs, unions and memory layout problems