C Program to Count No of Lines, Blank Lines, and Comments in the Program

This C program counts the number of lines, blank lines, and comments in a given program file. It reads the file line by line and analyzes each line to determine its type.

Problem Statement

You are required to write a C program that reads a program file and counts the number of lines, blank lines, and comments present in the file. The program should perform the following tasks:

  1. Prompt the user to enter the name of the program file.
  2. Open the program file for reading.
  3. Read the file line by line and analyze each line to determine its type:
    • If a line is empty (contains no characters), it should be considered a blank line.
    • If a line contains a single-line comment (starts with “//”), it should be counted as a comment line.
    • If a line contains the start of a multi-line comment (“/*”), it should be counted as a comment line.
    • If a line is inside a multi-line comment, it should also be counted as a comment line.
    • If a line contains the end of a multi-line comment (“*/”), it should mark the end of the comment.
  4. After processing all the lines in the file, close the file.
  5. Display the following information to the user:
    • The total number of lines in the program file.
    • The number of blank lines found.
    • The number of comment lines found.

Ensure that your program handles any errors that may occur, such as being unable to open the file or encountering invalid input.

C Program to Count No of Lines, Blank Lines, and Comments in the Program

#include <stdio.h>
#include <string.h>

int countLines(char program[]) {
    FILE *file = fopen(program, "r");
    if (file == NULL) {
        printf("Unable to open the file.\n");
        return -1;
    }

    char line[200];
    int totalLines = 0;
    int blankLines = 0;
    int commentLines = 0;
    int inComment = 0;

    while (fgets(line, sizeof(line), file)) {
        totalLines++;

        // Check for blank line
        if (strlen(line) == 1) {
            blankLines++;
            continue;
        }

        // Check for comment line
        if (inComment) {
            commentLines++;
            if (strstr(line, "*/") != NULL) {
                inComment = 0;
            }
            continue;
        }

        if (strstr(line, "//") != NULL) {
            commentLines++;
            continue;
        }

        if (strstr(line, "/*") != NULL) {
            commentLines++;
            inComment = 1;
        }
    }

    fclose(file);

    printf("Total lines: %d\n", totalLines);
    printf("Blank lines: %d\n", blankLines);
    printf("Comment lines: %d\n", commentLines);

    return 0;
}

int main() {
    char program[100];

    printf("Enter the name of the program file: ");
    scanf("%s", program);

    countLines(program);

    return 0;
}

How it Works

  1. The program includes the necessary header files stdio.h and string.h.
  2. The countLines function is defined to perform the counting of lines, blank lines, and comments. It takes the name of the program file as input.
  3. Inside the countLines function:
    • It opens the program file using fopen in read mode and checks if the file exists and can be opened.
    • It initializes variables to track the total number of lines (totalLines), blank lines (blankLines), comment lines (commentLines), and a flag to indicate if the program is currently inside a multi-line comment (inComment).
    • Using a loop, it reads each line from the file using fgets.
    • For each line, it performs the following checks:
      • If the line is empty (i.e., its length is 1), it increments blankLines and continues to the next line.
      • If the program is inside a multi-line comment (inComment is 1), it increments commentLines and checks if the line contains the closing comment tag “*/”. If found, it sets inComment to 0.
      • If the line contains a single-line comment (“//”), it increments commentLines and continues to the next line.
      • If the line contains the start of a multi-line comment (“/*”), it increments commentLines and sets inComment to 1.
      • If none of the above conditions are met, it continues to the next line without incrementing any counts.
    • After processing all lines, it closes the file and displays the counts of total lines, blank lines, and comment lines.
  4. The main function:
    • Declares a character array program to store the name of the program file.
    • Prompts the user to enter the name of the program file.
    • Reads the input using scanf.
    • Calls the countLines function, passing the name of the program file as an argument.
  5. The program execution ends, and the results are displayed.