Print C line numbers intelligently

Up ../

prcl — print C source code with intelligent line numbers

Many documents have code extracts with line numbers on the left of the lines that are referred to in the text. W.Rich Stevens' texts on TCP/IP and Network Programming are good examples. In many cases I suspect the line numbers are generated by pr(1) or cat(1) with the -n switch and the numbers manually removed from the uninteresting lines. I wondered whether a simple program could do a little better.

Prcl is a small program that prepends line numbers to interesting lines in C source code for the purposes of documentation etc. Uninteresting lines such as blank lines, line containing only comments, strings and punctuation do not have their line numbers printed. A selection of keywords appearing on a line with only other uninteresting symbols are also deemed uninteresting.

$ ./prcl example.c

        //    @(#) example.c
   4    # include    <stdio.h>
        //    C++ style comment
   6    main (int argc, char* argv[]) {
   7        if (argc < 4) {
   8            switch (argc) {
   9            case    1:
  10                printf ("Hello!\n");
  12            case    2:
  13                printf ("Hello %s!\n", argv[1]);
  15            case    3:
  16                printf ("Hello %s and %s!\n", argv[1], argv[2]);
            else    {
  21            printf ("Hello all!\n");

Source at
Only requires the standard C [89] library so no Makefile
Build with cc -o prcl prcl.c

This program has no knowledge of C syntax or preprocessor constructions so erroneous C code and clever macros will likely break it. What constitutes an uninteresting keyword is a matter of taste but can be add or subtracted from the BORING[] table in the program's source.

Creative Commons CC0

James Sainsbury

Pr(1) with -n 8 will print numbers against every line.

Indent(1) is a C beautfier and source code reformatter that probably can do intelligent line numbering if you can locate the correct switches