Introduction
In this post, I am going to write a c program to extract comments from c program file. Basically, this is a file handling program. Here, we will read the file i.e. file1.c and display the comments in that c program which is written in file1.c.
Also Read: Program in C to Remove White Spaces and Comments from a File
Before going further, let us see the expected output.
Expected Output
Here, we will read the contents of the file and display all the comments from that file to the console.
Input file: file1.c
#include<stdio.h>
//This is a simple c program
int main()
{
int a = 5, b = 10;
/*Now we will display the values of these variables.
printf("a = %d and b = %d ", a, b);*/
return 0;
}
//End of the c program
As you can see in the above file, you can find two single line comments and one multi-line comment. We just need to display only the comment part. So, output will be

I hope, you have understood what we need to do. Let us see the logic of this program first.
Also Read: Switch Case in C Program to Calculate Area of Circle and Triangle
Logic to Extract Comments from C Program
Mainly, there are two types of comments i.e. single line comment (//) and multi-line comment (/* …….. */).
- We will read a file character by character.
- When we get first slash i.e. /, then we will search for another slash or asterisk (*).
- If above condition satisfied, then we will continue to write to the console until we get new line character for single line comment and ( */ ) for multi-line comment. This will show us that comment is over.
- In this program, I have used the variable flag. The value of this flag will be changed when we get first slash, second slash, new line character and asterisk followed by slash.
Also Read: C Program to Print Multiples of 5 using do-while loop
Let us see the actual c program.
C Program Extract Comments from C Program
#include <stdio.h> #include <stdlib.h> int main() { FILE *fp1; int flag=0; char ch; fp1=fopen("file1.c","r"); if(fp1==NULL) { printf("Error while opening a file for reading"); return 0; } while((ch=fgetc(fp1))!=EOF) { if((ch=='/')&&(flag==0)) { flag=1; continue; } else if((ch=='/')&&(flag==1)) { flag=2; continue; } else if((ch=='*')&&(flag==1)) { flag=3; continue; } if(flag==2) { if(ch=='\n') { flag=0; putchar('\n'); } else { putchar(ch); } continue; } if(flag==3) { if(ch=='*') { flag=4; } else { putchar(ch); } continue; } if(flag==4) { if(ch=='/') { flag=0; putchar('\n'); } else { putchar(ch); } continue; } } fclose(fp1); return 0; }
The variable ch will have the character value read from the file1.c. The value of flag will be 1, 2, 3 and 4 according to the value of ch. This is necessary because we will come to know the previous value of the variable ch. Single slash is not useful for us.
I hope you have understood the program. If you have any difficulty then please feel free to contact me. Thank you.
Become a Patron!Some Important C Programs
- Program in C to Find Longest Line in a File
- Palindrome in C using Pointers
- Insert and Delete element in Array in C using switch case
- C Program to Add Alternate Elements of a 2D Array
- Arrays in C for Complete Beginners
- C Program to Find Area of a Circle using Preprocessor
- Program in C to Remove White Spaces and Comments from a File
- C Program to Print Numbers Except Multiples of n
- Reverse a Number using getchar and putchar function in c
- The while loop in C Programming