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 Mode Description
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; }

指定铸造模式Using theMATLABCoder应用程序

  1. On theGenerate Codepage, to open theGeneratedialog box, click theGeneratearrow.

  2. SetBuild typeto one of the following:

    • 源代码

    • Static Library (.lib)

    • Dynamic Library (.dll)

    • Executable (.exe)

  3. ClickMore Settings.

  4. On theAll Settingstab, underAdvanced, setCasting modeto one of the following values:

    • Nominal

    • Standards Compliant

    • Explicit

指定铸造模式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 theCastingModeproperty to one of the following values:

    • 'Nominal'

    • “标准”

    • 'Explicit'

    For example:

    cfg.CastingMode =“标准”;

See Also

Related Topics