Most Useful

Dave Burton ioccc@snox.net
http://snox.net/ioccc

Judges' comments:

To use:

make

./prog "expression"

or

./prog
(input expressions)

Try:

./prog '?0x3343<<2'

echo '?0x3343<<2' | ./prog

./prog

Enter:

a=0xfeedface
b=0xcafe
a << 16 + b

grep '[0-9]' prog.c

./prog < test.in

less calc.man

Selected Judges Remarks:

“Look, Ma, no hands digits!”

The place where some arithmetic operations are done, for example, division or modulo, are easy to find. Where are addition and subtraction?

We were impressed with the level of detail the author “blogged” the obfuscation process.

Author’s comments:

calc - an integer expression calculator that outputs in both hex and decimal

All useful tools should include sufficient documentation. See the included man page. ;-)

Notes

This program is named calc. Onomatopoeia of sorts, in the visual realm. The formatting was easier: Form Follows Function (although Hou did this better). It was tempting to call it iCalc, with i for integer (and it was written on an iDevice), but sanity prevailed.

Printed in 8 pt Courier, it makes aesthetically pleasing 8.5 x 11 wall art.

prog.c vs prog.orig.c

Dominik Muth observed a syntax error on 1+1, 1-1, 1|1, and 1^1 when using an ARM based computer, although 1*1 and others worked correctly. This occurs because some platforms by default treat char as unsigned char. The original code has this weakness, which can be corrected by adding -fsigned-char to the compile line. The modified code is agnostic to char signed-ness.

Why is this entry obfuscated/interesting?

Yes, this is another calculator program.

However, unlike any other calculator source code the author is aware of, this one contains no digits. At all. Anywhere. As in

grep '[0-9]' prog.c || echo no digits!

Nor are the digits simply obfuscated. There are no character constants. There is only one single, short string, and it does not contain any obfuscated digits. And yet the output is in decimal and hexadecimal, and it accepts octal, decimal, and hex input.

Flow control is by for and return. It is If-less. Switch-less. While-less. Hope-less.
Literally, if not figuratively. ;-)

The code size is intentionally 2015, as stated by iocccsize -i, and can be reformatted without changes (as in rule2.c) to exactly 4096 octets.

Finally, calc is interesting, if for no other reason than it is practically useful: Hex/decimal conversion, arithmetic, byte-swapping, bit-shifting, and logical operations with memory, on the command line, or interactively.

But the question is why is the entry obfuscated?

The code follows many industry best-practices:

Compiler warnings

Calc is C99 compliant, and is ANSI/C90 compliant except for wanting 64-bit long long. Changing the two long long typedefs to long, and compiling with gcc -m32 yields a program that operates on 32-bit values, with wrap-around of shifts mod 32:

1<<48
65536   0x00010000

gcc 4.2.1 without arguments compiles cleanly (e.g. cc prog.c -o prog). This is my workaday compiler. gcc is also clean with -ansi, and -std=c99.

clang 3.6.0 lives up to its namesake, resounding on non-problems. To stifle these complaints: -Wno-return-type -Wno-parentheses -Wno-empty-body

When invoked with -ansi -pedantic, there are two warnings, which can be ignored:

With -Wall -std=c99, both are quite noisy, and all can be ignored:

Test Suite

If the program name begins with an “e”, it echoes stdin to stdout. This allows for a convenient test suite:

ln -s prog eprog
PATH=. eprog < test.in | diff - test.out

Spoilers

If you do not want to puzzle out how it works, see spoilers.markdown.


Creative Commons License

© Copyright 1984-2016, Leo Broukhis, Simon Cooper, Landon Curt Noll - All rights reserved
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.