How to print comments in a given program?

2020-07-23 07:00发布

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?

标签: c
10条回答
Explosion°爆炸
2楼-- · 2020-07-23 07:31

Perhaps you can invert your questions. Why not use a string (which you can print) as a comment? The point of comments is that

  • They can hold pretty free-form text.
  • Humans can read it when looking at the source code.

String literals have both of these characteristics.

Thus

void a_function()
{
    docstring = "This string is a comment. Blah blah.";
    printf("DOC %s", docstring);
}

(Python has a very similar convention, and this is where I get the name "docstring").

查看更多
【Aperson】
3楼-- · 2020-07-23 07:34

This is an awk script that can extract comments out of your sourcecode file.

查看更多
ゆ 、 Hurt°
4楼-- · 2020-07-23 07:35

Here is a Java version which extracts single line and multiline comments (not tested exhaustively).

public void showComments(String fileName) {
    StringBuffer sb = new StringBuffer();
    FileReader br = null;
    try {
        br = new FileReader(fileName);

        int ch = -1;
        boolean singleLineComment = false;
        boolean multiLineComment = false;
        char prev = (char)ch;
        while ((ch = br.read()) != -1) {
            if (ch == '/' && prev == ch && !singleLineComment) {
                singleLineComment = true;
            } else if (ch == '*' && prev == '/' && !multiLineComment) {
                multiLineComment = true;
            } else if (ch == '\n' && singleLineComment) {
                System.out.println(sb.toString());
                singleLineComment = false;
                sb = new StringBuffer();
            } else if (ch == '/' && prev == '*' && multiLineComment) {
                System.out.println(sb.toString());
                multiLineComment = false;
                sb = new StringBuffer();
            } else if (multiLineComment || singleLineComment) {
                sb.append((char)ch);
            }
            prev = (char)ch;
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
查看更多
We Are One
5楼-- · 2020-07-23 07:36

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);
  }
}
查看更多
登录 后发表回答