What’s the shortest code to print all alphabets?

Note: This post is not a tutorial!

Ever thought about what’s the least amount of code you need to do something, maybe something simple like printing all the alphabets? (A-Z). In this post we’ll see what could be the shortest code to do that, in the journey we might learn a few things, who knows! (I’ll be using C as my language in this post)

First off, the obvious way to print A-Z is like this:

#include <stdio.h>
int main(){
    printf("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    return 0;
}

Obviously, this is C, and we don’t need to have int as return type of main, so let’s change it to void (it’s by default so no need to write anything) and also remove the return statement. So code would be reduced to :

#include<stdio.h>
main(){printf("ABCDEFGHIJKLMNOPQRSTUVWXYZ");}

Note that I have also removed the indentations and the newline characters to make it even smaller, but we still need #include and main() to be in different lines as #include needs to be on a seperate line. Our code right now is 63 characters long (including newline character at end of first line).

How can we make this smaller? First of all, writing all the alphabets manually inside the printf statement doesn’t seem right. What if we change that? I mean, we could use ASCII values of characters and loop from A (ASCII value 65) to Z (ASCII value 90) right? Let’s try that out:

#include<stdio.h>
main(){int i=65;while(i<91)printf("%c",i++);}

Well, it looks like we might have improved our code, but it still is as big as the previous code. exactly 63 characters long. Hmm… Hey wait a minute, we’re printing characters with printf, why not use putchar()? And even better putchar doesn’t need stdio.h! Perfect! Let’s do that:

main(){int i=65;while(i<91)putchar(i++);}

Awesome! now out code is just 41 characters long!  Could the code be even more smaller?

Yea, the meme was not needed, but what if I said the code can be even more compact? Let’s see how.
We’ll need the help of recursion for this. As you have seen we used a int variable in our previous code, but instead of declaring a new variable, we can use main function’s argc (argument count) variable, which takes the value of now number of command line arguments passed, which in our case is zero. So we can iterate from 0 to 25 and print all the 26 characters, using recursion:


main(a){putchar(a+64);if(a<26)main(-~a);}

Well, this is the final code, as you can clearly see this is 41 characters, i.e. it’s as long as the previous code, we need to make it shorter. I’ll do that by replacing if() with a XOR operator. So final code would be:


main(a){putchar(a+64);a^26&&main(-~a);}

So, that’s it! 39 characters of code! And it works perfectly.
I think I’ll have to explain few parts. First is the XOR operator and how it replaced if() statement. XOR has this property that unless two equal numbers are there on both sides of operator, it gives a non-zero number. So a^26 will be false only when a becomes 26. So until a acquires the value of 26, main() is called again. Once it is 26, the statement a^26&&main(-~a); stops at the first part and never goes to the second part as in an AND statement if first statement is False, there is no need to check for the second part, so compiler just ignores it.
And next what I think I need to explain is the -~a thing I did while recursing. The ~ operator is called the tilde operator or the bit-wise not operator. -~a is the same as a+1 because of two’s complement. So main would be called with next number each time.

 

Ok that’s cool but in fact, we didn’t have to go for recursion, we could just use argc variable and avoid the declaration we had in our 41-character long program and:

main(i){while(i<27)putchar(i+++64);}

We have a code with just 36 characters! Cool!

Ok! That’s how far I could shorten the program! If anyone has any doubts/suggestions or a better/shorter code please comment!

Hope you enjoyed reading this post! Keep visiting the blog for posts like these!

Logo

About these ads

8 thoughts on “What’s the shortest code to print all alphabets?

  1. abuse argc even more can get you down to 33 chars… ;P

    main(i){for(;i<91;putchar(i++));}

    save as alpha.c and then run with

    cc -o alpha alpha.c && ./alpha 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

    • I was kind of avoiding command line arguments, but yeah, whatever I guess it can be done in just 33 characters now! :D

  2. Or, I could use a different language:
    Python:
    print”ABCDEFGHIJKLMNOPQRSTUVQXYZ”
    33 chars
    Bash:
    echo {a..z}
    11 chars

  3. Well, brilliant tutorial…..
    But, is it just me that the compiler complains that :
    ‘putchar’ was not declared in this scope…
    And i am pretty sure c++ has no in-built funtions. So, definitely 1 #include is needed to use putchar…
    .
    One more thing,
    main(){} : the return type is NOT void.. It is int indeed!
    main automatically returns 0 even if you do not specify… i.e,
    “main(){}” and “int main(){}” are both correct and both return 0 by default… :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s