Hi there,
I have the following variable
char str[] = "printf("Hello")";
I want to use the value of str as a part of the program code.
How to do that?
In JavaScript there is a function called Eval. Is there any equivalent of it in C?
Hi there,
I have the following variable
char str[] = "printf("Hello")";
I want to use the value of str as a part of the program code.
How to do that?
In JavaScript there is a function called Eval. Is there any equivalent of it in C?
Last edited by shb27; 01-06-2017 at 06:41 AM.
No, there's no such equivalent in C. You need to parse the string manually, either by means of strtok() or by purely your own code.
Devoted my life to programming...
As GReaper says, there is no way to do this using standard C. A quick search however yielded potentially interesting results that may be of use:
See first and third answers here: Convert string to code at run time c++ - Stack Overflow
More info: https://web.stanford.edu/~engler/tickc.pdf
mmm Is there a way to include .c file within main.c? like include function in php?
Another (experimental) gnu possibility: https://gcc.gnu.org/onlinedocs/jit/
You can compile a separate .c file and #include its associated header in your main source file - but you can't include "live" code in this way.
Perhaps if you tell us exactly what you're trying to accomplish, we can offer alternative suggestions.
I managed to include a header and everything seems OK so far.
Actually I'm trying to make a very simple programming language with c for learning purposes.
Thanks for your help.