# How to retrieve global variable in C

**URL:** <https://discourse.julialang.org/t/how-to-retrieve-global-variable-in-c/35139>\
**Category:** New to Julia\
**Tags:** question, embedding\
**Created:** [February 25, 2020, 9:09pm UTC](https://discourse.julialang.org/t/how-to-retrieve-global-variable-in-c/35139 "2020-02-25T21:09:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nnn](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@nnn](https://discourse.julialang.org/u/nnn)\
**Post date:** [February 25, 2020, 9:09pm UTC](https://discourse.julialang.org/t/how-to-retrieve-global-variable-in-c/35139/1 "2020-02-25T21:09:04Z")

</div>

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

```plaintext
#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](https://docs.julialang.org/en/v1/manual/embedding/) but it does not touch on arbitrary variable retrieval.

[![enter image description here](https://global.discourse-cdn.com/julialang/original/3X/6/5/658ea4ea56f8c1092a698c4127d9dfa82126607a.png)](https://i.stack.imgur.com/uTzzs.png)

---

<div class="post-metadata">

**Author:** ![nnn](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@nnn](https://discourse.julialang.org/u/nnn)\
**Post date:** [February 25, 2020, 9:09pm UTC](https://discourse.julialang.org/t/how-to-retrieve-global-variable-in-c/35139/2 "2020-02-25T21:09:54Z")

</div>

Source code shows [similar usage](https://github.com/JuliaLang/julia/search?p=1&q=jl_get_global&unscoped_q=jl_get_global) for `jl_get_global`.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [February 25, 2020, 9:17pm UTC](https://discourse.julialang.org/t/how-to-retrieve-global-variable-in-c/35139/3 "2020-02-25T21:17:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nnn](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@nnn](https://discourse.julialang.org/u/nnn)\
**Post date:** [February 25, 2020, 9:24pm UTC](https://discourse.julialang.org/t/how-to-retrieve-global-variable-in-c/35139/4 "2020-02-25T21:24:28Z")

</div>

> 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?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [February 25, 2020, 9:56pm UTC](https://discourse.julialang.org/t/how-to-retrieve-global-variable-in-c/35139/5 "2020-02-25T21:56:10Z")

</div>

> [@nnn](#):
>
> How do I get the result directly?

It’s returned from the eval call.

> [@nnn](#):
>
> Or should I transform the value to string in Julia code and use `jl_string_ptr` on that?

Yes.

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