how can I remove all the comments in my c program.

Discussion in 'Ruby' started by Vellingiri Arul, Sep 19, 2007.

  1. Dear Friends,
    using The Ruby how can I remove all the comments in my c program.
    For example I have mentioned c program ,that may be ruby program
    for your purpose you assume.
    I have tried to do using the regular expression.
    But I was unable to find the anser for this.

    Please any one tell me.

    Vellingiri Arul, Sep 19, 2007
    1. Advertisements

  2. The most simple regexen would be %r(//.*$) to match single line comments
    and %r(/\*.*?\*/)m to match multiline comments, but these will give you some
    false positives - for example, when you use // or /* inside a string.
    If you want something that works reliably, you would need to use a C-parser
    rather than regular expressions.

    Sebastian Hungerecker, Sep 19, 2007
    1. Advertisements

  3. Hai,
    very very thank you.
    I wil try to implement.

    Vellingiri Arul, Sep 19, 2007
  4. Vellingiri Arul

    On Web Guest

    Why would anyone want to remove the comments from a program?

    On Web, Sep 19, 2007
  5. Daniel Lucraft, Sep 19, 2007
    Marcin Raczkowski, Sep 19, 2007
  7. Vellingiri Arul

    ara.t.howard Guest

    i'd start with c's own preprocessor and go from there:

    cfp:~ > cat a.c
    /* comment be gone */
    main(){ printf("%i\n", (int)(0.55 * 100)); }

    cfp:~ > cpp a.c
    # 1 "a.c"
    # 1 "<built-in>"
    # 1 "<command line>"
    # 1 "a.c"

    main(){ printf("%i\n", (int)(0.55 * 100)); }

    cfp:~ > cpp -P a.c
    main(){ printf("%i\n", (int)(0.55 * 100)); }

    it already knows how to strip comments.

    a @
    ara.t.howard, Sep 19, 2007
  8. Can we have the name of the company / project please. I wish to add it
    to my list of things to avoid.
    Peter Hickman, Sep 19, 2007
  9. Vellingiri Arul

    Konrad Meyer Guest

    Content-Type: text/plain;
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    Quoth Marcin Raczkowski:
    Umm... That's illegal, by terms of the GPL. The GPL defines source code as
    the preferred form for modification. Intentionally obfuscated code isn't
    the preferred form for modification. So while it's technically "source
    code", it isn't according to the definition in the GPL, and thus they're
    violating the license by doing that.

    Konrad Meyer <>

    Content-Type: application/pgp-signature; name=signature.asc
    Content-Description: This is a digitally signed message part.

    Version: GnuPG v1.4.7 (GNU/Linux)

    -----END PGP SIGNATURE-----

    Konrad Meyer, Sep 19, 2007
  10. well, i had "pleasure" of having internship in company which policy was
    - use only MIT/BSD or LGPL as liblary, we won't pay for any licenses.
    If you HAVE to use GPL make sure your changes are not documented, and
    you use variable names like a,b,c and function names like fa fb etc.

    while it's illegal to obscure code that's covered by gpl, if you write
    code that extends it, you can argue it's your programming style, code
    like this even not technically obfuscated is useless.

    GPL can't force you to write documentation or make sefl-documenting code.
    Marcin Raczkowski, Sep 19, 2007
  11. Vellingiri Arul

    On Web Guest

    Not quite the same thing as taking out what's already there. I really hope
    the name of this company comes to light so they can get the negative
    response they deserve.

    On Web, Sep 20, 2007
  12. Vellingiri Arul

    Todd Benson Guest

    Please try to meet your helpers half way.


    Todd Benson, Sep 20, 2007
  13. Vellingiri Arul

    Chris Carter Guest

    Nowhere did the OP state that this is the reason. Someone else threw
    that out. There may be a legitimate reason for this.
    Chris Carter, Sep 20, 2007
  14. Vellingiri Arul

    Phlip Guest

    Worshipping comments is an intermediate, not advanced, behavior.

    Comments have plenty of legitimate reasons to disappear, not least of which
    is comments written only to satisfy some perverse mandated code rules.
    Phlip, Sep 20, 2007
  15. well, i guess this flamewar is becouse of me, so... it's of course
    possible that someone want to remove all the comments from code for some
    strange reason (i don't understand why tho), i might have biased view
    becouse of my internship ... but tell me seriously ...

    i don't understand this statement... but if it means what i think it
    means you are seriously wrong, comments are more important the better
    you are becouse more advenced programmers tend to use lots of idioms and
    write optimized but not easilly readable code
    i understand NOT writing comments, i do it myself often ... but if you
    already have them in C code ... then why remove them?
    if you compile code the'lll disapear anyway.. i doubt he works on
    project that's source code is larger then 10mb compresed (and source
    files compress with 90% ratio) so size is probably not the problem euther...

    Philip could you tell me one legitimate reason to remove comments from c
    Marcin Raczkowski, Sep 20, 2007
  16. Some programmers seem to think that comments are an excuse for badly
    written code. Well written code should be readable to a programmer
    proficient in the language in question, this means that the need for
    comments is reduced. People who insist on comments for everything tend
    not to be good programmers. People who do not write any comments are
    idiots no matter how advanced they are. There is always a need for
    comments, it just depends on the degree.

    As to idiomatic code, if you are an X programmer you should be familiar
    with the idioms, if not learn some more. Also what is called idiomatic
    depends on you background, ++x could be considered an idiom if your
    background was perhaps COBOL, FORTRAN or LISP. Do you really expect such
    code to be commented just in case someone isn't familiar with the 'idiom'?
    The comments might be wrong! Comments tend not to be updated with the
    same rigour as the code. It is sometimes better to throw the comments
    out and make the programmer read the code to determine what it is doing
    than be led astray by badly written comments. The code is the authority
    as to what the program is doing, the comments are just commentary and
    cannot affect the execution of the code. Of course the *best* course of
    action is to completely rewrite the comments but if you can't do that
    then I would recommend removing comments that serve only to confuse the
    Peter Hickman, Sep 20, 2007
  17. Vellingiri Arul

    Paul Knight Guest

    Perhaps the original poster is going to run some custom code metrics
    or code analysis, but wants to get rid of comments first.

    Perhaps the original poster is going to annotate the code in a
    different way, by surrounding it with prose, and the comments would
    obfuscate rather than illuminate.

    Perhaps it's for golfing.

    Perhaps it's for an art project. Or an illustration for an argument.

    Perhaps the original poster hasn't thought it all the way through.

    Perhaps it's something dark, sinister, and nefarious. But perhaps not.

    Perhaps the original poster could respond?

    Paul Knight, Sep 20, 2007
  18. Hello,
    What do you want?Why are you scolding me like that.
    If we are asking only doubts,If you know tell yes.otherwise you tell no.
    Don't speak nonsence others.

    Vellingiri Arul, Sep 20, 2007
  19. Vellingiri Arul

    Paul Knight Guest

    We're curious why you want to strip out comments, and whether it may
    or may not be a good idea. I wanted to suggest to several others that
    there are legitimate reasons, though a few of them might be slightly
    misguided. Perhaps if you explained why you want to get rid of the
    comments, it would clear up some of the messages in this thread.

    Paul Knight, Sep 20, 2007
  20. Hello Friends,
    I am also working in software company.
    You ask know why are removing the comments in your c program.
    For needness I want to remove all the comments and also I want to align
    all the program coding lines.
    Vellingiri Arul, Sep 20, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.