Take the 2-minute tour ×

I'm writing a simple LOC counter in C to count how many lines of code are in my C source files. It's meant to be run from the command line, redirecting the target file as input and seeing a total line count printed to standard out. For example:

  counter.exe < counter.c

So far the only rules I'm using are:

  1. Only count lines that have more than 3 characters (no blank lines or lines that only have a closing brace and semi-colon, etc).

  2. Don't count spaces as characters.

Here's my program:

#include <stdio.h>

int main() {

    int input;
    int linecount = 0;
    int charcount = 0;

    while ((input = getchar()) != EOF) {

        if (input == ' ') {
        else if (input == '\n') {
            if (charcount > 3) {
            charcount = 0;
        else {

    printf("%d\n", linecount);

    return 0;

My question is, can you offer some improvements to the rules to make this a more valid measure? Do people often count comments as valid lines of code? How about spaces or blank lines?

I don't want to start a debate over the validity of LOC counts in general, it's something I've been asked in several interviews and I think is worth knowing, in a general sense, how many lines of code my own projects are. Thanks!

share|improve this question
I wouldnt worry so much about counting the lines of code, it is in my experience irrelevant. Something which counted the quality of code on the other hand... –  Mick Walker Mar 11 '11 at 16:03
I have to agree. You could even argue that a smaller line count is better, if it has the same functionality and is easy to understand by others. But again I'm interested in this because I was asked this during an interview question (how many lines of code would you say were in the largest project you've personally worked on?) I drew a blank, I really couldn't even come up with a guess. So this is a programming exercise and also just to get a deeper understanding of what a LOC count might be useful for. Thanks again. –  dvanaria Mar 11 '11 at 16:20
Just a comment on the code itself: if you find yourself writing an empty if statement, this means there is usually a better way to structure your if's. Ex. if (input == '\n') { ... } else if (input != ' ') { ... } –  MahlerFive Mar 11 '11 at 16:46
Do tabs count as regular characters? –  pmg Mar 11 '11 at 21:36
hi, how to make the while loop'll exit ? –  Kumar Jun 13 '11 at 6:10
add comment

5 Answers

up vote 5 down vote accepted

Generally, people do:

  • Count comments as LOC
  • Count blank lines as LOC

However, people also assume/practice:

  • Comments included are necessary/useful (not gratituitous)
  • Blank lines are not excessive, and exist to provide clarity

Code line counters, as a result, generally take into account all line breaks in their computation.

share|improve this answer
+1 for "Comments included are necessary/useful (not gratituitous)" –  Sagar Mar 11 '11 at 16:02
add comment

Don't write a program. Use wc --lines

share|improve this answer
But it will count comments. –  Raveline Mar 11 '11 at 16:00
@Raveline: So will OP's program. –  nmichaels Mar 11 '11 at 16:03
Thanks, I was doing this more as a programming exercise though. I agree, normally an existing tool would be preferred. –  dvanaria Mar 11 '11 at 16:04
Combine with variations of grep -v to skip blank lines, short lines, and comments as desired. –  nmichaels Mar 11 '11 at 16:04
add comment

I guess I came from a different development style than kvista, but we counted lines of code and comments separately. We expected that the comments would be a certain percentage of the total lines (comments + code). We didn't count blank lines at all.

If you're looking for a bit of a programming challenge, you can measure the cyclomatic complexity (or conditional complexity) of your C programs.

share|improve this answer
add comment

Make a schedule of how long it will take you to write and debug the application. Note the ending date.

Spend some time searching the web for tools such as:

Take the remaining time in your schedule and go on vacation. :-)

Otherwise the task becomes monstrous. Such as parsing comments. Defining a line (and how many statements or tokens are on a line). Are blank lines counted?

Save your precious time for other projects. Use existing tools.

share|improve this answer
add comment

I realise this was a programming example, but if you want to use a windows command line I found this.

Which lead to:

findstr /R /N "^" *.h *.c | find /C ":"

Counts all lines in .h and .c files.

share|improve this answer
add comment

Your Answer


