0

There are a lot of answers and quotations about "Compiled vs. Interpreted" and I do understand the differences between them.

When it comes to C, I am not sure: Is C a compiled or an Interpreted language, or both? And, if both I will really thankful if you add a bit of explanation.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Hanoch
  • 339
  • 1
  • 3
  • 10
  • 2
    Read this answer: http://stackoverflow.com/a/3265602/1175077 – jpw Nov 29 '13 at 16:20
  • It's usually compiled into assembly. Although that doesn't mean no interpreter exists (in fact, there does). – Aleph Nov 29 '13 at 16:20
  • 1
    Languages are not "compiled" or "interpreted". Their implementations might be located somewhare on "compiled-interpreted" spectrum. – SK-logic Nov 29 '13 at 16:20
  • Is there any reason for the downvotes? as far as am not a native for this language you are free to correct me. – Hanoch Nov 29 '13 at 16:29
  • 6
    I guess it's mostly because you could have easily found this information with a minimum of research effort. In addition to that, as you can see from the discussion in the answers and the comments to them, the definition of "interpreted language" itself is somewhat unclear. – Hulk Nov 29 '13 at 16:39
  • I want to improve this question by editing it, but I have a question for moderators. Is it allowed to change the topic when i edit it? like can i ask something about Android or PHP? – Hanoch Jul 06 '14 at 19:09

3 Answers3

11

A programming language is simply a textual representation of abstract principles. It is not compiled or interpreted - it is just text.

A compiler will take the language and translate it into machine language (assembly code), which can easily be translated into machine instructions (most systems use a binary encoding, but there are some "fuzzy" systems as well).

An interpreter will take the language and translate it into some byte-code interpretation that can be easily translated into a binary encoding on supported platforms.

The difference between the two is when that change occurs. A compiler typically will convert the text to machine language and package it into a binary file before the user runs the program (e.g. when the programmer is compiling it). An interpreter will typically do that conversion when the user is running the program. There are trade-offs for both approaches.

The whole point here is that the language itself is not compiled nor interpreted; it is just a textual standard. The implementation details of turning that text into machine instructions is where the compilation or interpretation choice is made.

Zac Howland
  • 15,777
  • 1
  • 26
  • 42
8

It's typically compiled, although there is of course nothing preventing people from implementing interpreters.

It's generally wrong to classify languages as either/or; what is the language going to do? It's just a spec on paper, it can't prevent people from implementing it as either a compiler or an interpreter, or some combination/hybrid approach.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
  • 5
    I don't think the user will find this answer useful in any way... – Claudio Nov 29 '13 at 16:25
  • I dont really like this argumentation. Just because you can doesnt mean one (pratically) does. C (and C++) are for me, personally, classical examples of compiled languages. – Sebastian Hoffmann Nov 29 '13 at 16:31
  • 1
    @Paranaix Just because you always compile your C/C++ does not mean it always is compiled. There are uses for C/C++ as scripting languages - which are interpreted. – Zac Howland Nov 29 '13 at 16:36
  • @Paranaix, the very notion of a "compiled" or "interpreted" language is just wrong and lame. Calling something lame a "classical example" is not going to make it more credible. – SK-logic Nov 29 '13 at 16:38
  • 2
    It's clear that the user asked for the typical usage of C (i.e., compiled) and a short description between the difference between compiled and intepreted. – Claudio Nov 29 '13 at 16:49
5

There are languages which are designed to make compilation easy, by giving the user only features that directly map to machine instructions, such as arithmetic, pointer manipulation, function calls (and indirect function calls which give you virtual dispatch). Interpretation of these is generally also easy, but particularly poor performance. C is one of these.

Other languages are designed for interpretation. These often have dynamic typing, lazy dispatch, dynamic (not lexical) scope of closures, reflection, dynamic codegen, and other features that make compilation incredibly difficult. Of course difficult is not the same as impossible, and some of these languages do end up with compilers as a result of Herculean efforts.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • All such features are not going to make compilation any more difficult than interpretation. They are simply making it hard to produce an *efficient* implementation, compiled or interpreted. A compiler for a very dynamic language is still going to produce a little bit more efficient code than an interpreter, it just does not worth the effort (due to a weird belief that compilation is more complicated than interpretation, which is wrong in general). – SK-logic Nov 29 '13 at 16:42
  • @SK-logic: Can an interpreter for a dynamic language do some preprocessing or caching and gain performance? Yes. Is that the same as compilation? Well, you can say it's compiling to an intermediate form, but it isn't compiling to native. And one can easily devise an interpretable language that is almost impossible to compile to native. (For example, add primitives that perform textual modification of the source code, such that every line potentially accesses different symbols and even performs different operations than before. Maybe the replacement even splits and/or merges basic blocks) – Ben Voigt Nov 29 '13 at 16:47
  • A compiler can do all the same stuff as interpreter. Even if you allow to modify the code - it's easy to recompile it every time it was modified. There are compiled-only implementations of Lisp, despite the fact there is an `eval` function. – SK-logic Nov 29 '13 at 17:04
  • P.S., and any interpreter can be turned into a compiler - see *Futamura projections*. – SK-logic Nov 29 '13 at 17:10
  • @SK-logic: If you have to run the compiler during execution, then the code is not *compiled* (past tense, compilation complete). In that case execution is based on the source text (as the language description requires), so your overall system qualifies as an interpreter not a compiler. – Ben Voigt Nov 29 '13 at 17:12
  • P.S. It seems Futamara Projections don't apply to self-mutating (non-static) source code. – Ben Voigt Nov 29 '13 at 17:14
  • Your definition of a "compiled" code is clearly different from mine. In fact, there is no precise definition at all, so you're free to use any. – SK-logic Nov 29 '13 at 17:30
  • 1
    Many compilers for language that are not designed to be compiled constantly need to call library helpers or insert glue code to help with runtime typing, dispatch, errorhandling constructs etc. Merely having a compiler for it still doesn't make it equal to a compiled language that was designed as such. – Marco van de Voort Nov 29 '13 at 17:41