what's the difference between the two kinds comment syntaxs?

Discussion in 'C++' started by cdrsir, Jul 7, 2006.

  1. cdrsir

    cdrsir Guest

    we can use
    1) // my comments a
    2) /* my comments b */
    when we want to add some comments.

    I know some compilers just support the 2nd syntax, but normally both of
    these 2 syntaxs are supported by most of the compilers. But what are
    the difference for those two syntaxs in the sense for the compiler, if
    it support both. Is one of them will be processed faster?

    Another question, if I write comments in NON-Alfa-Beta languages, like
    Chinese, will it cause the compiler to use more time?

    thanks
    cdrsir, Jul 7, 2006
    #1
    1. Advertising

  2. cdrsir <> wrote:
    > we can use
    > 1) // my comments a


    This type of comment will automatically end by the end of the line.

    > 2) /* my comments b */


    This one will end only when */ is found. This implies that you can
    comment multiple lines using just /* and */ instead of having to start
    each line with //.

    hth
    --
    jb

    (reply address in rot13, unscramble first)
    Jakob Bieling, Jul 7, 2006
    #2
    1. Advertising

  3. cdrsir

    Rolf Magnus Guest

    cdrsir wrote:

    > we can use
    > 1) // my comments a
    > 2) /* my comments b */
    > when we want to add some comments.
    >
    > I know some compilers just support the 2nd syntax,


    Well, if you find one that doesn't support the first syntax, throw that
    compiler away.

    > but normally both of these 2 syntaxs are supported by most of the
    > compilers. But what are the difference for those two syntaxs in the sense
    > for the compiler, if it support both. Is one of them will be processed
    > faster?


    No. One is for single-line comments, the other for multi-line comments.
    That's all there is about it.

    > Another question, if I write comments in NON-Alfa-Beta languages, like
    > Chinese, will it cause the compiler to use more time?


    Why are you concerned about compile time? The lexical analyzer is probably
    the part that takes the smallest percentage of the total execution time of
    the compiler. And for throwing away characters, it doesn't really matter
    much what those characters are.
    If you really want to speed up compilation, use precompiled headers if the
    compiler supports it. Another way is to switch off optimizations, since the
    optimizer is probably the part taking the majority of the compile time.
    However, your program will of course be less optimal.
    Rolf Magnus, Jul 7, 2006
    #3
  4. cdrsir

    Jim Langston Guest

    "cdrsir" <> wrote in message
    news:...
    > we can use
    > 1) // my comments a
    > 2) /* my comments b */
    > when we want to add some comments.
    >
    > I know some compilers just support the 2nd syntax, but normally both of
    > these 2 syntaxs are supported by most of the compilers. But what are
    > the difference for those two syntaxs in the sense for the compiler, if
    > it support both. Is one of them will be processed faster?
    >
    > Another question, if I write comments in NON-Alfa-Beta languages, like
    > Chinese, will it cause the compiler to use more time?
    >
    > thanks


    // is for single line coments and comments everything til the end of hte
    line.

    /* */ is for multi line comments and /* comments eveyrthing until it finds
    another */ no matter how far away that is.

    I alwasy use // comments to comment code.

    When I temporarily comment out a block of code (for testing or such) I use
    /* */ one reason so I can find it again easier. If I searched for // I
    would find way too many lines, but since I don't use /* */ for "normal"
    comments, it will only exist where I used to to comment out a block of code.
    Jim Langston, Jul 7, 2006
    #4
  5. cdrsir

    Gavin Deane Guest

    Jakob Bieling wrote:
    > cdrsir <> wrote:
    > > we can use
    > > 1) // my comments a

    >
    > This type of comment will automatically end by the end of the line.
    >
    > > 2) /* my comments b */

    >
    > This one will end only when */ is found. This implies that you can
    > comment multiple lines using just /* and */ instead of having to start
    > each line with //.


    You can also use /* */ to comment out part of a line should you wish
    to.

    void foo(int param1, int param2, /* int param3 */);

    /* */ comments aren't clever enough to nest so you can get caught out
    sometimes if you try to use them to temporarily remove a chunk of code,
    for example during debugging, if that chunk of code already contains /*
    */ comments. For example, I might have this function somewhere in my
    code.

    void foo()
    {
    // code here

    /*
    Here is a multi-line comment
    to help the reader understand what is going on
    */

    // more code here
    }

    Now suppose I'm debugging. I want to simplify the code to track down
    the problem. One of the things I need to do is temporarily comment out
    the entire body of foo so it can be eliminated from my investigation.
    The easiest way to do that is to add /* immediately after the opening
    brace at the start of the function and add */ immediately before the
    closing brace at the end of the function. Except with m multi-line
    comment in there that won't work. The /* */ comment will end at the end
    of the multi-line comment. The later part of the foo function (where I
    wrote "more code here") will still be there not commented out. And the
    */ immediately before the closing brace of foo will be a simple syntax
    error.

    For this reason I personally never use /* */ for comments that I intend
    to keep.

    Gavin Deane
    Gavin Deane, Jul 7, 2006
    #5
  6. cdrsir wrote:
    > we can use
    > 1) // my comments a
    > 2) /* my comments b */
    > when we want to add some comments.
    >


    There is a third way - you can also use #if 0 if you need to comment
    out a block of code, thus:

    #if 0
    void dont_want_this_any_more()
    { ... }
    #endif

    This is useful if you want to avoid /* .. */ nesting problems.
    tragomaskhalos, Jul 7, 2006
    #6
  7. cdrsir

    Bo Persson Guest

    "cdrsir" <> skrev i meddelandet
    news:...
    > we can use
    > 1) // my comments a
    > 2) /* my comments b */
    > when we want to add some comments.
    >
    > I know some compilers just support the 2nd syntax,


    That's old C compilers.

    > but normally both of
    > these 2 syntaxs are supported by most of the compilers. But what are
    > the difference for those two syntaxs in the sense for the compiler,
    > if
    > it support both. Is one of them will be processed faster?


    The difference is that version 2 is from the original C language.
    Style 1 was added to C++, and later to C as well.

    >
    > Another question, if I write comments in NON-Alfa-Beta languages,
    > like
    > Chinese, will it cause the compiler to use more time?


    It might confuse some compilers, not to mention non-chinese
    programmers. :)




    Bo Persson
    Bo Persson, Jul 7, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page