Introduction

The Wolfram Language's core tree-oriented symbolic structure makes it well suited to working with a hierarchical view of C code as Wolfram Language expressions. This supports the use of the Wolfram Language for the creation, manipulation, and optimization of C code.

It is used extensively for the Wolfram Language's code generation tools. In addition, you can use SymbolicC for your own code manipulation purposes.

To use SymbolicC you first need to load the package.

Needs["SymbolicC`"]

Now you can start to build up elements of a C program. The following represents a variable declaration.

dec=CDeclare[ double,"x"]

An important feature of SymbolicC expressions is that they are inert; they evaluate to themselves, staying in an unevaluated form.

Here is an assignment and an entire function.

asgn = CAssign["a","x"];
fun=CFunction[void, "fun",{{double,x}},CBlock[{dec,asgn}]];

You can keep the function as an expression, or you can convert it into a string that shows the C code.

ToCCodeString[fun]

Importing from C Programs

At present the Wolfram Language has no feature for importing from C programs. This has a number of challenges, such as how to work with the C preprocessor and dealing with context-sensitive parsing. Despite this, SymbolicC is very useful as a way to create C programs.

The C preprocessor adds difficulties, because it is appealing to represent it in SymbolicC, but there are programs that are hard to represent in a strict tree fashion. An example is shown below.

#ifdef __cplusplus
extern "C" {
#endif
int function( int arg);
#ifdef __cplusplus
}
#endif

To parse the C language you need to understand the meaning and context of the input. For example, in the following it is not clear whether this is a declaration of b, which is a pointer to type a, or a multiplication of two variables, a and b.

a * b

C++

At present, SymbolicC only supports C syntax.