Thread: GCC not able to distinguish comments inside string

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    224

    GCC not able to distinguish comments inside string

    I'm writing some C code that output large tables into a file. I am also outputting C-style comments. Here's the problem I'm having:
    Code:
    int main()
    {
     int a;
    
    /*
     printf("/* */\n");
    */
    
     return 0;
    }
    But this gives me the following errors:
    tmp.c: In function `main':
    tmp.c:6: error: stray '\' in program
    tmp.c:6:17: missing terminating " character
    tmp.c:7: error: `n' undeclared (first use in this function)
    tmp.c:7: error: (Each undeclared identifier is reported only once
    tmp.c:7: error: for each function it appears in.)
    tmp.c:7: error: parse error before string constant

    Is there a way around this? I don't want to output C++ style comments. This problem persists even in PHP.

    Thanks,
    Yasir

  2. #2
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    If you put spaces between / and * in the printf string, the problem is solved. Like
    Code:
    int main()
    {
     int a;
    
    /*
     printf("/ * * /\n");
    */
    
     return 0;
    }
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Yasir_Malik
    I'm writing some C code that output large tables into a file. I am also outputting C-style comments. Here's the problem I'm having:

    Thanks,
    Yasir
    In this case, the problem is with the C preprocessor not recognizing that the comment is within a string.

    Maybe you could do something like this:

    Code:
    #include <stdio.h>
    
    int main()
    {
     int a;
    
    #if 0
     printf("/* A commented-out comment */\n");
    #endif
    
     printf("/* A comment in a string */\n");
    
     return 0;
    }
    I use the "#if 0" thing all of the time to comment out blocks of code that have comments in them.



    Regards,

    Dave
    Last edited by Dave Evans; 06-19-2005 at 10:17 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    If you're going to be commenting stuff a lot, then try this
    Code:
    #define BC  "/*"
    #define EC  "*/"
    
    int main ( void ) {
      int a;
    /*
      printf( BC " a comment " EC "\n" );
    */
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    What Dave Evans suggests is what I've done before, but I was looking for something that I could use in PHP as well. PHP does not (I think) have a preprocessor or defines. But what Salem suggested is also a good solution. An IRIX compiler does the same thing (although the error messages are a lot better than GCC).
    Last edited by Yasir_Malik; 06-19-2005 at 03:53 PM.

  6. #6
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Quote Originally Posted by Dave Evans
    Code:
    #if 0
     printf("/* A commented-out comment */\n");
    #endif
    Excellent! I've been needing something like this for awhile. Especially for blocking out functions I don't need at the time without the compiler warning me that I'm not using the function. (but of course I would use later.)

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Just use lots of statements:

    Code:
    printf("/");
    printf("* *");
    printf("/");
    That should work.

    dwk

  8. #8
    .
    Join Date
    Nov 2003
    Posts
    307
    Quote Originally Posted by Kleid-0
    Excellent! I've been needing something like this for awhile. Especially for blocking out functions I don't need at the time without the compiler warning me that I'm not using the function. (but of course I would use later.)
    A word of warning.

    When you maintain code like this - and the #if #endif pair are spread far apart or there are loads of them, it becomes REALLY confusing to read, and therefore hard to change correctly.

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by dwks
    Just use lots of statements:

    Code:
    printf("/");
    printf("* *");
    printf("/");
    That should work.

    dwk
    3 function calls much overhead...


    Quote Originally Posted by Yasir_Malik
    I'm writing some C code that output large tables into a file. I am also outputting C-style comments. Here's the problem I'm having:
    Code:
    int main()
    {
     int a;
    
    /*
     printf("/* */\n");
    */
    
     return 0;
    }
    But this gives me the following errors:
    tmp.c: In function `main':
    tmp.c:6: error: stray '\' in program
    tmp.c:6:17: missing terminating " character
    tmp.c:7: error: `n' undeclared (first use in this function)
    tmp.c:7: error: (Each undeclared identifier is reported only once
    tmp.c:7: error: for each function it appears in.)
    tmp.c:7: error: parse error before string constant

    Is there a way around this? I don't want to output C++ style comments. This problem persists even in PHP.

    Thanks,
    Yasir
    your having those errors because the comented code is between the first /* and first */

    so your precompiled source will be
    Code:
    int main()
    {
     int a;
    
     
                  \n");
    */
    
     return 0;
    }
    which obviously is syntaticly incorrect
    removing the comment arround the printf will do
    still if you want to keep the comment around the printf use
    Code:
    printf("/""* *""/");
    Last edited by xErath; 06-21-2005 at 10:41 PM.

  10. #10
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    Of course that's the case. But shouldn't the C-language be defined to handle cases like this? GCC and IRIX's native compiler gave the same results.

  11. #11
    Banned
    Join Date
    Jun 2005
    Posts
    594
    if its just one line comments why not

    Code:
    printf("// My Commented String");

    if its multistring

    Code:
    printf("/* My reallly long string that");
    printf("has no point what so ever but");
    print("who care its an example. */");

  12. #12
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    The reason you had trouble - and any compiler would - is that you started the comment with /* so the compiler never knew you then started a string with " and didn't want the */ in your string recognized. Then you started a string with your closing " and the rest of the code went blooie. End program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM