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 Mathematica input or as a string.

First, you need to load the package.

In[1]:=
Click for copyable input

You can enter a variable as a Mathematica symbol. Note how ToCCodeString creates the C-formatted output.

In[2]:=
Click for copyable input
Out[2]=

You can also enter the variable as a Mathematica string.

In[3]:=
Click for copyable input
Out[3]=

Integers can be entered directly.

In[4]:=
Click for copyable input
Out[4]=

Floating-point numbers can also be entered directly.

In[5]:=
Click for copyable input
Out[5]=

The numbers might be output using an exponential notation.

In[6]:=
Click for copyable input
Out[6]=

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

In[7]:=
Click for copyable input
Out[7]=

If you want to specify the type of a numeric value, you can use CConstant.

In[8]:=
Click for copyable input
Out[8]=

Strings

You can output strings into the C code using the CString wrapper.

First, you need to load the package.

In[1]:=
Click for copyable input

This creates a C string.

In[2]:=
Click for copyable input
Out[2]=

Comments

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

First, you need to load the package.

In[1]:=
Click for copyable input

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

In[8]:=
Click for copyable input
Out[8]=

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

In[10]:=
Click for copyable input
Out[10]=

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.

In[1]:=
Click for copyable input

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

In[2]:=
Click for copyable input
Out[2]=

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

In[3]:=
Click for copyable input
Out[3]=

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

In[4]:=
Click for copyable input
Out[4]=
In[5]:=
Click for copyable input
Out[5]=

If you want to enclose the statements into a block, you should use CBlock.

In[6]:=
Click for copyable input
Out[6]=

Note that constructs, such as CBlock, automatically turn their arguments into statements.

CExpression

CExpression lets you include C code generated using the Mathematica formatting function CForm.

First, you need to load the package.

In[1]:=
Click for copyable input

Here the result is generated by CForm.

In[2]:=
Click for copyable input
Out[2]=

If the input would evaluate, as in this Set expression, you need to use HoldForm.

In[3]:=
Click for copyable input
Out[3]=

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

In[4]:=
Click for copyable input
Out[4]=
New to Mathematica? Find your learning path »
Have a question? Ask support »