In other languages regulations are often very strict. For example: Don't access array elements with indices exceeding the range. But what if I just WANT to do this? If I decide to do it, the computer has to obey. I hate languages that complain "No. No. Don't do this. Bad things may happen...". C doesn't complain. A C compiler knows that you are intelligent and have good reasons whenever you break the rules of old-fashioned programming style.
Due to the range of possibilities that you have in C solutions of problems tend
to be very short.
Imagine you want to print the sequence of the integers
from a to b.
If a<b then the sequence is to be printed in ascending order and if a>b
then it is to be printed in descending order.
A solution in a Pascal-style language could look like this:
IF a<b THEN FOR i:=a TO b DO writeln(i) ELSE FOR i:=a DOWNTO b DO writeln(i);
but in C it can be simply written as
for(i=a;printf("%d\n",i),d=b-i;i+=(d|1)%2);
The C notation may confuse you but this is because you still think in a
human language. Try to think like a computer!
1. You initialize i with i=a. 2. Print i. This can't be wrong. 3. Compute the difference to your goal: d=b-i C now interprets the result as a condition. If d is zero, then the for-loop is exited. 4. Now notice that the expression (d|1)%2 yields +1 if d is greater than zero and -1 if d is less than zero. So add (d|1)%2 to your current i for the next iteration.It's straight-forward, isn't it?
November 30, 1995
Mark Dettinger
Here's a comment by
Leigh Lundin
from
Infinite Space Systems.
P.S. :
Don't take it too serious. In reality I wouldn't write code like this.
In the ACM Southwestern Programming Contest 1996 me and my team used C.
To avoid bugs we tried to keep the code as pretty as possible and we
compiled our programs with gcc -ansi -Wall -pedantic to get every
warning message you can think of. As the
ranklist
shows you, this strategy works.