Quine

Quine is a self-reproducing program. Here's the task as defined by Ken Thompson in his article “Reflections on Trusting Trust” (Communication of the ACM, Vol. 27, No. 8, August 1984, pp. 761-763):

…to write a source program that, when compiled and executed, will produce as output an exact copy of its source.

And this is one of many possible solutions:

main(a,b){a="main(a,b){a=%c%s%c;b='%c';printf(a,b,a,b,b);}";b='"';printf(a,b,a,b,b);}

Duff's Device

The point is to express general loop unrolling directly in C.

void send (register int *to, register int *from, register int count)
{
  register int n = (count + 7) / 8;
  switch (count % 8)
  {
    case 0: do { *to++ = *from++;
    case 7:      *to++ = *from++;
    case 6:      *to++ = *from++;
    case 5:      *to++ = *from++;
    case 4:      *to++ = *from++;
    case 3:      *to++ = *from++;
    case 2:      *to++ = *from++;
    case 1:      *to++ = *from++;
            } while (--n > 0);
  }
}

CORDIC

Trigonometric functions can be effectively computed using hardware as simple as shift registers and adders using CORDIC algorithm. And I thought it's always about power series and table lookups.

Anonymous Snippents

Can you figure out what this function does? Hint: K&R might help…

int function (unsigned int value) {  
  int count;  

  for (count = 0; value; count++)
    value &= (value - 1);
  return count;
}

To see the answer drag your mouse from here… function returns the number of bits set in value.…to here.

Ok, how about this line? Both x and y are integers.

  x ^= y ^= x ^= y;

To see the answer drag your mouse from here… this line swaps x and y…to here.

Error Messages

These are actual error messages from different computer programs.

OHHHH.... I give up
Core dumped

You lied to me when you told me this was a program

PROGRAMMER GOOFED . . .  YOU SHOULD NEVER SEE THIS MESSAGE

YOU CAN'T DO THAT!

Sorry, I don't know how to help in this situation.
Maybe you should try asking a human?

This can't happen.

I'm broken. Please show this to someone who can fix.

very funny

Keyboard error or no keyboard present. Press F1 to continue.

String literal too long (I let you have 512 characters, that's 
3 more than ANSI said I should)

a typedef name was a complete surprise to me at this point in 
your program

You can't modify a constant, float upstream, win an argument 
with the IRS, or satisfy this compiler

This struct already has a perfectly good definition

type in (cast) must be scalar; ANSI 3.3.4; page 39, lines 10-11 
(I know you don't care, I'm just trying to annoy you)

Can't cast a void type to type void (because the ANSI spec. 
says so, that's why)

This label is the target of a goto from outside of the block containing this label AND this block has an automatic variable with an initializer AND your window wasn't wide enough to read this whole error message

Call me paranoid but finding '/*' inside this comment makes me suspicious

I didn't think this set of error conditions could ever happen

ERROR 1164   HOW IN THE HELL DID YOU GET HERE?

Have I been a bad computer?

You must be joking.

ERROR 0:  POWER NOT ON

MORE CORE AVAILABLE, BUT NOT FOR YOU

NONE of your errors have been found

Comments

/* You are not expected to understand this */

C++

I just love C++. You can write something like this:

template<typename T, int size> int arraysize (T (&)[size]) { return size; }

This little gem is perfectly legal (even though Visual C++ v6.0 wouldn't accept it), and so much better than the hopelessly outdated

#define ARRAYSIZE(x) (sizeof(x)/sizeof(x[0]))

Isn't it?

Garbage Collection

Boehm-Demers-Weiser conservative garbage collector for C and C++ is useful for fighting memory leaks.

Fine Print

I couldn't trace back all the authors. If you know who should get the credit please drop me a line.


Back to table of contents