2

Org and babel in emacs are awesome. Is it possible to have something like this:

Here is a C code

#+BEGIN_SRC C :output results 
  #include <stdio.h>
  int main(){
    int x = 5.00;
    int i;
    for (i = 0; i < 5; ++i) {
      x = x+1;
    }
    printf("X = %d", x);
    return 0;
  }
#+END_SRC

Then I would like to use this X in another programming environment, say Python and do something else with this X.

#+BEGIN_SRC python :output resultPython
print(x+2)
#+END_SRC

It is not as simple as just directly use the variable X. Is it possible to do this in Babel?

1 Answer 1

4

That is one of the main pillars of babel.

Give the C source block a name if you want to use in the variable assignments of another source block.

You input the result of the C source block to another source block by the :var header argument.

The python source blocks are described at https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-python.html. The output of a non-session python block is its return value.

#+NAME: myCSrcBlock
#+BEGIN_SRC C
  #include <stdio.h>
  int main(){
    int x = 5.00;
    int i;
    for (i = 0; i < 5; ++i) {
      x = x+1;
    }
    printf("%d", x);
    return 0;
  }
#+END_SRC

#+RESULTS: myCSrcBlock
: 10

#+RESULTS:
: X = 10

Then I would like to use this X in another programming environment, say Python and do something else with this X.

#+BEGIN_SRC python :var x=myCSrcBlock
return x+2;
#+END_SRC

#+RESULTS:
: 12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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