Main Content

Specify Indent Style for C/C++ Code

If you have an Embedded Coder® license, you can control the indent style and indent size in C/C++ code generated from MATLAB® code. Indent style controls the placement of braces. Indent size controls the number of characters per indentation level.

You can specify the K&R indent style or the Allman indent style. Both styles:

  • Place the function opening and closing braces on their own lines at the same indentation level as the function header.

  • Indent code within the function according to the indent size.

  • For blocks within a function, place closing braces on a new line at the same indentation level as the control statement.

  • Indent code within a block according to the indent size.

The K&R style and the Allman style differ in their placement of the opening brace for a control statement. If you want the opening brace on the same line as its control statement, select the K&R style. Here is code that has the K&R indent style:

void addone(const double x[6], double z[6])
{
  int i;
  for (i = 0; i < 6; i++) {
    z[i] = x[i] + 1.0;
  }
}

If you want the opening brace on its own line, select the Allman style. Here is code that has the Allman indent style:

void addone(const double x[6], double z[6])
{
  int i;
  for (i = 0; i < 6; i++)
  {
    z[i] = x[i] + 1.0;
  }
}

Specify Indent Style Using the MATLAB Coder App

  1. On the Generate Code page, to open the Generate dialog box, click the Generate arrow .

  2. Set Build type to one of the following:

    • Source Code

    • Static Library (.lib)

    • Dynamic Library (.dll)

    • Executable (.exe)

  3. Click More Settings.

  4. On the All Settings tab, under Advanced, set Indent style to K&R or Allman.

  5. On the All Settings tab, under Advanced, set Indent size to an integer from 2 to 8.

Specify Indent Style Using the Command-Line Interface

  1. Create a code configuration object for 'lib', 'dll', or 'exe'. For example:

    cfg = coder.config('lib','ecoder',true); % or dll or exe
    

  2. Set the IndentStyle property to 'K&R' or 'Allman'. For example:

    cfg.IndentStyle = 'Allman';
    

  3. Set the IndentSize property to an integer from 2 to 8. For example:

    cfg.IndentSize = 4;
    

See Also

Related Topics