The Joy of Clang

If it were spelled Klang, then it might be a reference some Klingon character from Star Trek, but it’s not. Clang is one of those program names that’s a contraction. In this instance, it’s a portmanteau of C Language. It’s an ├╝ber nifty command line compiler.

In my most recent C programming book, Beginning Programming with C For Dummies, I focus on the IDE method of program creation, specifically by using the Code::Blocks environment. That’s pretty much the way all the cool kids are programming today.

For the small programs introduced in my books, however, the command line works just fine. In fact, if you’re familiar with the command line, it can be easier to use than an IDE for those small programs.

I use the command line to compose all the sample code in this blog.

For years, the cc compiler, or the GNU version gcc, was preferred for compiling C code. After all, cc, was the original C Compiler developed by Kernighan and Ritchie.

Today, a better solution is clang, the C language compiler. Unlike gcc, it doesn’t attempt to compile everything, just C code. And it does a super spiffy job.

On many Unix systems, cc is aliased to the clang compiler, so using cc or clang to compile is the same thing.

First, a tutorial:

To compile on the command line, you need a text editor and a compiler/linker. The routine works like this:

  1. Use a text editor to create your code. I use vim, the vi improved editor, although Unix is bursting at the seams with text editors various and sundry.
  2. Compile and link the code. The cc command is followed by the source code filename. That command both compiles and links, assuming no errors are found.
  3. Run the program. Type the program’s name at the prompt to see how it works.

As an example, suppose you wanted to compose the following code:

#include <stdio.h>

int main()
    int x;



This code contains a deliberate error — a common programming mistake, but not one that generates a warning when compiled with cc. If you can’t find the problem (and most programmers won’t, especially in their own code), then clang will.

Assume the source code filename is 0607.c. The code is compiled with this command:

clang 0607.c

Figure 1 shows a screen cap of the output:

Figure 1. Compiling bugging code by using clang.

Figure 1. Compiling bugging code by using clang.

As you can see, clang dutifully spotted the errant semicolon — a bane of all programmers. It specifically pointed out not only the line, but its assumption of what was wrong. Further, it offered a suggestion on how to avoid the warning in the future, should this condition be intentional.


clang was developed by Apple and Google several years back, designed to be perfectly compatible with cc. If you’re using gcc to compile a terminal window, switch to clang. You can determine whether or not the system has clang installed by typing:

clang --version

at the command prompt.

Leave a Reply