http://www.perlmonks.org?node_id=185314


in reply to Counting lines of C code

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.


Jeremy