How to retrieve global variable in C

I want to retrieve global variable x I just set in Julia from my C application.
Here’s the code I have so far:

#include <julia.h>

void SimpleExecute(char *command, char *resultVar, char* result) {

	jl_eval_string(command);

	jl_value_t *var = jl_get_global(jl_base_module, jl_symbol(resultVar));

	const char *str = jl_string_ptr(var);

	sprintf(result, "%s", str);
}

int main(int argc, char *argv[])
{
	char* result = malloc(sizeof(char) * 1024);

    jl_init();

    //(void)jl_eval_string("println(sqrt(2.0))"); //works
    (void)SimpleExecute("x=sqrt(2.0)", "x", result);

    jl_atexit_hook(0);
    return 0;
}

However debugger shows that var is still NULL after jl_get_global call. Why?
I followed this tutorial but it does not touch on arbitrary variable retrieval.

enter image description here

Source code shows similar usage for jl_get_global.

Your code is equivalent to x = sqrt(2.0); Base.x, you are not getting the variable in the right module.

You don’t need to assing to a global and get the result like this, you can get the result directly.

Also, the use of jl_string_ptr is invalid, you did not have a string as a result to begin width.

you can get the result directly.

How do I get the result directly?

you did not have a string as a result to begin width.

Is there a C call to get the string or string representation of requested variable (regardless of type)? Or should I transform the value to string in Julia code and use jl_string_ptr on that?

It’s returned from the eval call.

Yes.

Note that I don’t think it’s a good idea to do everything in string.