C Program to Count Lines of Code

14,512

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

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:
14,512
dvanaria
Author by

dvanaria

I'm currently working for a telecommunications company in Colorado, doing some software development and systems integration work. I've been interested in programming from an early age, from about when I was 13 or so, programming on Apple II systems at school and eventually at home when my father bought me an Apple IIc. I loved picking up programming books from the public library and just picking out whatever interested me and trying it out first hand. I went on to learn C programming in college, then OpenGL graphics programming, Object Oriented Programming with Java, just about anything new to me I found interesting. Today I still work on my own programming projects (now usually in Python and C++), but I've always had great memories of how much fun it was to discover programming when I was a kid. I'm more interested these days in getting other people interested in computers and programming, maybe trying to inspire other people (especially kids) to get into it. I'm always going back to the basics and really trying to break concepts down so they are accessible and so I understand them better myself. My hope is to one day either write a programming book for kids, that recaptures some of that wonder and excitement, or develop a series of YouTube tutorials that may help newbies pick up programming in a way that is more accessible and easily understood. I think today's biggest barrier to entry in this field of interest is the complexity of today’s systems. It's not like the old days where you turned your computer on and it booted in a few seconds into a BASIC command prompt. Those systems were a lot of fun because you had to pick up programming right from the start in order to really do anything with them. Either way, this site (Stack Overflow) has been a tremendous help to me and a lot of fun to contribute to. Ideally, I would love to feel some kind of expertise with programming in general, and this site is a good step toward getting that kind of experience – the best way to learn anything, I’m convinced, is to teach others, to ask a lot of questions, and help out other people by answering their questions.

Updated on June 13, 2022

Comments

  • dvanaria
    dvanaria almost 2 years

    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
      15
    

    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) {
                   linecount++;
                }
                charcount = 0;
            }
            else {
                charcount++;
            }
        }
    
        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!

  • Sagar
    Sagar about 13 years
    +1 for "Comments included are necessary/useful (not gratituitous)"
  • nmichaels
    nmichaels about 13 years
    @Raveline: So will OP's program.
  • dvanaria
    dvanaria about 13 years
    Thanks, I was doing this more as a programming exercise though. I agree, normally an existing tool would be preferred.
  • nmichaels
    nmichaels about 13 years
    Combine with variations of grep -v to skip blank lines, short lines, and comments as desired.