in reply to Counting lines of C code

do you all know a good and SIMPLE script that can counts the line of code for a c or c++ code ?

The simplest, most naive way to do this is   wc -l or the equivalent in Perl, which would look something like

#!/usr/bin/perl my $lc = 0; while ( <> ) { $lc++ } print "$lc\n";

The meta problem depends on what you're going to use the LOC count for. If you're trying to draw a parallel between actual code and lines of text for some purpose that will eventually involve rating coders, you're in for a lot of trouble. People quickly figure out how to generate lots of lines of stuff when they're being graded on it. Simple ways of counting can lead to stupid behavior.

Replies are listed 'Best First'.
Re: Re: LOC
by Chmrr (Vicar) on Jul 25, 2002 at 19:49 UTC

    As a side note, it's probably slightly easier to say:

    perl -e '1 while <>; print "$.\n"' file1.c file2.c

    ..which is what the $. special variable is more or less meant for.

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

      Or equivalently, (still naive as dws points out, but a few less keystrokes :-)

      perl -lne'}{print$.' file1.c file2.c

      Update: Massaged one-liner, but still manage to lose by 2 strokes...

        If it's a golfin' that ye'll be wantin', just say so!

        perl -ple'}{$_=$.' file1.c file2.c

        Update: Shaved off a semicolon.

        perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'