Extract Comments from C Program


This article will guide you through a C program designed to extract comments from another C program.

In the world of programming, comments play a crucial role in making the code more understandable and maintainable.

Also Read: C Program To Read Two Files Simultaneously

Comments provide valuable insights into the code logic, purpose, and functionality.

However, there are scenarios where extracting comments from a C program becomes necessary.

We will explore the implementation, step-by-step process, and the significance of extracting comments. So, let’s dive in!

Also Read: Armstrong Number in C Programming

Why Extract Comments from a C Program?

Comments serve as a documentation tool for programmers.

They explain the code’s intention, add clarifications, and make it easier for others to comprehend the logic.

Also Read: C Program to Find the Inverse of 2×2 Matrix

Extracting comments can be beneficial for various reasons:

  1. Documentation and Understanding: Extracted comments can be utilized to generate technical documentation, aiding developers in understanding the codebase efficiently.
  2. Code Analysis and Review: Extracted comments can be used for code review processes, allowing other developers to assess the code’s quality and suggest improvements.
  3. Refactoring and Maintenance: Extracted comments assist in identifying sections of code that require refactoring or modification, enhancing the overall maintainability of the code.
  4. Legacy Code Inspection: Extracting comments from legacy code helps in comprehending the functionality of the codebase and facilitates the migration or enhancement process.

Implementation of the C Program

The C program we are going to discuss implements a simple algorithm to extract comments from another C program.

Also Read: C Program to Copy the Contents of One File into Another File

Let’s take a look at the step-by-step process:

Include the Required Headers:

#include <stdio.h>
#include <stdlib.h>

Define the Function to Extract Comments:

void extractComments(FILE *sourceFile) {
    int ch, prev_ch;
    while ((ch = fgetc(sourceFile)) != EOF) {
        if (ch == '/') {
            prev_ch = ch;
            ch = fgetc(sourceFile);
            if (ch == '/') {
                printf("Comment: ");
                while ((ch = fgetc(sourceFile)) != '\n' && ch != EOF) {
            else if (ch == '*') {
                printf("Comment: ");
                while ((ch = fgetc(sourceFile)) != EOF) {
                    if (prev_ch == '*' && ch == '/')
                    prev_ch = ch;

Open the Source File and Extract Comments:

int main() {
    FILE *sourceFile = fopen("source.c", "r");
    if (sourceFile == NULL) {
        printf("Failed to open the file.\n");
        return 1;
    return 0;

Also Read: Getchar and Putchar Function in C with Example

Frequently Asked Questions

Q: What is the purpose of the C program to extract comments?

The C program to extract comments is designed to automate the process of extracting comments from another C program. It helps in code analysis, documentation, and codebase understanding.

Q: Can the C program handle nested comments?

No, the provided C program doesn’t handle nested comments. It extracts single-line (//) and multi-line (/* ... */) comments, but it doesn’t account for comments within comments.

Q: How can I modify the program to handle nested comments?

To handle nested comments, you would need to implement a more sophisticated parsing algorithm. It would involve tracking the nesting level of comments and handling each level accordingly.

Q: Are there any libraries or tools available to extract comments from C programs?

Yes, several libraries and tools are available for comment extraction in various programming languages, including C. Some popular ones include Doxygen, Understand, and SourceTrail.

Q: Can the extracted comments be saved to a file instead of printing them on the console?

Yes, you can modify the extractComments function to write the extracted comments to a file instead of printing them on the console. You would need to open a new file for writing and use the fprintf function to write the comments to the file.


Extracting comments from a C program is a valuable process for understanding, documenting, and reviewing code.

Also Read: 25 Tricky Questions on Pointers in C: Explained and Answered

The provided C program offers a simple yet effective solution for extracting comments. By incorporating this program into your development workflow, you can enhance code comprehension, facilitate maintenance, and improve collaboration among developers.

Remember to handle nested comments and explore additional tools if your requirements demand more advanced comment extraction capabilities.