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条回答
聊天终结者
2楼-- · 2020-07-23 07:11

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;
    }
查看更多
smile是对你的礼貌
3楼-- · 2020-07-23 07:17

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.

查看更多
狗以群分
4楼-- · 2020-07-23 07:18

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.

查看更多
Luminary・发光体
5楼-- · 2020-07-23 07:18

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.

查看更多
小情绪 Triste *
6楼-- · 2020-07-23 07:19

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 */

查看更多
老娘就宠你
7楼-- · 2020-07-23 07:20

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)

查看更多
登录 后发表回答