I want to print the comment to a given program to be printed in console?
Is there any function and any own logic to get print the comment in a program?
e.g.
int main()
{
/* this is a comment */
}
the above program has only one comment i want to print this comment to console by a certain logic or any function ,if there is in C?
You need to write a program that reads c source files, and identifies and prints comments within these files.
#include <stdio.h>
void read_until_char (FILE *f, char c) {
// read file until c is found
}
void read_until_char_into (FILE *f, char c, char *buf) {
// read file until c is found
// also write each char read into buffer
}
void print_comments(const char *filename) {
FILE *f = fopen(filename, "r");
char buffer[2048];
int c;
if (f) {
while (!feof(f)) {
read_until_char(f, '/');
c = fgetc(f);
if (c == '*') {
while (!feof(f)) {
read_until_char_into(f, '*', buffer);
if ((c=fgetc(f)) == '/') {
// print buffer
break;
} else if (c == '*') {
// consider what to do if c is *
}
}
} else if (c == '/') {
read_until_char_into(f, '\n', buffer);
// print buffer
}
}
fclose(f);
}
}
A compiled C program without access to its own source code will not be able to print the comments. The comments are removed by the C preprocessor, and never even seen by the compiler. In other words, the comment text is not available once the program is executable.
This is why your approach is going to have to be to somehow process the source code directly, you can't do this at runtime for the program itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With