WANTED: "C" code line counter program

Doug Lee dlee at pallas.athenanet.com
Thu Mar 14 16:37:21 AEST 1991


In article <dcda02id05Q.01 at JUTS.ccc.amdahl.com> cpm00 at DUTS.ccc.amdahl.com (PUT YOUR NAME HERE) writes:
>What about counting newlines, but ignoring those
>that immediately follow another newline (ie, skip blank lines)?

My first thought was to skip all comments (single- and multi-line) and then
count only lines containing characters other than whitespace.  This should
be close, though it will still overcount on constructs like
    if (( <long_condition_1> ) ||
        ( <long_condition_2> ) ||
        ... )
Then again, maybe a line that long *should* count as more than one line.  We
also run into the somewhat common declaration syntax
    char *
    foo()
which, by my method, counts as two lines.

Unfortunately, I see no quick way to give a consistent line count regardless
of program syntax.  Counting lines ending in '{' or ';' (after removing
comments and trailing whitespace) would catch most loops and function
definitions without counting them more than once, but constructs like
    while (line = get_next_line(file))
        (void) process_line(line);
would still count only once unless the braces were included (not a bad idea,
imho).  We need a more precise definition of "line" for this, I fear.

Does this remind anyone else of _The Mythical Man Month_?  :-)

-- 
Doug Lee  (dlee at athenanet.com or {bradley,uunet}!pallas!dlee)



More information about the Comp.lang.c mailing list