Skip to content

Instantly share code, notes, and snippets.

# Nexuapex/7segment.c Created Oct 29, 2011

7-segment display in 50 lines of C99.
 #include int const schematic[7][7] = { {0,1,1,1,1,0,0}, {2,0,0,0,0,3,0}, {2,0,0,0,0,3,0}, {0,4,4,4,4,0,0}, {5,0,0,0,0,6,0}, {5,0,0,0,0,6,0}, {0,7,7,7,7,0,0}, }; #define seg(i) (1<<(i)) int const digits[10] = { /* 0 */ seg(1) | seg(2) | seg(3) | seg(5) | seg(6) | seg(7), /* 1 */ seg(3) | seg(6), /* 2 */ seg(1) | seg(3) | seg(4) | seg(5) | seg(7), /* 3 */ seg(1) | seg(3) | seg(4) | seg(6) | seg(7), /* 4 */ seg(2) | seg(3) | seg(4) | seg(6), /* 5 */ seg(1) | seg(2) | seg(4) | seg(6) | seg(7), /* 6 */ seg(1) | seg(2) | seg(4) | seg(5) | seg(6) | seg(7), /* 7 */ seg(1) | seg(3) | seg(6), /* 8 */ seg(1) | seg(2) | seg(3) | seg(4) | seg(5) | seg(6) | seg(7), /* 9 */ seg(1) | seg(2) | seg(3) | seg(4) | seg(6) }; void segmentize_print(int digit, int row) { if (digit >= 0 && digit <= 10) for (int col = 0; col < 7; ++col) putchar((seg(schematic[row][col]) & digits[digit]) ? 'X' : ' '); } void segmentize(char const* number) { for (int row = 0; row < 7; ++row) { char const* it = number; while (*it) segmentize_print(*it++ - '0', row); putchar('\n'); } } int main(int argc, char const* argv[]) { if (argc > 1) segmentize(argv[1]); return 0; }
to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.