C Function
Integrate and call external C/C++ code from a Simulink model
Libraries:
Simulink /
User-Defined Functions
Description
The C Function block integrates and calls external C/C++ code from a Simulink® model. Use this block to define external code and customize the integration of your code by preprocessing or postprocessing the data. In addition, you can specify customized code for simulation and C code generation. You can conditionally call functions defined in your code, and you can also call multiple functions in one block. Using this block, you can initialize persistent data and pass it to an external function.
The C Function block supports initializing persistent data and calling external functions from the block dialog box. Persistent data can include an object of a C++ class defined in your custom code. See Interface with C++ Classes Using C Function Block. The block supports only initializing and terminating persistent data. The block does not support updating the data during simulation. To model a dynamic system with continuous states, use an S-Function block. To learn more about S-functions, see What Is an S-Function?
Note
C99 is the standard version of C language supported for custom C code integration into Simulink.
Specify Custom Code
Starting in R2024a, the block provides you with a range of options to specify custom C/C++ code for the block.
You can specify the custom code locally in the block, so you can integrate a code component that is fully contained in a single block. For example, to integrate a custom filter defined using C/C++ code into your model, specify the custom code locally using this option. The block then behaves like the specified filter.
For versions R2026a and later:
When you debug a C Function block with locally specified custom code, the local custom code runs in a separate process outside of MATLAB®, known as out-of-process simulation, regardless of the simulation mode of the model containing the block. This mode is safer because the custom code is built into a separate executable. As a result, any problems with the custom code do not cause MATLAB to crash. For more information about debugging custom C/C++ code, see Debug Custom C/C++ Code.
When you simulate a model containing a C Function block with locally specified custom code:
In normal mode, the local custom code simulates in out-of-process simulation.
In accelerator mode, the local custom code is built into a dynamic library and simulates in the same process as MATLAB, known as in-process simulation. This mode is faster than the out-of-process simulation, but any problems with the custom code might cause MATLAB to crash.
Previously, both debugging and simulation of locally specified custom code occurred in out-of-process simulation, regardless of simulation mode of the model containing the C Function block.
You can share the custom code with other blocks in a model or across multiple models. To share the custom code across multiple models you must define the custom code in the Configuration Parameters dialog box of a Simulink library model. Then, use the code in a block within the library and fetch that block from that library in other models.
For more information, see Configure custom code settings.
Report Run-time Errors and Warnings in Simulink
In custom C code that you specify in the C Function block , use the
slError and slWarning functions to report
run-time errors and warnings, respectively. An error terminates the simulation and must be
resolved before simulation continues. A warning indicates a potential issue with the code
but does not terminate the simulation.
Both slError and slWarning functions support
different data type format specifiers (for example, %d for integer,
%f for float) that are similar to C.
Examples :
slError("Error message");slError("Input %f should be non-negative", x);slWarning("Warning message");slWarning("Input %d should be non-zero", y);
The following code executes at the start of the simulation. It uses the
slError function to issue an error message if the data store fails to
initialize at the start of simulation.

Specify Row-Major and Column-Major Array Layouts in Custom Code
Use the slSetRowMajor and slSetColumnMajor
functions to specify row-major and column-major array layouts, respectively. Specify these
functions in the Simulation Target pane of the Configuration Parameters
dialog box. To access the Configuration Parameters dialog box from the C
Function block dialog box, set the Configure custom code
settings
parameter to Use Configuration Parameters Custom
Code.
For a row-major layout, the row elements of a matrix are stored contiguously in memory. Similarly, for a column-major layout, the column elements of a matrix are stored contiguously in memory. For more information, see Row-Major and Column-Major Array Layouts (MATLAB Coder).
When you set the Configure custom code settings
parameter to Use Configuration Parameters Custom
Code , the block can use both row-major and column-major configurations for
both simulation and code generation.
When you set the Configure custom code settings
parameter to Use Block Custom Code, the
block uses row-major for simulation and column-major for code generation.
Function prototypes:
void slSetRowMajor(const char* functionName)void slSetColumnMajor(const char* functionName)
functionName must be a string literal and fully qualified.
For example, consider the following code:
namespace NS1 { class ACLASS{ public: void foo_row(); }; namespace NS2{ void foo_col(double*, double*); } }
For a fully qualified function name.
If the function is inside a namespace, all the nested namespaces should be included in the function name string. Based on the above code, for the column-major function,
foo_col, useslSetColumnMajor("NS1::NS2::foo_col")in the configuration parameters custom code.For all class members, the class name along with any nested namespaces should be included in the function name string. Based on the above code, for the row-major function
foo_row, useslSetRowMajor("NS1::ACLASS::foo_row")in the configuration parameters custom code.
For a C Function block that uses classes, if the constructor takes majority, the majority must be specified in the Start code.
Examples
Limitations
Limitations apply when using these Simulink features with the C Function block.
General Limitations
These are general limitations applicable to all Simulink versions.
Starting in R2025a, code coverage is supported.
For versions prior to R2025a, Simulink Coverage™ is not supported. Only execution coverage of the C Function block is measured.
Simulink Design Verifier™
Simulink Design Verifier does not generate test cases for coverage objectives inside the C Function block. The limitations that apply to C/C++ S-functions also apply to C/C++ code in a C Function block. In particular, calls to external or library functions are replaced by stubs for analysis. For more information on these limitations, see Simulink Design Verifier Limitations and Considerations for S-Functions and C/C++ Code (Simulink Design Verifier).
Simulink Code Inspector™
Simulink Code Inspector does not inspect the code generated from the C Function block.
C Function block that uses local custom code is not supported inside a Dataflow Subsystem (DSP System Toolbox).
C and C++ Language Limitations
These limitations apply to C and C++ code that you specify in a C Function block in versions R2025b and earlier. These limitations also apply to versions R2026a and later under these block settings:
The Configure custom code settings
parameter is set to Use Configuration Parameters Custom Code.The Configure custom code Settings
parameter is set to Use Block Custom Codeand Parse block code (enable for backward compatibility) parameter is selected.
Note
These limitations do not apply to the C /C++ code that you specify in a C
Function block for versions R2026a and later under its default settings. The
default settings occur when the Configure custom code settings
parameter is set to Use Block Custom Code
and the Parse block code
(enable for backward compatibility) parameter is not selected.
When the C Function block meets the above mentioned limitation conditions, the block supports only these C Math Library functions.
abs | acos | asin | atan | atan2 | ceil |
cos | cosh | exp | fabs | floor | fmod |
labs | ldexp | log | log10 | pow | sin |
sinh | sqrt | tan | tanh |
When you call these functions, double precision applies unless all the input arguments
are explicitly single precision. When a type mismatch occurs, a cast of the input
arguments to the expected type replaces the original arguments. For example, if you call
the sin function with an integer argument, a cast of the input argument
to a floating-point number of type double replaces the original
argument.
You can call these string library functions inside the C function block when using versions R2025a and later.
strlen | strncpy | strncat | strcmp | strncmp | strstr |
For more information, see Use Simulink Strings Inside C Function Block to Model Scrolling Display.
To call other C library functions, create and call an external wrapper function that calls the C library function.
abs, fabs, and labs
FunctionsInterpretation of the abs, fabs, and
labs functions in C Function block goes beyond the
standard C version to include integer and floating-point arguments:
If
xis an integer, the standard C functionabsapplies tox, orabs(x).If
xis a double, the standard C functionlabsapplies tox, orlabs(x).If
xis a single, the standard C functionfabsapplies tox, orfabs(x).
The call to the function should call the correct CRL based on the type of data passed into the function. If no CRL is specified, the call to the function should call to type-specific library. The CRL for C99 generates a type-specific function. For example:
| Type passed in | Code generation call |
|---|---|
sin(doubleIn) | sin(doubleIn) |
sin(floatIn) | sinf(floatIn) |
You can call printf,
memcpy and memset functions in the C
Function block when using versions R2023a and later.
Local static variables using the
statickeyword are not supported. To cache values across time steps, define a symbol asPersistentin the Symbols table of the block dialog box.Taking the address of a
Constantsymbol is not supported.Directly calling C library functions other than the C Math library functions listed under the C Library Functions is not supported. To call other C library functions, create and call a wrapper function that calls the C library function.
Ports
Input
Output
Parameters
Block Characteristics
Tips
If the imported types are defined under C++ namespaces, use any of these options:
Generate type alias header which contains type alias corresponding to the type signatures from the imported header file using the
SimulinkTypeAliasHeaderargument of theSimulink.importExternalCTypesfunction. Then, specify the generated header file in the Headers section of the Simulation Custom Code tab. For more information, seeSimulinkTypeAliasHeader. Reimport types with unique type names usingUseFullyQualifiedNameargument of theSimulink.importExternalCTypesfunction. For example, you can use this argument when you want to import the same type defined under different namespaces. For more information, seeUseFullyQualifiedNameImport types from the headers to Architectural Data section of a Simulink data dictionary using
DataDictionarySectionargument of theSimulink.importExternalCTypesfunction. When you import the types, their corresponding namespaces are automatically synchronized. However, you cannot import two types of the same name under one namespace. For more information, seeDataDictionarySection.






