Let's Have Some Fun with 'C'

This page contains three C programs, each interesting in its own way and guaranteed to pique your curiosity, which demonstrate the versatility and power of the language in the hands of creative programmers.


A Variant on the Original korn.c

Do you know that there is an International Obfuscated C Code Contest (IOCCC) that showcases some of the craziest, the most beautiful, the most creative and mind-blowing C programs written by some of the masters? That it started all the way back in 1984: more than 40 years ago?

The absolute best, a masterpiece of C coding, was the winner of the contest in 1984. It was written by David Korn who also wrote the Korn Shell for UNIX to overcome the limitations of the original Bourne Shell and the later C shell.

Here is a modified version of it! (also given below). Try to figure out what it prints and, more importantly, how! This works only on Linux or UNIX OS. Ignore all the warnings when you run it. Remember pointers and arrays as we talked about them in the class.

main() { printf(&unix["\021C%set\012\0"],(unix)["Chakravarthy"]+"Trick"-0x67);}

Tribute to Dennis Ritchie on the 50th Anniversary of 'C'

Here's another example: this one created by me in honour of Dennis MacAlistair Ritchie, the American computer scientist on whose shoulders the entire world runs today. He created the C programming language and the UNIX operating system. The influence of C is so powerful that its features are clearly visible in almost all programming languages that came later, be it C++, be it Java. Today, every Android phone and every high-performance supercomputer in the top-500 list runs Linux. Linux is a derivative, arguably a clone, of UNIX. Read this wonderful tribute to Dennis Ritchie to see his impact on the technological world.

The following program (also given below) illustrates the beauty of C: a language whose syntax is flexible, yet powerful. A language that lets you write the most professional code meeting every high standard set by your organisation, yet also lets you write programs that satisfy your innermost creative instincts.

#include <stdio.h>
#include <stdlib.h>
#include "tribute.h"


                                int  main
                                () {DENNIS
                                *the, *man;
                                int dmr,r,o,
                                c,k      ,s;
                                int     god[
                                UNIX    ][C],
                                krc     [UNIX
                                ][16    *C];
                                char    cwd[]
                                = {     'C',
                                'U',    'N',
                                'I',    'X',
                                };(!    (the
                                = Fedora16(
                                FNAME, "r"


                                ) ))       ? (
                                exit      (1))
                                :(01)    ;(! (
                                man= Fedora16(
                                "dmr.txt" ,"w"
                                )))   ?   (exit
                                (02)      ):(-
                                unix      );att
                                (r){      dec(
                                c ){      mul(
                                the,      "%d",
                                &(god     [r][
                                c] )      );}}
                                pdp11    (the);


                                att(r){dec
                                (c){dmr=god
                                [r][c];for(k
                                =0;     k<16;
                                k++     ) {
                                krc     [r]
                                [K and R]=
                                dmr%2;dmr
                                >>=   1;}}
                                }att   (k){
                                for(    r=0;
                                r<16     *C ;
                                r++)     (krc[


                                k][r] == 0 )
                                ? ( D ( man,
                                "%c",' ')):(
                                D(man, "%c",

                                cwd[(k+r)%5]
                                )); D (man ,
                                "\n");}pdp11
                                     (man);
                                    exit
                                   (0)
                                ;}
  

Copy this file and this file into the same directory as the C program above. Compile the program as

gcc -o dmr final-sept09.c
Run it by typing ./dmr in a terminal. It will generate a text output file called "dmr.txt". Open it in a text editor and have fun!


Kernighan and his Turing Award Lecture

Finally, let's end this session on C with another gem: this time from the one who co-wrote its definitive guide/tutorial, a book respectfully referred to as K&R C. I am talking about Brian Kernighan, of course! This is, many consider, the precursor to computer viruses although its origins came from the extremely benign idea of making software installations easy. You can see all the wonderful programs authored by BWK here.

Run this program (given below) and see what happens.

char*p="char*p=%c%s%c;main(){printf(p,34,p,34,10);}%c";main(){printf(p,34,p,34,10);}
Again, compile it as
gcc -o quine quine.c
Run it by typing ./quine. See the output and try to figure out exactly how it is done.


DONE FOR NOW!