Next: , Previous: , Up: Indent Program   [Contents][Index]

1.6 Statements

The -br or -bl option specifies how to format braces.

The -br option formats statement braces like this:

if (x > 0) {
  x--;
}

The -bl option formats them like this:

if (x > 0)
  {
    x--;
  }

If you use the -bl option, you may also want to specify the -bli option. This option specifies the number of spaces by which braces are indented. -bli2, the default, gives the result shown above. -bli0 results in the following:

if (x > 0)
{
  x--;
}

If you are using the -br option, you probably want to also use the -ce option. This causes the else in an if-then-else construct to cuddle up to the immediately preceding ‘}’. For example, with -br -ce you get the following:

if (x > 0) {
  x--;
} else {
  fprintf (stderr, "...something wrong?\n");
}

With -br -nce that code would appear as

if (x > 0) {
  x--;
}
else {
  fprintf (stderr, "...something wrong?\n");
}

An exception to the behavior occurs when there is a comment between the right brace and the subsequent else statement. While the -br option will cause a left brace to jump over the comment, the else does not jump over the comment to cuddle because it has a strong likelihood of changing the meaning of the comment.

The -cdw option causes the while in a do-while loop to cuddle up to the immediately preceding ‘}’. For example, with -cdw you get the following:

do {
  x--;
} while (x);

With -ncdw that code would appear as

do {
  x--;
}
while (x);

The -slc option allows for an unbraced conditional and its inner statement to appear on the same line. For example:

if (x) x--;
else x++;

Without -slc that code would appear as

if (x)
  x--;
else
  x++;

The -cli option specifies the number of spaces that case labels should be indented to the right of the containing switch statement.

The default gives code like:

switch (i)
  {
  case 0:
    break;
  case 1:
    {
      ++i;
    }
  default:
    break;
  }

Using the -cli2 that would become:

switch (i)
  {
    case 0:
      break;
    case 1:
      {
        ++i;
      }
    default:
      break;
  }

The indentation of the braces below a case statement can be controlled with the -cbin option. For example, using -cli2 -cbi0 results in:

switch (i)
  {
    case 0:
      break;
    case 1:
    {
      ++i;
    }
    default:
      break;
  }

If a semicolon is on the same line as a for or while statement, the -ss option will cause a space to be placed before the semicolon. This emphasizes the semicolon, making it clear that the body of the for or while statement is an empty statement. -nss disables this feature.

The -pcs option causes a space to be placed between the name of the procedure being called and the ‘(’ (for example, puts ("Hi");. The -npcs option would give puts("Hi");).

If the -cs option is specified, indent puts a space between a cast operator and the object to be cast. The -ncs ensures that there is no space between the cast operator and the object. Remember that indent only knows about the standard C data types and so cannot recognise user-defined types in casts. Thus (mytype)thing is not treated as a cast.

The -bs option ensures that there is a space between the keyword sizeof and its argument. In some versions, this is known as the ‘Bill_Shannon’ option.

The -saf option forces a space between a for and the following parenthesis. This is the default.

The -sai option forces a space between a if and the following parenthesis. This is the default.

The -saw option forces a space between a while and the following parenthesis. This is the default.

The -prs option causes all parentheses to be separated with a space from whatever is between them. For example, using -prs results in code like:

  while ( ( e_code - s_code ) < ( dec_ind - 1 ) )
    {
      set_buf_break ( bb_dec_ind );
      *e_code++ = ' ';
    }

Next: , Previous: , Up: Indent Program   [Contents][Index]