111

I was wondering if there is something like an interpreter for C. That is, in a Linux terminal I can type in "python" and then code in that interpreter. (I'm not sure interpreter the right word). This is really helpful for testing different things out and I'm curious if something similar exists for C. Though I doubt it. The only thing I can think of that would do it would be the C shell...

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
devin
  • 6,407
  • 14
  • 48
  • 53
  • 5
    The correct word for what you want is "shell" or "prompt" or "REPL" (Read-Eval-Print-Loop, in reference to the 4 Lisp commands that must be joined to make a Lisp interpreter in Lisp), but an interpreter would work. – Chris Lutz Feb 25 '09 at 04:40
  • 1
    As an aside, the term you are looking for is REPL (Read Evaluate Print Loop). – tsimon Feb 25 '09 at 05:05
  • duplicate of [Is there a REPL for C programming?](http://stackoverflow.com/q/10766900/309483) – Janus Troelsen Jan 31 '14 at 12:29
  • The preferred word is actually "interactive" environment for C. An interactive environment opens up a shell or prompt specific to the language. REPL is also good. – jcchuks Jan 02 '17 at 18:37
  • A comprehensive list of interpreters and compilers for C/C++ can be found here: https://www.thefreecountry.com/compilers/cpp.shtml – froggsy May 08 '18 at 01:43

13 Answers13

71

There are many - if you narrow down the scope of your question we might be able to suggest some specific to your needs.

A notable interpreter is "Ch: A C/C++ Interpreter for Script Computing" detailed in Dr. Dobbs:

Ch is a complete C interpreter that supports all language features and standard libraries of the ISO C90 Standard, but extends C with many high-level features such as string type and computational arrays as first-class objects.

Ch standard is freeware but not open source. Only Ch professional has the plotting capabilities and other features one might want.

I've never looked at this before, but having a c interpreter on hand sounds very useful, and something I will likely add to my toolset. Thanks for the question!


Edit:

Just found out that one of my favorite compilers, TCC, will execute C scripts:

It also handles C script files (just add the shebang line "#!/usr/local/bin/tcc -run" to the first line of your C source code file on Linux to have it executed directly.

TCC can read C source code from standard input when '-' is used in place of 'infile'. Example:

echo 'main(){puts("hello");}' | tcc -run -
Community
  • 1
  • 1
Adam Davis
  • 91,931
  • 60
  • 264
  • 330
  • 2
    I wish a version of CH was available under an OSI approved license, so it could be distributed. You can design really nice utilities using their interpreter, unfortunately they're just too problematic to bundle. – Tim Post Feb 25 '09 at 04:56
  • That would be nice. Freeware is better than nothing, but until someone does it... – Adam Davis Feb 25 '09 at 05:01
  • Weirdly, `tcc` exits with status `10` by default. It looks like you have to explicitly add a `return 0` giving `echo 'main(){puts("hello"); return 0;}' | tcc -run -` – Greg Nisbet Dec 23 '16 at 21:27
18

picoc - A very small C interpreter

PicoC is a very small C interpreter for scripting. It was originally written as the script language for a UAV's on-board flight system. It's also very suitable for other robotic, embedded and non-embedded applications.

aleroot
  • 71,077
  • 30
  • 176
  • 213
9

the ROOT project provides a very functional C and C++ interpreter called Cint. I'm quite fond of it. It takes a little getting used to interpretively, though.

TCC is a very good choice as well, but i'm not able to vouch for its REPL

SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
  • 1
    Isn't the ROOT team phasing out Cint? – Z boson Apr 27 '15 at 08:46
  • @Zboson "CERN has switch to a new interpreter, cling. CINT is not supported by CERN anymore" – endolith Nov 30 '17 at 15:24
  • ROOT replaced cint by cling. So cling can be taken as updated recommendation. (though - see other answers - cling is C++, not C, if that's relevant, though iirc cint also did some C++) – pseyfert Mar 27 '18 at 09:51
8

Probably. There are several for c++. See Have you used any of the C++ interpreters (not compilers)? for examples. Certainly cint will eat nearly any c code with good results, and tcc is so fast that you can use it like a interpreter.

Community
  • 1
  • 1
dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
7

Give a look to the Ch Interpreter.

Ch is an embeddable C/C++ interpreter for cross-platform scripting, shell programming, 2D/3D plotting, numerical computing, and embedded scripting.

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
6

I know we use CINT in class. It seemed pretty good you might want to give it a try!

Javed Ahamed
  • 2,804
  • 6
  • 32
  • 41
5

Check Out iGCC

Neel Basu
  • 12,638
  • 12
  • 82
  • 146
4

cinterp is one for a start.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 7
    Are you referring to https://github.com/danielkeller/cinterp or to http://free-compilers.sharnoff.org/TOOL/C-12.html ? A link would improve this answer ... – f3lix May 08 '13 at 10:41
2

ccons appears to satisfy your constraints:

The goal of the ccons project is to create an interactive console for the C programming language, similar to "python" and "irb" for Python and Ruby respectively. The project builds on top of clang and llvm.

Tilded
  • 421
  • 3
  • 11
icktoofay
  • 126,289
  • 21
  • 250
  • 231
2

You can use CompCert. Here are some examples: http://compcert.inria.fr/man/manual004.html

Janus Troelsen
  • 20,267
  • 14
  • 135
  • 196
2

More recently there is Cling (based on LLVM/Clang)

http://root.cern.ch/drupal/content/cling

ideasman42
  • 42,413
  • 44
  • 197
  • 320
1

It has been done, even though the vast majority of C work is compiled. One example is CH

Rob Lachlan
  • 14,289
  • 5
  • 49
  • 99
0

CERN has a toolkit called ROOT, which is meant mainly for scientific or data analysis etc.. purposes but it has a Clang - based C/C++ Interpreter called Cling.

They were using a C interpreter called CINT before they adapted Cling. CINT is lightweight & still seems to be available for download.

I think you may get some other by carefully searching through Wikipedia list articles.