Wednesday, October 26, 2022

Formatting C++ code in Visual Studio

C++ code is usually formatted as follows:
if (condition)
{

}
else {

}

I like the curly braces to be on the same line so that I can see more code in less lines:
if (condition) {

} else {

}

To define this format in Visual Studio 2022, go to Tools - Options - Text Editor - C/C++ - Code Style - Formatting - New Lines and uncheck the boxes at the bottom:
After this change, you can update the format of an existing file with Edit - Advanced - Format Document.

If you also want the code snippet for if to behave similarly, go to Tools - Code Snippet Manager - Visual C++, copy the location of the if snippet file, open the file in a text editor with admin privileges and edit and save it:


No comments:

Post a Comment