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 ToCCodeString creates the C-formatted output.
x//ToCCodeStringYou can also enter the variable as a Wolfram Language string.
"x"//ToCCodeStringIntegers can be entered directly.
1//ToCCodeStringFloating-point numbers can also be entered directly.
1.5//ToCCodeStringThe numbers might be output using an exponential notation.
1.5 10^10//ToCCodeStringOf course, you could use a string to create the result as well.
"1.5e100"//ToCCodeStringIf you want to specify the type of a numeric value, you can use CConstant.
CConstant[1, "L"]//ToCCodeStringStrings
You can output strings into the C code using the CString wrapper.
First, you need to load the package.
Needs["SymbolicC`"]CString[ "abc"]//ToCCodeStringComments
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"]//ToCCodeStringYou can add a space before the comment, and have a return after.
CComment["Assign variable here", {" ", "
"}]//ToCCodeStringStatements 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"]//ToCCodeStringAlternatively, when an expression is formatted, no semicolon or new line is added.
CAssign[ a, COperator[ Plus, {x, y}]]//ToCCodeStringIf 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}]]}//ToCCodeStringIf you want to enclose the statements into a block, you should use CBlock.
CBlock[CAssign[ a1, COperator[ Plus, {x1, y1}]], CAssign[ a2, COperator[ Plus, {x2, y2}]]]//ToCCodeStringNote that constructs, such as CBlock, automatically turn their arguments into statements.
CExpression
CExpression lets you include C code generated using the Wolfram Language formatting function CForm.
First, you need to load the package.
Needs["SymbolicC`"]Here the result is generated by CForm.
CExpression[a + b]//ToCCodeStringIf the input would evaluate, as in this Set expression, you need to use HoldForm.
CExpression[HoldForm[a = b]]//ToCCodeStringYou can include a semicolon by inserting the arguments into a list.
{CExpression[HoldForm[a = b]], CExpression[a * b]}//ToCCodeString