See this page as a slide show
IOCCC One Liner
Step 1
The original code.
- OK, really, it’s not the original code.
- The original code no longer compiles. ☹
Step 2
Add some formatting, and we get:
Note the old-style function header.
Step 3
Add #include
Step 4
- convert
main
arguments to new style
- Explicitly declare
main
’s return type
Step 5
Give variables traditional names
Step 6
argv
is an array, so it’s always non-zero,
so !argv==0
, and !!argv==1
.
Make this substitution:
Step 7
a[b]
is the same as *(a+b)
is the same as *(b+a)
is the same as b[a]
.
Step 8
- Replace
*argv
with argv[0]
- Replace
**argv
with argv[0][0]
Step 9
Replace !!
<true quantity> with 1
Step 10
Convert to a while
loop
Step 11
Pull argv[0][1]
out of the while
loop
Step 12
Assume that if execlp
is ever called, it won’t return,
so pull it out of the loop.
Step 13
Since --argv
is always true, make it a separate statement.
Step 14
The write
call gets a length of zero after the first time,
so the loop is a red herring. Nobody ever looks at argc
.
Step 15
Remove ++
and --
, adjust the values of argv
.
Step 16
Don’t have to zero out argv[1][0]
any more.
Step 17
The trailing paren is just to confuse % in vi, so remove it
and adjust the check.
Step 18
The argument string is only needed if no arguments are given.
Step 19
Make a proper sentinel value
Step 20
main
returns an int
.
Step 21
Add comments
Why not the original code?
Original code:
for (v[c++]="Hello, world!\n)";
New code:
char msg[]="Hello, world!\n)";
for (v[c++]=msg;
- The original code had
argv
pointing to a string constant.
- The new code has
argv
pointing to a string variable.
- What’s the difference?