Go to the first, previous, next, last section, table of contents.

Customizing C Indentation

Two variables control which commands perform C indentation and when.

If c-auto-newline is non-nil, newlines are inserted both before and after braces that you insert, and after colons and semicolons. Correct C indentation is done on all the lines that are made this way.

If c-tab-always-indent is nil, the TAB command in C mode does indentation only if point is at the left margin or within the line's indentation. If there is non-whitespace to the left of point, then TAB just inserts a tab character in the buffer. Normally, this variable is t, and TAB always reindents the current line. The default behavior means that to insert a real tab character you must quote it by typing C-q TAB.

C does not have anything analogous to particular function names for which special forms of indentation are desirable. However, it has a different need for customization facilities: many different styles of C indentation are in common use.

There are six variables you can set to control the style that Emacs C mode uses.

Indentation of C statements within surrounding block. The surrounding block's indentation is the indentation of the line on which the open-brace appears.
Extra indentation given to a substatement, such as the then-clause of an if or body of a while.
Extra indentation for line if it starts with an open brace.
An open brace following other text is treated as if it were this far to the right of the start of its line.
Indentation level of declarations of C function arguments.
Extra indentation for line that is a label, or case or default.

The variable c-indent-level controls the indentation for C statements with respect to the surrounding block. In the example

      foo ();

the difference in indentation between the lines is c-indent-level. Its standard value is 2.

If the open-brace beginning the compound statement is not at the beginning of its line, the c-indent-level is added to the indentation of the line, not the column of the open-brace. For example,

if (losing) {
  do_this ();

One popular indentation style is that which results from setting c-indent-level to 8 and putting open-braces at the end of a line in this way. I prefer to put the open-brace on a separate line.

In fact, the value of the variable c-brace-imaginary-offset is also added to the indentation of such a statement. Normally this variable is zero. Think of this variable as the imaginary position of the open brace, relative to the first nonblank character on the line. By setting this variable to 4 and c-indent-level to 0, you can get this style:

if (x == y) {
    do_it ();

When c-indent-level is zero, the statements inside most braces will line up right under the open brace. But there is an exception made for braces in column zero, such as surrounding a function's body. The statements just inside it do not go at column zero. Instead, c-brace-offset and c-continued-statement-offset (see below) are added to produce a typical offset between brace levels, and the statements are indented that far.

c-continued-statement-offset controls the extra indentation for a line that starts within a statement (but not within parentheses or brackets). These lines are usually statements that are within other statements, such as the then-clauses of if statements and the bodies of while statements. This parameter is the difference in indentation between the two lines in

if (x == y)
  do_it ();

Its standard value is 2. Some popular indentation styles correspond to a value of zero for c-continued-statement-offset.

c-brace-offset is the extra indentation given to a line that starts with an open-brace. Its standard value is zero; compare

if (x == y)


if (x == y)
  do_it ();

if c-brace-offset were set to 4, the first example would become

if (x == y)

c-argdecl-indent controls the indentation of declarations of the arguments of a C function. It is absolute: argument declarations receive exactly c-argdecl-indent spaces. The standard value is 5, resulting in code like this:

char *
index (string, c)
     char *string;
     int c;

c-label-offset is the extra indentation given to a line that contains a label, a case statement, or a default: statement. Its standard value is -2, resulting in code like this

switch (c)
  case 'x':

If c-label-offset were zero, the same code would be indented as

switch (c)
    case 'x':

This example assumes that the other variables above also have their standard values.

I strongly recommend that you try out the indentation style produced by the standard settings of these variables, together with putting open braces on separate lines. You can see how it looks in all the C source files of GNU Emacs.

Go to the first, previous, next, last section, table of contents.