Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a number of expressions, each one is an output: Out[135], Out[136], etc. I wish to print all of these outputs in a C++ form using the function: CForm. I can apply it on each Out[] separately like so,


I was trying to save time and create a for loop as follows:

For[i = 0, i = 34, i++, Out = Out[135 + i]; Print[CForm[Out]]]

But this didn't work!

Any suggestions?

share|improve this question
up vote 8 down vote accepted


Do[Print[CForm[Out[135 + i]]], {i, 0, 34}];
share|improve this answer
This worked just perfect. Thanks – stupidity May 22 '12 at 16:20

Here are two ways:

CForm[Out[#]]& /@ Range[135, 169]


Table[ CForm[Out[i]], {i, 135, 169}]

As the OP wants these printed out, instead of in lists, here are some alternatives. Both,

(* Note the semi-colons *)
Print[CForm[Out[#]]]& /@ Range[135, 169];
Table[Print[CForm[Out[i]]], {i, 135, 169}];

work, but Scan has no output by default, so this is superior

Scan[Print[CForm[Out[#]]]&, Range[135, 169]]
share|improve this answer
Both of them work. Thanks. But the expressions were separated by a comma (all grouped in one output), I have too many so this could be annoying. The answer by Alexey gives each expression as an output. But many thanks anyways :) – stupidity May 22 '12 at 16:22
@stupidity right, both of my methods return lists. This can be overcome by the additions I made above. – rcollyer May 22 '12 at 16:30
Fantastic. Thanks a bunch for your help! – stupidity May 22 '12 at 16:32

While you have been given answers on how you can do it, nobody yet did tell you what you did wrong. There are actually several errors in your code.

The first error is the i = 34. Here are actually two errors. The first error is that you used an assignment where you actually wanted to do a comparison. Equality comparison is done with ==, not =.

However, even if you replace i = 34 with i == 34, the code will still not work. That's because the second argument is not the condition for ending the loop, but the condition for continuing the loop. So the for loop will assign 0 to i, then check i == 34, find it false, and immediately leave the loop. Therefore you'd have to write i != 34 or i < 34 at this point.

If you fix that, you'll notice an error message. That's coming from the assignment Out = Out[135 + i]. Out is protected and therefore cannot be assigned to. However let's assume it were not protected, what would happen? Well, let's assume Out[135] is x. Now that value would be assigned to Out (and then printed). Then in the next iteration, when coming across that assignment again, it would first evaluate Out to x on the right hand side of the assignment, and then proceed with the statement Out = x[135+7], assigning x[136] to Out. Clearly not what you'd want.

To fix that last error, you should use another variable name, say nextout = Out[135 + i]; Print[CForm[nextout]] or even better, just don't use a variable at this point and just write Print[CForm[Out[135 + i]]].

After fixing that error as well, the loop works as intended (but the Do loop suggested by Alexey Popkov is still the better alternative in this case).

share|improve this answer
Thank you indeed for showing me my mistakes and teaching me how to do it correctly. I'm a beginner with Mathematica, but I'm liking it exponentially with time! Thanks again :) – stupidity May 22 '12 at 17:22

I think you want $Post = CForm.

$Post is a global variable whose value, if set, is applied to every output expression.

share|improve this answer
I see. This is useful if I want all expressions in my notebook to be in CForm. Thanks – stupidity May 22 '12 at 16:19
@stupidity I find the fact that you're using Out to store your expressions very confusing. I suggest you don't. I also suggest you set something reasonable like $HistoryLength = 3 to conserve memory, unless that is never a problem. – Mr.Wizard May 22 '12 at 16:39
Many thanks for the tips. I'm only a beginner. :D – stupidity May 22 '12 at 17:23
@stupidity that's OK. Take a look at these resources for help getting started. Also see Faysal's huge omnibus post of Mathematica resources. Last but not least, take advantage of the chat room. – Mr.Wizard May 22 '12 at 17:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.