Formatting

SymbolicC provides automated formatting of the generated C output. This section reviews some of the ways that you can work with formatting to create your own styles of output.

Atomic Input

Atomic input such as variables or numbers can be passed in as typical Wolfram Language input or as a string.

First, you need to load the package.

Needs["SymbolicC`"]

You can enter a variable as a Wolfram Language symbol. Note how (PackageLink[ToCCodeString, paclet:SymbolicC/ref/ToCCodeString, SymbolicC Package Symbol]) creates the C-formatted output.

x//ToCCodeString

You can also enter the variable as a Wolfram Language string.

"x"//ToCCodeString

Integers can be entered directly.

1//ToCCodeString

Floating-point numbers can also be entered directly.

1.5//ToCCodeString

The numbers might be output using an exponential notation.

1.5 (10^10)//ToCCodeString

Of course, you could use a string to create the result as well.

"1.5e100"//ToCCodeString

If you want to specify the type of a numeric value, you can use (PackageLink[CConstant, paclet:SymbolicC/ref/CConstant, SymbolicC Package Symbol]).

CConstant[1,"L"]//ToCCodeString

Strings

You can output strings into the C code using the (PackageLink[CString, paclet:SymbolicC/ref/CString, SymbolicC Package Symbol]) wrapper.

First, you need to load the package.

Needs["SymbolicC`"]

This creates a C string.

CString[ "abc"]//ToCCodeString

Comments

There are a number of ways that you can modify the formatting of comments.

First, you need to load the package.

Needs["SymbolicC`"]

This outputs a comment. Note how the contents of the comment are padded with spaces.

CComment["Assign variable here"]//ToCCodeString

You can add a space before the comment, and have a return after.

CComment["Assign variable here", {"    ","\n"}]//ToCCodeString

Statements and Expressions

SymbolicC attempts to distinguish between statements, which do not return anything, and expressions, which do return a result.

First, you need to load the package.

Needs["SymbolicC`"]

When converting a statement such as a declaration to a string, it is terminated with a semicolon and a new line as shown below.

CDeclare[ double,"x"]//ToCCodeString

Alternatively, when an expression is formatted, no semicolon or new line is added.

CAssign[ a, COperator[ Plus, {x,y}]]//ToCCodeString

If you want to add a semicolon and new line then you can place the argument inside a list.

{CAssign[ a, COperator[ Plus, {x,y}]]}//ToCCodeString

{CAssign[ a, COperator[ Plus, {x,y}]],
CAssign[ a2, COperator[ Plus, {x2,y2}]]}//ToCCodeString

If you want to enclose the statements into a block, you should use (PackageLink[CBlock, paclet:SymbolicC/ref/CBlock, SymbolicC Package Symbol]).

CBlock[CAssign[ a1, COperator[ Plus, {x1,y1}]],CAssign[ a2, COperator[ Plus, {x2,y2}]]]//ToCCodeString

Note that constructs, such as (PackageLink[CBlock, paclet:SymbolicC/ref/CBlock, SymbolicC Package Symbol]), automatically turn their arguments into statements.

CExpression

(PackageLink[CExpression, paclet:SymbolicC/ref/CExpression, SymbolicC Package Symbol]) lets you include C code generated using the Wolfram Language formatting function (RefLink[CForm, paclet:ref/CForm]).

First, you need to load the package.

Needs["SymbolicC`"]

Here the result is generated by (RefLink[CForm, paclet:ref/CForm]).

CExpression[a+b]//ToCCodeString

If the input would evaluate, as in this (RefLink[Set, paclet:ref/Set]) expression, you need to use (RefLink[HoldForm, paclet:ref/HoldForm]).

CExpression[HoldForm[a=b]]//ToCCodeString

You can include a semicolon by inserting the arguments into a list.

{CExpression[HoldForm[a=b]],CExpression[a*b]}//ToCCodeString