kTiwari
kTiwari

Reputation: 1508

How to print comments in a given program?

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?

Upvotes: 2

Views: 6844

Answers (10)

Usman
Usman

Reputation: 2029

Try this code !

Code :

#include <stdio.h>
int main()
{

   char comment[25] ="/* this is a comment */";
   printf("%s",comment);
    return 0;
}

Output :

/* this is a comment */

Upvotes: -1

Priom Sarkar
Priom Sarkar

Reputation: 342

This code can handle single line comment.

  while((scanf("%c",&s[i]))!=EOF)
        {
            b[i]=s[i];
           if(s[i]=='\n')
            {
                for(j=k; j<=i; j++)
                {
                    if(s[j]=='/')
                    {
                        if(s[j+1]=='/')
                        {
                            temp=1;

                        }
                        else if(s[j+1]=='*')
                        {
                            for(j=j+2; j<=i; j++)
                            {
                                if(s[j]=='*')
                                {
                                    if(s[j+1]=='/')
                                    {
                                        temp=1;
                                    }
                                }
                            }
                        }
                        printf("%c",s[j]);
                        k=j;
                    }
                }
                printf("===");
                if(temp==1)
                {

                    printf("This Line has been commented :( \n");
                    temp=0;
                }
                else
                {
                    printf("this line is  code :) \n");
                }
            }

            i++;

        }
        return 0;
    }

Upvotes: 0

mani bhushan
mani bhushan

Reputation: 3

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();
    }
}

Upvotes: 0

Keith Miller
Keith Miller

Reputation: 1347

I'm very confused about this question, if you want to print every comment why not just use a string instead of a comment? Or both? Why do you need to print the comments of your program? Just wondering because I'm not sure what you're trying to do here, and perhaps there is a better way to achieve what you are trying to do.

Does printf("/*foo*/"); not work or something? Or are you trying to make this automatic?

If it's the latter then you will need to have your program parse your source file and search for comments (unless there is some other easier magical way).

I suggest you just read the C file char by char until you encounter a /* and start populating an array with each char until the next occurrence of */.

If for whatever reason the problem you are having is the former, and you can't for some reason print a comment with printf here's a function I wrote to make a comment string (very pointless since I'm 99% sure you can just use printf("/*foo*/") .

int printcomment(const char *comment)
{
    char *commentln, open[] = "/*", close[] = "*/";
    if(!(commentln = malloc(strlen(comment) + 5))) return 0;
    strcat(commentln, open);
    strcat(commentln, comment);
    strcat(commentln, close);
    printf("%s", commentln);
    free(commentln);
    return 1;
}

Good luck.

Upvotes: 0

Adrian Ratnapala
Adrian Ratnapala

Reputation: 5693

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").

Upvotes: 1

Dmitry Zagorulkin
Dmitry Zagorulkin

Reputation: 8548

It's a sort of program called a Quine

C example:

main(){char *c="main(){char *c=%c%s%c;printf(c,34,c,34);}";printf(c,3
4,c,34);}

http://en.wikipedia.org/wiki/Quine_(computing)

Upvotes: 0

perreal
perreal

Reputation: 97948

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);
  }
}

Upvotes: 8

Benoit Thiery
Benoit Thiery

Reputation: 6387

Refer to How can I delete all /* */ comments from a C source file? and then make a diff between the generated file and the original file.

Upvotes: 0

unwind
unwind

Reputation: 399843

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.

Upvotes: 4

halex
halex

Reputation: 16403

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

Upvotes: 1

Related Questions