1

is there a exec() / eval() function in C??

for example..

exec(printf(3 + 4))

or

eval(printf(3 + 4))

i've done this in python... but i don't know how to do this in C and C languages..

i understand that this might be harmful in python as it can take any input.. so to overcome that i came to C which is more strict

this is just a experiment i am curious to learn

an explaination would be appreiciated

i am using sublime text (just in case...:] )

Adam
  • 2,820
  • 1
  • 13
  • 33
CodeMaster
  • 35
  • 1
  • 8
  • No `C` standard doesn't support any such functions. `C` is strictly typed language. – kiran Biradar Jun 05 '20 at 08:39
  • No, there is not a built-in function like that. See discussion of why here: https://www.quora.com/What-is-a-method-in-C++-to-evaluate-strings-like-eval-in-Python#:~:text=C%2B%2B%20programs%20can%20run%20on,but%20it's%20a%20bit%20tricky. There are weird ways to do something similar, but they are slow and not worth the trouble: https://stackoverflow.com/questions/39091681/writing-eval-in-c – jdaz Jun 05 '20 at 08:40
  • I am not sure tha C have this built in functions like. In C language, if you wish to `execute` some code you need to `fork()` and `execv` (family of executions) or to execute function by spawning a `thread` and let him do the job. Or to `load` dynamic library, `SO` – Adam Jun 05 '20 at 08:41
  • Adam do you have a link / example to use fork() and execv ()? – CodeMaster Jun 05 '20 at 08:51
  • i don't know how to spawn thread , load either.... kind of a begginer in c.. – CodeMaster Jun 05 '20 at 08:53

2 Answers2

1

Python is a simplified Lisp and in Lisp there is the eval/apply paradigm. In C this paradigm exists only inside the compiler. The compilation system of C uses eval/apply, also called the Maxwell equations of software. But at user level you do not interact with eval/apply, only if you write/develop in C a compiler for a language you cope with.

So in C you need to implement this yourself, in case you develop some language that is Turing complete (a Turing machine is equivalent with eval+apply cycle).

alinsoar
  • 15,386
  • 4
  • 57
  • 74
1

An eval() function is typical of interpreter based languages. An interpreter is a "compiler on the fly" that interprets (hence its name) the program text and executes the program commands immediately.

An interpreter based language requires a program to be running that can read the program text and execute its instructions.

C is a compiler based language which means that the program text is translated "off-line" into an executable format that can run without a program to interpret the program text.

Because C does not have such an interpreter program running (and so the program does not have a language system "on board"), it has no way to interpret C program text in an eval function. For that reason, such functions do not exist in C.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41