For me, Visual Studio's Ctrl + K, Ctrl + C keyboard shortcut is used to comment-out the selected lines. When editing C++, this sometimes uses block comments (/* */
) and sometimes uses line comments (//
). Why does it change? How does it decide which to use when?
-
+1: I haven't used VS for years but I remember the behaviour you describe. Good question. I could never work out the pattern. – Troubadour Apr 18 '11 at 20:40
2 Answers
A couple other discussions on the topic:
Visual studio feature - commenting code Ctrl K - Ctrl C
visual studio C++ toggle comment ? comment while not whole line is selected?
Based on my own tinkerings, and what was said in those articles...
It's based on the start/end of the selection. It seems to use double slashes //
whenever you start your selection at the beginning of the line AND end it at the end of a line.
It will use /* */
notation whenever the selection occurs midway through lines.
IE:
If I have the code
int main () {
return 0;
}
and highlight only int main
, it will convert it to /*int main*/
.
If I highlight the entire code section, starting after the indent tab, it will convert it to
/*int main () {
return 0;
}*/
But if I highlight the section starting before the indent tab, it converts it to
//int main () {
// return 0;
//}
Summary of links under Zhais' answer. Because following links is hard!
- Selecting entire lines (including leading whitespace) will use
//
- Selecting at least one partial line
- If a
//
comment is included, will use//
- Otherwise, will use
/* */
- If a

- 2,556
- 2
- 29
- 38