Main Content

Control Data Type Casts in Generated Code

If you have an Embedded Coder® license, you can control data type casts in C/C++ code generated from MATLAB® code. You can specify one of the following casting modes.

Casting ModeDescription
Nominal

Nominal casting mode is the default casting mode. Generated C/C++ code uses the default C compiler data type casting. When you do not have special data type information requirements, choose this option. Here is an example of code generated using nominal casting mode:

short addone(short x)
{
  int i;
  i = x + 1;
  if (i > 32767) {
    i = 32767;
  }

  return (short)i;
}
Standards Compliant

Generated C/C++ code has data type casts that conform to MISRA® standards. The MISRA data type casting eliminates common MISRA standard violations, including address arithmetic and assignment. It reduces 10.1, 10.2, 10.3, and 10.4 violations. Here is an example of code generated using standards-compliant casting mode:

short addone(short x)
{
  int i;
  i = (int)x + (int)1;
  if (i > (int)32767) {
    i = (int)32767;
  }

  return (short)i;
}
Explicit

Generated C/C++ code has explicit data type casts. Explicit data type casts provide information about the amount of memory that the variable uses and the level of precision for calculations using the variable. Here is an example of code generated using explicit casting mode:

short addone(short x)
{
  int i;
  i = (int)x + 1;
  if (i > 32767) {
    i = 32767;
  }

  return (short)i;
}

Specify Casting Mode 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 Casting mode to one of the following values:

    • Nominal

    • Standards Compliant

    • Explicit

Specify Casting Mode 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 CastingMode property to one of the following values:

    • 'Nominal'

    • 'Standards'

    • 'Explicit'

    For example:

    cfg.CastingMode = 'Standards';
    

See Also

Related Topics