({{, (RefLink[CForm, paclet:ref/CForm])[expr]
prints as a C language version of expr. }})

OpenersNotesSection(Spacer1)Details

Standard arithmetic functions and certain control structures are translated.

No declarations are generated.

(RefLink[CForm, paclet:ref/CForm]) acts as a “wrapper”, which affects printing, but not evaluation.

OpenersPrimaryExamplesSection(Spacer1)Examples  (6)

OpenersExampleSection0(Spacer1)Basic Examples  (1)

A C language version of a polynomial:

CForm[1+2x+3x^2]

A mixed numeric and symbolic expression:

CForm[1.2 x+Exp[y]]

OpenersExampleSection1(Spacer1)Scope  (1)

C formatting for an arbitrary function:

CForm[f[x]]

A composite power:

CForm[f^g[h[x]]]

A vector:

CForm[{1,2,3}]

A matrix:

CForm[{{1,2,3},{4,5,6}}]

OpenersExampleSection2(Spacer1)Applications  (1)

Compute the analytic Hessian for a multivariate function for use in C code:

f=x^2+y^3 Sin[x];

D[f,{{x,y},2}]

CForm[%]

OpenersExampleSection3(Spacer1)Properties & Relations  (2)

(RefLink[CForm, paclet:ref/CForm]) formats expressions like C code:

CForm[1+2x+3x^2]

(RefLink[FortranForm, paclet:ref/FortranForm]) formats expressions like Fortran code:

FortranForm[1+2x+3x^2]

(RefLink[Splice, paclet:ref/Splice]) can be used to convert expressions in a file:

CForm[Integrate[Sin[x]^5, x]]

The following C file contains the unevaluated integral:

ifile=FileNameJoin[{"ExampleData","splicefile.mc"}];

FilePrint[ifile]

(RefLink[Splice, paclet:ref/Splice]) inserts the result of the integral:

ofile=FileNameJoin[{$TemporaryDirectory,"spliceoutput.c"}];

Splice[ifile,ofile]

The spliced file:

FilePrint[ofile]

OpenersExampleSection4(Spacer1)Possible Issues  (1)

Additional C function definitions may be needed to run the code:

CForm[BesselJ[x,7]]

Here is a template for such an include file (shown on a typical Mac installation):

FileNames["*",FileNameJoin[{$InstallationDirectory,"SystemFiles","IncludeFiles","C"}]][[1]]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[FortranForm, paclet:ref/FortranForm])  ▪  (RefLink[Compile, paclet:ref/Compile])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Generating C and Fortran Expressions, paclet:tutorial/GeneratingCAndFortranExpressions])}, {▪(RefLinkPlain[Forms of Input and Output, paclet:tutorial/FormsOfInputAndOutput])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Display of Numbers, paclet:guide/DisplayOfNumbers])}, {▪(RefLinkPlain[C/C++ Language Interface, paclet:guide/CLanguageInterface])}, {▪(RefLinkPlain[Numerical Evaluation & Precision, paclet:guide/NumericalEvaluationAndPrecision])}})}})