Get Time
forums   
Search | Watch Thread  |  My Post History  |  My Watches  |  User Settings
View: Flat (newest first)  | Threaded  | Tree
Previous Thread  |  Next Thread
Be careful with comments in C | Reply
I just wasted a long time trying to find a bug in my C program. My code was something like:

int a=1;
//some comment \
a++;
printf("%d\n",a);


The output was always 1 and I couldn't figure out why. It turns out that the a++; is part of the comment due to the '\' escape character, so it never gets executed. The really annoying thing is that my editor (Kate) didn't show it as a comment, so it was really hard to find. Interestingly in Java this problem doesn't happen. I hope this post saves people from a similar problem.
Re: Be careful with comments in C (response to post by dimkadimon) | Reply
Another annoying thing is that I can't have a smaller comment inside a larger comment, something like:

/*
text
/*
more text
*/
*/ 


In the above I want everything to be commented out.
Re: Be careful with comments in C (response to post by dimkadimon) | Reply
A workaround is to use #if 0 if you need nested comments.
Re: Be careful with comments in C (response to post by dimkadimon) | Reply
Quick note on which popular editors show this as a comment :
vim - yes
emacs - no :)
kate - no
gedit - yes
netbeans - yes
kwrite - yes
Re: Be careful with comments in C (response to post by nadeem) | Reply
But, but, but... Emacs was supposed never to give me up, let me down or run around and desert me!
Re: Be careful with comments in C (response to post by nadeem) | Reply
I'm waiting for a bunch of emacs fanboys saying that you can program it to take that \ into consideration and that it is so much better this way, because you are in control ;-)
Re: Be careful with comments in C (response to post by dimkadimon) | Reply
Obviously, someone at TopCoder needs to fix the forums' syntax highlighting.
Re: Be careful with comments in C (response to post by dimkadimon) | Reply
I use emacs, and it doesn`t highlight too, but I always compile with -Wall

g++ -Wall -g -ggdb teste.cpp -o teste
teste.cpp:7:3: warning: multi-line comment

which avoid this problem
Re: Be careful with comments in C (response to post by dimkadimon) | Reply
\ at the end of line is the way to glue lines together in C or C++.

For example, this is valid program :-D :
i\
n\
t\
 \
m\
a\
i\
n\
(\
)\
{\
r\
e\
t\
u\
r\
n\
 \
0\
;\
}


I wander what editor can properly highlight that? And should it?
Re: Be careful with comments in C (response to post by dimkadimon) | Reply
Also trigraph ??/ works instead of backslash.

http://stackoverflow.com/questions/2384276/backslash-in-the-end-of-comment-lines-in-c-c
Re: Be careful with comments in C (response to post by kit1980) | Reply
I think every decent editor should be able to highlight that. I don't see why it is so hard to do?
Re: Be careful with comments in C (response to post by kit1980) | Reply
BTW, Microsoft was not issuing warning for usage of trigraph in sources for very long time, until was requested to do so.

https://connect.microsoft.com/VisualStudio/feedback/details/101048/compiling-source-code-with-trigraph-sequences-must-result-in-level-4-warning
RSS