Well, actually there is a way...
At runtime you could write a .c file containing the functions definitions and in main you would write the call to the function named in argv[1]
. Then you could invoke a compiler (gcc-linux or cl-windows or whatever) (via system(command)
) to generate an executable. Then you invoke that executable (via system(command)
) and voilà job done.
What? I didn't say it's a practical way, I just said it's a way.
Proof that this actually works:
#include <stdio.h>
#include <stdlib.h>
const char file_includes[] = "#include <stdio.h>\n";
const char file_functions[] =
"void print() {\n"
" printf(\"print function called\\n\");\n"
"}\n"
"void print2() {\n"
" printf(\"second print function called\\n\");\n"
"}\n";
const char file_main_prolog[] = "int main() {\n";
const char file_main_epilog[] = " return 0;\n}\n";
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("invalid program arguments\n");
return 1;
}
FILE *fout = fopen("function_call.c", "w");
fprintf(fout, "%s", file_includes);
fprintf(fout, "%s", file_functions);
fprintf(fout, "%s", file_main_prolog);
fprintf(fout, " %s();\n", argv[1]);
fprintf(fout, "%s", file_main_epilog);
fclose(fout);
system("gcc -o function_call function_call.c");
system("./function_call");
return 0;
}
This works under linux with gcc installed. You can run this program with the argument print
or print2
and you will get print function called
or second print function called
respectively.