Sunday, November 27, 2011

Shrinking the C code !

One of my friends  ,asked me to write a C program that will reverse and print the words in a file with minimum number of lines. Our intention was to create a program that will be of minimum size. Below is  my solution to the question.

My question to the readers would be , can you develop something shorter than this? If so feel free to post it out.
 So the above code is gnu99 standard , now let us try the c99 standard. Below is the code..

Noted the difference?? Well , here I have declared variable i as a character and have used it as an integer afterwards! For the integers between -128 to +126 ,character declaration will do. Even though this is not a rocket science , most students are unaware of it.


  1. There is an Mistake , Most Students Who Have Not Read " K & R The C programming " is unaware of it .
    There is an example in that book describing this usage

    Happy Hacking

  2. Well, most students have skipped K & R in my view. I started reading K&R only recently...

  3. You cant argue that this code is not shorter....
    main(int argc,char **argv)
    FILE *fp=fopen(argv[1],"r");
    char buffer[127],i;
    while(fscanf(fp,"%s",buffer)!=EOF&&printf(" "))

    and dont put screen casts for code...cant copy

  4. Well said ;)
    About the screen cast, I will fix it next time.....

  5. Shrinked more.....Now 8 lines...try this

  6. The use of fscanf makes my skin crawl. You do realize there's zero checking to make sure you don't over-run your buffer, right? This means that strlen(buffer) may be greater-than sizeof(buffer) (Why is buffer capitalized in your sample? It isn't a constant and has not been #define'd.)

    So my use of C may predate C89 support in my first compilers so I don't know what's been changed, but...
    With regards to 'char'... You can only be sure it is between 0 and 127 as it may or may not be signed. Some compilers use unsigned chars by default while others require a compile-time option to make them unsigned by default. You need to be aware of the setting when it matters. (This is one of the reasons why people have a harder time writing portable C code than they initially think -- and why autoconf exists.)

    Additionally, unless it was standardized in one of the standards... the return type of printf() has not always been guaranteed and it used to be you could find some C libraries where it returned void. (A nit, I know.)

    How does it go? Simple enough to work -- but no simpler?

    Really, though, my big issue is that any solution relying on a scanf function should be thrown out. There are folks that (rightly) consider any use of scanf/fscanf/sscanf to be a bug. Very large words may be the outliers in your data set, but they shouldn't cause the program to crash.

    I-am-sorry-if-you-disagree-but-such-things-happen-in-real-life-and-you-need-to-be-prepared-for-the-worst-unless-you-like-programs-to-crash. (139 bytes. It wouldn't crash the first example, but would crash the second.)