As I’ve noted in recent posts and tweets, my interest in C coding has been revitalized. A couple of weeks ago I found some source code from my college Computer Science and Computer Engineering courses. Of particular interest were my programs that counted lines of code in C source files. I re-entered the code, compiled it and started working with it and studying it.
As I recall the original class assignment was to write a program (loc.c) that simply counted lines of code, ignoring comments and blank lines. The follow-up to that assignment was to enhance the original program (fnloc.c) to identify functions and count the lines of code within them.
The program’s output is a listing of all the functions with their respective lines of code and then a summary showing how many functions it finds and the total lines of code in functions, how many lines of code located outside functions (compiler directives and global declarations), and the total number of lines in the code. I found the second program useful and I made a few minor modifications to it even after the class concluded.
After resurrecting the code I found myself trying to understand how the code works and the logic behind it. I ran the program against various source files and compared the results with my manual code counts. I found discrepancies between the counts and started looking at the code to see why some lines of code weren’t being counted correctly. See Revisiting Old Code and the accompanying comments for more details.
I created README and CHANGELOG files for the program to detail pertinent information and create a chronological history of the changes. I also read up on C code styling standards and formalize the style I use which is predominantly what Kernighan and Ritchie used in The C Programming Language, 2nd Edition along with some sage advice from Linux Kernel Coding Style by Linus Torvalds. Naturally, my style works well with my program. But nearly any C source file written in the K&R style should work.
I’ve corrected most of the problems I’ve discovered but there are a few limitations and requirements.
Functions should be written in the following general format:
int function(int x)
{
body of function
}
Data structures and array definitions (unless they fit on a single line) should have a format similar this:
struct {
int len;
char *str;
} *p;
Essentially, any line where an open brace is the first character of the next non-blank line will be processed as a function.
Conditional statements and for loops without an opening brace a the end of the line will be counted as a single line of code once and ending semi-colon is reached. Each of the following examples are seen as single lines of code:
if (condition)
action;
for (i=1; i < 10; i++)
if (condition)
action;
if (condition) {
action1;
action2;
}
else
action3;
In the last example, the if (condition), action1, and action2 are separate lines of code. The else action3 is seen as one line of code.
For now, I’m considering this to be acceptable behavior. In my mind, I can see each of those constructs as single logical lines of code. I can live with that.
I’ve tried several different things to force the program to see each line containing code as a line of code but so far none of them have worked. The code, as written, seems like it should recognize the above constructs as multiple code lines but it doesn’t. It’s been over 20 years since I wrote the original code so my understanding of how it works may not be as clear as it was then. For that matter, it’s possible that I may not have fully understood it then either.
My renewed interest in writing code is mostly for my own enjoyment and to keep my mind active. Around the time I dropped out of the Computer Science program, it had become tedious but that was probably because at that point, I was only taking one class at a time and it was difficult to recall when I’d taken the prerequisite classes, let alone what I might have learned in them.
I don’t think I ever really lost my interest in writing code. Since then I’ve worked with HTML and CSS, DOS batch files and Powershell, and, for the last couple of years, Bash scripts. Generally, I’ve done them in the pursuit of my own interests or to accomplish a particular task with no pressure to produce anything or meet anyone’s expectations.
Filed under: Linux, programming | Tagged: c, code, lines of code | 6 Comments »