obfuscated c programming contest

K

K4 Monk

I was looking at some of the entries and I have seen the light. All I
want to know is how to get to that light. How many of you here can
read and understand the OCC entries without blinking? I laughed at
some of them, but cannot figure out how the programmers came up with
their ideas. Is C dying out among college graduates? Because in a
class of over 150 students I couldn't find one who programs in C these
days.

Blah blah aside, I'm interested in knowing where this hidden knowledge
comes from so I could harness it as well. How do I learn C of such
dignified level which gives us the choice to program badly if we like.

What is the timeline of a good C programmer? How much time, daily
practice etc. will it take to become this good?
 
L

luser- -droog

K4 said:
I was looking at some of the entries and I have seen the light. All I
want to know is how to get to that light. How many of you here can
read and understand the OCC entries without blinking?

Most of them give me trouble, but I keep going back for more.
I think it's the same seduction that any code or cypher gives.
If someone went to such obvious effort to construct such a thing,
there must be some "meaning" behind it. It's like caballistic
gematria,
but you can actually check your answers!
I laughed at
some of them, but cannot figure out how the programmers came up with
their ideas. Is C dying out among college graduates? Because in a
class of over 150 students I couldn't find one who programs in C these
days.

That's a rather small sample set from which to draw any conclusion.
Blah blah aside, I'm interested in knowing where this hidden knowledge
comes from so I could harness it as well. How do I learn C of such
dignified level which gives us the choice to program badly if we like.

It seems to come partly from the mysterious source of creativity
itself,
and partly from the "culture". For the first, you have to have Mozart
in the
womb; for the second, I'm having some success by reading all the
classic
books by the authorities in question. Anything and everything by or
about
Thompson, Ritchie, McIllroy, Pike, and then sample from all the other
"hubs" of the time: Xerox PARC, Evans & Sutherland, um, IBM.

To create the best obfuscated stuff, I think you need the same skills
you develop by writing as clearly as possible. Because you, the
author,
need to be able to split your attention between "what it looks like"
and
"what's really going on".
What is the timeline of a good C programmer? How much time, daily
practice etc. will it take to become this good?

One way to investigate this would be to see how long it took the
original team to get "good" at C. http://www.tuhs.org/ has some old,
old source code that's freickin' weird!
 
H

Hans Vlems

Blah blah aside, I'm interested in knowing where this hidden knowledge
comes from so I could harness it as well. How do I learn C of such
dignified level which gives us the choice to program badly if we like.

What is the timeline of a good C programmer? How much time, daily
practice etc. will it take to become this good?

Once you've seen what APL programmers can do in one line you'll
realize that attempts in any other language are futile....
I understand what you're aiming for. It's an attitude that kept my
profession (I'm a chemist) in the dark ages for centuries. Its
practitioners wearing pointy eads, mumbling odd latin phrases and
trying to make gold.
Obfuscated programs are nice riddles, possibly a good testcase for a
compiler but never confuse them with good programming.
Hans
 
K

K4 Monk

Once you've seen what APL programmers can do in one line you'll
realize that attempts in any other language are futile....
I understand what you're aiming for. It's an attitude that kept my
profession (I'm a chemist) in the dark ages for centuries. Its
practitioners wearing pointy eads, mumbling odd latin phrases and
trying to make gold.
Obfuscated programs are nice riddles, possibly a good testcase for a
compiler but never confuse them with good programming.
Hans

I didn't mean to say that I'm interested in writing hard to decipher
code, but that I'd like to know how to become as good as possible.
Anyone who has read K&R can come up with "good" readable "C" code
which doesn't push the limits. Its because the K&R book is not about
testing the boundaries of C. It is an overview and introduction to the
language.

I'd say my OP had a metaphysical question. When I looked at the OCC
entries I realized that being able to do crazy stuff with C requires
you to know what works and what doesn't work. I'm interested in
finding out

1. What is it that I don't know which OCC participants know about C?
2. How do I go about knowing what I don't know?
3. What is it that neither I nor the OCC participants can know or take
for granted about C?

etc.
 
H

helmuterik

I didn't mean to say that I'm interested in writing hard to decipher
code, but that I'd like to know how to become as good as possible.
Anyone who has read K&R can come up with "good" readable "C" code
which doesn't push the limits.

I disagree. You have to practice a lot to get good. Reading will get
you, at most, a shallow passive type knowledge.
Peter Norvig has an essay entitled "Learn a programming language in 10
years" that I suggest you read. I get the feeling from your posts ( I
saw that you were also asking for good C++ books in that group) that
you are seeking some sort of miraculous shortcut to proficiency. There
is none (as Norvig's text tells you).
 
T

Thad Smith

I didn't mean to say that I'm interested in writing hard to decipher
code, but that I'd like to know how to become as good as possible.
Anyone who has read K&R can come up with "good" readable "C" code
which doesn't push the limits. Its because the K&R book is not about
testing the boundaries of C. It is an overview and introduction to the
language.

I'd say my OP had a metaphysical question. When I looked at the OCC
entries I realized that being able to do crazy stuff with C requires
you to know what works and what doesn't work. I'm interested in
finding out

I haven't looked at OCCC entries in a long time, but my recollection is that
several of the older entries worked for a particular compiler and not all
Standard C compilers. Stretching the bounds of a single compiler is different
than doing unusual things within the confines of Standard C.
1. What is it that I don't know which OCC participants know about C?

You can analyze OCCC entries and determine how they work. I think good OCCC
entries require knowledge of C and creative inspiration.
2. How do I go about knowing what I don't know?

For Standard C, there are several sources, such as the comp.lang.c FAQ, the
discussions here, and, of course, the standard itself. In general, you can learn
1) the C language itself
2) useful techniques that people have developed that aren't obvious from the
language definition, such as Duff's device.
3. What is it that neither I nor the OCC participants can know or take
for granted about C?

Read the standard and discussions in comp.std.c.

What value is printed by the following code?

#include <stdio.h>
unsigned char x;
int main(void) {
x = (x-1) & 3;
printf("%u\n", (unsigned int)x);
return 0;
}
 
H

Hans Vlems

I'd say my OP had a metaphysical question. When I looked at the OCC
entries I realized that being able to do crazy stuff with C requires
you to know what works and what doesn't work. I'm interested in
finding out

1. What is it that I don't know which OCC participants know about C?
2. How do I go about knowing what I don't know?
3. What is it that neither I nor the OCC participants can know or take
for granted about C?

etc.- Tekst uit oorspronkelijk bericht niet weergeven -

- Tekst uit oorspronkelijk bericht weergeven -

Good programs have two characteristics:
- their algorithms are constructed in a way that their correctness can
be proven
- their runtime (elapsed and processor time) is as efficient as can be
All it takes is studying the craft of building algorithms. Once you've
mastered that, learn
a programming language to implement them in.
C.A.R. Hoare and E.W. Dijkstra are among the first to stipulate
rigorous mathematical training for fledgling programmers. You might
want to read their books to get an idea about the point I'm trying to
make.
What triggered my response was your wish to do clever things in C (or C
++, C# whatever).
Note that the language may be obsolete in 2014. Unlikely? Well, ask
those clever APL programmers :)
Your desire to wear a pointed hat, be a true wizard, is as old as our
trade. In the days
when computers had little memory, tricks to conserve memory made
sense. Assembly languages are even better vehicles for obfuscated code
than C.
The bottomline is that if you want to be an expert programmer then
learn to be a mathematician first.
Hans
(who's just a chemist)
 
K

Keith Thompson

Thad Smith said:
I haven't looked at OCCC entries in a long time, but my recollection
is that several of the older entries worked for a particular compiler
and not all Standard C compilers. Stretching the bounds of a single
compiler is different than doing unusual things within the confines
of Standard C.
[...]

It's actually called the IOCCC (International Obfuscated C Code
Contest); the web site is www.ioccc.org. Alas, it seems to be
dormant; the last "What's New" entry is:

30 April 2010: Announcement coming soon. Check back here on
15 May 2010!

(Unless that's 2010 factorial, in which case the lack of an update
is entirely unsurprising.)

Yes, the early entries tended to be extremely non-portable.
One of the entries from 1984 declares main, not as a function,
but as an array; the initial value was machine code that would
execute either on a VAX or on a PDP-11. Such entries tend to
result in rule changes; in more recent years, submitted code must
be reasonable portable.

Of course, do not for a moment mistake the extreme cleverness of the
IOCCC entries for Good Code.
 
E

Eric Sosman

[... concerning IOCCC ...]
Yes, the early entries tended to be extremely non-portable.
One of the entries from 1984 declares main, not as a function,
but as an array; the initial value was machine code that would
execute either on a VAX or on a PDP-11. Such entries tend to
result in rule changes; in more recent years, submitted code must
be reasonable portable.

My all-time favorite (which resulted in a rule change) was
the extraordinarily flexible one-line program

#include "/dev/tty"

(There was also a zero-line entry, but it somehow failed to elicit
the same gasp of delighted astonishment.)
 
K

K4 Monk

What value is printed by the following code?
#include <stdio.h>
unsigned char x;
int main(void) {
   x = (x-1) & 3;
   printf("%u\n", (unsigned int)x);
   return 0;

}

I think the answer you're looking for is that the unsigned int is not
initialized to a particular value. Therefore x-1 is a random number.
The bitwise representation of 3 is 011b. (x-1) & 011b can give us
0,1,2,3
 
J

Jens Thoms Toerring

Please don't snip attribution lines, here should be something
like

Instead remove signatures like this one
I think the answer you're looking for is that the unsigned int is not
initialized to a particular value. Therefore x-1 is a random number.
The bitwise representation of 3 is 011b. (x-1) & 011b can give us
0,1,2,3

'x' is a global variable, so it's (like static variables) auto-
matically initialized to 0.
Regards, Jens
 
J

Jorgen Grahn

Good programs have two characteristics:
- their algorithms are constructed in a way that their correctness can
be proven

With that definition, there *are* no good programs (except maybe some
of Knuth's work). I happen to think that by applying *some* of that
Dijkstra-ish thinking (invariants, preconditions ...) is good for your
programs ... but even *that* seems rare.
- their runtime (elapsed and processor time) is as efficient as can be

I think most people would disagree with that!

/Jorgen
 
H

Hans Vlems

With that definition, there *are* no good programs (except maybe some
of Knuth's work).  I happen to think that by applying *some* of that
Dijkstra-ish thinking (invariants, preconditions ...) is good for your
programs ... but even *that* seems rare.


I think most people would disagree with that!

/Jorgen
Proving algorithms is not difficult, nor time consuming and surely not
unnecessary.
Hoare, Knuth, Dijkstra et al demonstrated this quite well. But it's
like writing
documentation, it's the last thing programmers do, and hate to do...
I fail to see why people would disagree with the second statement. You
prefer to wait longer
for a program to finish?

Hans

PS
When you decide to remove text, make sure to remove the lines that
identify the author(s) too.
Now you're attributing text to me that I'd never have written...
 
H

Herbert Rosenau

I think the answer you're looking for is that the unsigned int is not
initialized to a particular value. Therefore x-1 is a random number.
The bitwise representation of 3 is 011b. (x-1) & 011b can give us
0,1,2,3

No. x is at file scope and for that it is initialised at program
startup with the value 0.

printf prints 3 if the program is running on a 2 complement mashine
because 0 -1 gets 0xffff (16 bit) or 0xffffffff on a32 bit mashine as
result of the expression in () and 3 after the operator & has taken
its work.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 2.0 ist da!
 
K

Keith Thompson

Hans Vlems said:
Good programs have two characteristics:
- their algorithms are constructed in a way that their correctness can
be proven [...]
- their runtime (elapsed and processor time) is as efficient as can be

I think most people would disagree with that!
Proving algorithms is not difficult, nor time consuming and surely not
unnecessary. Hoare, Knuth, Dijkstra et al demonstrated this quite
well. But it's like writing documentation, it's the last thing
programmers do, and hate to do... I fail to see why people would
disagree with the second statement. You prefer to wait longer for a
program to finish?

Efficiency is good, all else being equal. I think Jorgen was
disagreeing with "as efficient *as can be*". If you make a program
run 1% faster while destroying its portability and maintainability,
that's unlikely to be a good tradeoff. (Not impossible, just
unlikely.)
 
J

Jorgen Grahn

....

Proving algorithms is not difficult, nor time consuming and surely not
unnecessary.
Hoare, Knuth, Dijkstra et al demonstrated this quite well. But it's
like writing
documentation, it's the last thing programmers do, and hate to do...

Really? I got the impression (during my CS education, which was
admittedly ~20 years ago) that it was difficult enough to be
infeasible for anything beyond (say) sorting algorithms. Especially in
imperative languages like C -- the lecturer used it as an argument for
pure functional programming languages.
I fail to see why people would disagree with the second statement. You
prefer to wait longer
for a program to finish?

The effort you spend optimizing the program to perfection could
usually be spent better: maintainability, features, correctness, being
done earlier.

(I'm not in the camp which screams "premature optimization!" though.
I think you should choose data structures and architecture with
performance in mind, because patching it later when you realize
performance is too low can be very difficult.)
Hans

PS
When you decide to remove text, make sure to remove the lines that
identify the author(s) too.
Now you're attributing text to me that I'd never have written...

The misattribution above (if there is one, after you considered the
'>'s) comes from your own failure to add attribution lines for the
"... my OP had a metaphysical question ..." part.

/Jorgen
 
S

Seebs

1. What is it that I don't know which OCC participants know about C?
2. How do I go about knowing what I don't know?
3. What is it that neither I nor the OCC participants can know or take
for granted about C?

Hmm.

Long ago, I wrote:
http://www.seebs.net/c/growup.html

I sort of want to come back to the topic now and write about what's happened
since. Might get around to it.

-s
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top