Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Counting lines of C code

by Sara (Acolyte)
on Jul 25, 2002 at 19:08 UTC ( #185289=perlquestion: print w/replies, xml ) Need Help??
Sara has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks , do you all know a good and SIMPLE script that can counts the line of code for a c or c++ code ? thanks
edited by boo_radley

Replies are listed 'Best First'.
by RMGir (Prior) on Jul 25, 2002 at 19:17 UTC
    wc -l *.cpp *.h? :)

    Seriously, what are the restrictions? Counting or not counting comments? Blank lines? Are you going to penalize one developer over another because one does

    if(a) { do_this(); }
    and another does
    if(a) do_this();

    You probably want to go over to and type "metrics". That'll find you things like CCCC, to pick the first result from the list.

    I like perl, and I do a lot of things with it, but this isn't a wheel I'd chose to reinvent.

by dws (Chancellor) on Jul 25, 2002 at 19:22 UTC
    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.

      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...

by enoch (Chaplain) on Jul 25, 2002 at 20:10 UTC

    As other people have stated wc -l is the easy way to go. But, if you want "lines" of code as in actual code statements, well, that gets a little more rough.

    Parsing out comments in C/C++ is not trivial. Page 293 of Mastering Regular Expressions, 1st Edition gives a regex that will remove multiline C comments ( /* */ style), but like I said, it is non-trivial.

    Furthermore, statements, themselves, could span multiple lines; thus, making a line count of non-comment lines be not indicative of how many statements of code are in a file.
    if( foo ) i = 1; // just one statement if( foo ) i = 1; // still just one statement if( foo ) { i = 1; // still just one } if ( foo ) { i = 1; // 1 statement }

    So, I doubt it would be worth the effort to try and define statements of C/C++ code and count them. I would stick to wc -l.

Re: Counting lines of C code
by BrowserUk (Pope) on Jul 26, 2002 at 23:04 UTC

    Not that the point really needs reenforcing any, but the story might amuse.

    A few (more..) years ago whilst contracting at a large azure company, a bean counter somewhere started recording "kloc metrics". Through an accident of mixed up mailing lists, one of 'us' became aware what some of 'them' were doing, and for some strange reason, a relatively small group of 'us' starting harmonising our comment block style on the rather obscure

    /*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; This bit should be reasonably self explanitory ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/

    which was nice, but soon became passé...and had to become

    //;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; // this bits quite important //;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    but again this went out of vogue as the fashion police became cleverer at detecting fakes so

    {;};{;};{;};{;};{;};{;};{;};{;};{;};{;};{;};{;};{;};{;}; // Not sure about this bit {;};{;};{;};{;};{;};{;};{;};{;};{;};{;};{;};{;};{;};{;};

    This cycle-of-fashion ran on for a while until the edict came down from on high

    Thou shalt not use extranious semi-colons in C code

    The challenge then for the top couturiers was to see who could write the longest single, valid, line of c code.

    Of course, the comma operator came into its own for this.

    Interestingly, my very first sight of a Perl program (deftly moving the subject back on-topic:), was a 300+ line Perl/CGI script of which about 200 lines was a single, poetry-mode, print statement producing a (near) static table in html.

    I knew then that I was going to like Perl...albeit that it was one ugly piece of code.

by grep (Monsignor) on Jul 25, 2002 at 19:20 UTC
    Yes, it's standard *nix/Cygwin utility called wc.

    If you want a perl script, you should try and write one yourself (you should be able to do this even as a novice in just a couple of lines). Then if you run into a problem you can post your code and ask your question here.

    Just me, the boy and these two monks, no questions asked.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://185289]
Approved by grep
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2017-08-18 01:49 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (294 votes). Check out past polls.