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...:] )

5
  • No C standard doesn't support any such functions. C is strictly typed language. Commented Jun 5, 2020 at 8:39
  • No, there is not a built-in function like that. See discussion of why here: quora.com/…. There are weird ways to do something similar, but they are slow and not worth the trouble: stackoverflow.com/questions/39091681/writing-eval-in-c Commented Jun 5, 2020 at 8: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 Commented Jun 5, 2020 at 8:41
  • Adam do you have a link / example to use fork() and execv ()? Commented Jun 5, 2020 at 8:51
  • i don't know how to spawn thread , load either.... kind of a begginer in c.. Commented Jun 5, 2020 at 8:53

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

2 Comments

Interesting reference, "Maxwell equations of software"
its going to be difficult I_I
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.