Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Formatting like Visual Studio C# formatting

I like the way Visual Studio (2008) formats C# code; unfortunately it seems it doesn't behave in the same way when writing C++ code.

For example, when I write a code in this way:

class Test {
public:
    int x;
    Test() {this->x=20;}
    ~Test(){}
};

in C# (ok this is C++ but you can understand what I mean), this part:

Test() {this->x=20;}

Will become:

Test() { this->x=20; }

This is obviously a stupid example, but there are a lot of things where putting brackets in correct position, indenting code and other things with my own hands becomes boring.

I can obviously change editor if you suggest me a good one for C++ code, I would like to find something with these features:

  • Intellisense (like vs, at least similar)
  • Custom class coloring (in C=C# they are cyan, why are they black in C++?)
  • Wordwrap (possibly)
  • Documentation when you mouse over a method/variable
  • Auto formatting (when you close a bracket like "}" in C# you'll get everything well formatted)

Obviously I can find other features, but this is what is in my mind at the moment.

Thanks for any suggestion.

like image 883
Francesco Belladonna Avatar asked Apr 09 '10 15:04

Francesco Belladonna


People also ask

How do I format C code?

The C/C++ extension for Visual Studio Code supports source code formatting using clang-format which is included with the extension. You can format an entire file with Format Document (Ctrl+Shift+I) or just the current selection with Format Selection (Ctrl+K Ctrl+F) in right-click context menu.

Is Visual Studio best for C?

Yes, you very well can learn C using Visual Studio. Visual Studio comes with its own C compiler, which is actually the C++ compiler. Just use the . c file extension to save your source code.

How do I Auto Arrange codes in Visual Studio?

To automatically format the file on save, In Visual Studio Code, press Control + Shift + P or Command + Shift + P (Mac) to open the command palette and type setting and then select Preferences: Open User Settings option. Search for format on save setting and check the checkbox.

Can I use Visual Studio Code for C?

C/C++ support for Visual Studio Code is provided by a Microsoft C/C++ extension to enable cross-platform C and C++ development on Windows, Linux, and macOS.


2 Answers

Select the code-text and press CTRL+K-F in Visual Studio - That's it :)

like image 53
hB0 Avatar answered Oct 08 '22 09:10

hB0


I'm using AStyle extension for VS2012 and I'm happy with it.
It's free :)

like image 36
Achilles Avatar answered Oct 08 '22 08:10

Achilles