#13101 - yaustar - Fri Dec 05, 2003 4:36 pm
We found an interesting bug/glitch/something within c++
Code: |
//--------------hello world---------------\\
if(something == something else)
{
}
|
Somehow the comment caused the compilar to ignore the if statement on the next line. However:
Code: |
//--------------hello world---------------\\
if(something == something else)
{
}
|
This worked. Can anyone explain why?
_________________
[Blog] [Portfolio]
#13102 - Dwedit - Fri Dec 05, 2003 4:47 pm
The Backslash + newline combo is an escape character for nothing, used to continue long lines. Just like \n, or \t, there's also \(newline).
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."
#13112 - Miked0801 - Fri Dec 05, 2003 7:25 pm
I don't know that the last post was clear enough in meaning:
Placing a backslash on the end of the line tells the compiler to ignore the carriage return on the current line and assume that the following line is part of the statement. Backslashes are most often used in macro definitions where all statements must be on one line. So in essence, placing the backslashes at the end of the line tells the compiler that the following line is part of the commented statement and should be ignored. I don't know if the is a compiler bug or not, but I'd avoid using backslashes in C code for decorations anyways just to be sure.
Mike
#13116 - sgeos - Fri Dec 05, 2003 8:01 pm
Miked0801 wrote: |
Placing a backslash on the end of the line tells the compiler to ignore the carriage return on the current line and assume that the following line is part of the statement. *SNIP* |
I did not know that. I was under the impression that backslash was used to continue macros. I'd assume that:
Code: |
#define ADD(a,b) \
((a) + (b))
|
Would not work if the backslash was followed by a space? Worse yet, I'd assume that something stupid like this would not work:
Code: |
#define SUB(a,b) \ /* Useless macro */
((a)[(b)])
|
#13119 - torne - Fri Dec 05, 2003 8:26 pm
sgeos wrote: |
I did not know that. I was under the impression that backslash was used to continue macros. |
No, it works in any context at all, including inside a literal string.
Quote: |
I'd assume that:
Code: | #define ADD(a,b) \
((a) + (b))
|
Would not work if the backslash was followed by a space? |
It works in GCC, but emits a warning as it does not work on all preprocessors/compilers. (GCC ignores any amount of trailing whitespace after a backslash IF there is nothing else on the line)
Quote: |
Worse yet, I'd assume that something stupid like this would not work:
Code: | #define SUB(a,b) \ /* Useless macro */
((a)[(b)])
|
|
No, that doesn't work. The \ is treated as escaping a space (to be.. a space. Wow), the comment is parsed as a comment, and the next line is parsed as a statement.
#13121 - sgeos - Fri Dec 05, 2003 8:38 pm
torne wrote: |
sgeos wrote: | I did not know that. I was under the impression that backslash was used to continue macros. |
No, it works in any context at all, including inside a literal string. |
That is interesting and wacky. I'll still think I'll split strings like this:
Code: |
mvprintw(0, 0,
"What you say?\n"
"*All* you %d base?!?!\n",
base
); |
-Brendan
#13126 - Miked0801 - Fri Dec 05, 2003 11:17 pm
Which is why when you place backslashes in strings, you must escape them - that is place 2 backslashes next to each other to get one in the string. On DOS/WinDoze systems, in include statements, it's very common to see this (if you're too lazy to set your search directories correctly):
#include "c:\\agb\\devkit\\foo.h"
Forget the second slash and you get all sorts of interesting characters :)
Again, don't use backslashes unless you absolutely have to. They cause all sorts of hard to track behavior if used incorrectly...
#13139 - sgeos - Sat Dec 06, 2003 2:56 am
Miked0801 wrote: |
Again, don't use backslashes unless you absolutely have to. They cause all sorts of hard to track behavior if used incorrectly... |
I find it bothersome that shift JIS string literals typed into source code sometimes contain "hidden" backslashes. I suppose any non-ascii text really should be converted to hex.
-Brendan
#13143 - tepples - Sat Dec 06, 2003 4:13 am
Miked0801 wrote: |
that is place 2 backslashes next to each other to get one in the string. On DOS/WinDoze systems, in include statements, it's very common to see this (if you're too lazy to set your search directories correctly):
#include "c:\\agb\\devkit\\foo.h" |
Or you can use forward-slashes, which do work as path separators in many DOS and Win32 console apps, including just about anything that isn't trying to be bug-compatible with an old DOS 2.x/3.x app:
Code: |
#include "e:/gbadev/asprite/pin8gba.h" |
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.