简体   繁体   English

C 编程语言,如何在代码中转换输入字符串并运行它[暂停]

[英]C programming language,how to convert input string in code and run it [on hold]

Is there a way to transform what the user inputs in code?有没有办法转换用户在代码中输入的内容? like if就像如果

char k[50]; 
printf("insert the code to insert");
scanf("%s", &k);

function => converts string and execute the string if the code is correct function => 转换字符串,如果代码正确则执行字符串

for example if the user insert "int a = 0;"例如,如果用户插入"int a = 0;" as input, the program run the input string?作为输入,程序运行输入字符串?

What you're looking for is essentially something like the eval function that some languages has.您正在寻找的内容本质上类似于某些语言所具有的eval function。

Nope, there is not a way to do that.不,没有办法做到这一点。 At least not in a practical way.至少不是以实际的方式。 The reason is quite simple.原因很简单。 In C, you need to compile the complete program before you run it.在C中,运行前需要编译完整的程序。 If there is even a single syntax error in you 10k+ lines of code, you will not get an executable binary from the compiler.如果在 10k+ 行代码中甚至存在单个语法错误,您将无法从编译器中获得可执行的二进制文件。

Python on the other hand can execute line by line.另一方面,Python 可以逐行执行。 That's why you can execute Python code that has syntax errors.这就是为什么您可以执行有语法错误的 Python 代码的原因。 The program will not crash until you encounter the error.在您遇到错误之前,程序不会崩溃。

Besides, you should always think twice before using something like this irregardless of the language.此外,无论使用哪种语言,在使用这样的东西之前都应该三思而后行。 The risks for vulnerabilities are extremely high.漏洞风险极高。

Contrary to some other languages, the C language doesn't have an eval function to do what you want.与其他一些语言相反,C 语言没有 eval function 来做你想做的事。 The only option is to write the source file into a file and then execute the compiler available on the host machine.唯一的选择是将源文件写入文件,然后执行主机上可用的编译器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM