Pages: [1]   Go Down
Author Topic: Compilers goes crazy when you have "\\" inside a comment  (Read 1746 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There's a problem when I put two \\ (not comment slashes, the opposite ones...) at the end of a comment.

It seems that compiler "reads" a multi line comment or something like that, and report errors that disappear if you delete them.

I think it's a compiler bug.
Logged

Offline Offline
Edison Member
*
Karma: 43
Posts: 1552
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think inside a comment the backslash doesn't have its special meaning as the escape character except the last one on a line. The first backslash is therefore ignored - i.e. it isn't read as an escape for the following backslash. The second backslash then gets read as the line continuation. The compiler issues a warning:
Code:
sketch_sep16a.cpp:8:1: warning: multi-line comment

Pete
Logged

Where are the Nick Gammons of yesteryear?

Chile
Offline Offline
Edison Member
*
Karma: 35
Posts: 1251
Arduino rocks?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

There's a problem when I put two \\ (not comment slashes, the opposite ones...) at the end of a comment.

It seems that compiler "reads" a multi line comment or something like that, and report errors that disappear if you delete them.

I think it's a compiler bug.

http://arduino.cc/forum/index.php/topic,118440.0.html can you test the updated compiler? I don't get any error at all.
« Last Edit: September 17, 2012, 01:48:45 am by eried » Logged

My website: http://ried.cl

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 124
Posts: 6637
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The second backslash then gets read as the line continuation.
This sounds right.  And if there is "important stuff" on the next line, it will become part of the comment instead of being compiled properly, possibly leading to a cascade of errors.  This would be especially bad:
Code:
//  bad comment \\
#include <Ethernet.h>
#include <other.h>
Logged

Dubuque, Iowa, USA
Offline Offline
Edison Member
*
Karma: 44
Posts: 2460
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Like westfw said:

Code:
root@zygot:/tmp# cat helloworld.c
#include <stdio.h>

// Comment \\
int main() {
  printf("Hello World!\n");
  return(0);
}
root@zygot:/tmp# gcc -Wall helloworld.c
helloworld.c:3:1: warning: multi-line comment
helloworld.c:5: error: expected declaration specifiers or ‘...’ before string constant
helloworld.c:5: warning: data definition has no type or storage class
helloworld.c:5: warning: type defaults to ‘int’ in declaration of ‘printf’
helloworld.c:5: error: conflicting types for ‘printf’
helloworld.c:5: note: a parameter list with an ellipsis can’t match an empty parameter name list declaration
helloworld.c:6: error: expected identifier or ‘(’ before ‘return’
helloworld.c:7: error: expected identifier or ‘(’ before ‘}’ token
Logged

Leeds, UK
Online Online
Edison Member
*
Karma: 78
Posts: 1719
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It isn't particularly the "\\", but rather just the single \ at the end of the comment. It isn't a bug with the compiler at all, in fact it is part of the c language specification to allow you to wrap comments onto the next line.

It works with #define statements and comments:

Code:
#define hello\
this is still\
part of the\
define above
This line is not

//This is a multi\
line comment\
which can also\
be written as:

/*
This is a multi
line comment
*/
Logged

~Tom~

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 124
Posts: 6637
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
to allow you to wrap comments onto the next line.
Wrapping comments is useless;  you might as well have used /* */ or another comment line.
the backslash continuation feature was for code (where it's still not very useful, since newlines are largely irrelevant in C) and strings (where it's ugly and error prone, and string catenation should be replacing it.)
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ooooppss...
You're write..!

Excuse me... I have 20 years of software development... but in Turbo Pascal and in the last years in Visual Basic!
I didn't know the C++ comment continuation...

Thanks everyone..|
Logged

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 53
Posts: 1832
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

As others have noted, this is part of the C and C++ language standards.  In the C99 standard, it is covered in section 5.1.1.2 (translation phases):

Each instance of a backslash character (\) immediately followed by a new-line character is deleted, splicing physical source lines to form logical source lines. If, as a result, a character sequence that matches the syntax of a universal character name is produced, the behavior is undefined. Only the last backslash on any physical source line shall be eligible for being part of such a splice. A source file that is not empty shall end in a new-line character, which shall not be immediately preceded by a backslash character before any such splicing takes place.

Logged

Pages: [1]   Go Up
Jump to: