Source code formatter/indenter

Solution 1:

If you have the vim editor installed, open the file with vim file.c and type =G to indent the file from begin to end. Then save it with :wq.

On default installations, vi (not vim) is installed, so it will not have the required ident package (as mentioned by karel).

Solution 2:

clang-format is your friend! Its easy to use and useful.
Here are some information about it.

Usage

$ clang-format file > formattedfile

Or:

$ clang-format -i file


Step by step guide

1. Horribly formatted code

#include <iostream>
  using namespace std;
    int main() {
         cout << "Oh";
      cout << "clang format rulez!";       
             }

main.cc

2. Magical command

$ clang-format -i main.cc


3. Well formatted code

#include <iostream>
using namespace std;
int main() {
  cout << "Oh";
  cout << "clang format rulez!";
}

main.cc

4. Happiness

Installing

If you like it, you can install it with,

$ sudo apt-get install clang-format

command.