# Why is ccall syntax?

**URL:** <https://discourse.julialang.org/t/why-is-ccall-syntax/72427>\
**Category:** General Usage\
**Tags:** ccall\
**Created:** [December 2, 2021, 4:41am UTC](https://discourse.julialang.org/t/why-is-ccall-syntax/72427 "2021-12-02T04:41:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [December 2, 2021, 4:41am UTC](https://discourse.julialang.org/t/why-is-ccall-syntax/72427/1 "2021-12-02T04:41:16Z")

</div>

`ccall` isn’t a function or macro but syntax. Why does it need to be syntax?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [December 2, 2021, 4:57am UTC](https://discourse.julialang.org/t/why-is-ccall-syntax/72427/2 "2021-12-02T04:57:49Z")

</div>

well `@ccall` is a macro. I guess the answer is simply that because nothing is lower-level than `ccall`? it doesn’t make sense to make a function `ccall` just so it eventually perform what we now call `ccall`.

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [December 2, 2021, 7:13am UTC](https://discourse.julialang.org/t/why-is-ccall-syntax/72427/3 "2021-12-02T07:13:05Z")

</div>

Good question. There’s a few possible answers…

#### The basic answer

It doesn’t need to be syntax. `@ccall` is a more recent and much more attractive syntax for the same thing which doesn’t need special support from the parser or lowering.

#### The historical answer

`ccall` dates from _very_ early in Julia’s development. Perhaps around [this commit](https://github.com/JuliaLang/julia/commit/18f456233b280f2b5649c033c1977133c73bfd88) where it was first added to the runtime or [this commit](https://github.com/JuliaLang/julia/commit/e9fbc5c52ff123da27b9cc368b77372fa0618574) where the syntax became more similar to its current form.

This was back when Julia source code was still stored in `.j` files, and the first `ccall` commit was before macros were even implemented as part of [this commit](https://github.com/JuliaLang/julia/commit/a1b9fee4e72e8438693eddc25d769fab4cc98c9d).

#### The pedantic answer

`ccall` is parsed in the same way as a normal function call and it’s represented the same way in the AST. So it’d be somewhat reasonable to say it’s _not_ special syntax at all. (This is almost true, though I admit there’s some special processing for the name `ccall` as part of syntax lowering. I’m not 100% sure whether this is essential or just a convenient place to put this.)

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [December 2, 2021, 7:48am UTC](https://discourse.julialang.org/t/why-is-ccall-syntax/72427/4 "2021-12-02T07:48:55Z")

</div>

Here are some of the examples I am looking at

```julia
julia> n = 2; t = (Cstring, repeat([Cint], n)...); ccall(:printf, Cvoid, t, "%d\n", 1, 1)
ERROR: syntax: ccall argument types must be a tuple; try "(T,)" around REPL[10]:1
Stacktrace:
 [1] top-level scope
   @ REPL[10]:1

julia> n = 2; t = (Cstring, repeat([Cint], n)...); @code_lowered ccall(:printf, Cvoid, t, "%d\n", 1, 1)
ERROR: UndefVarError: ccall not defined

```

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [December 2, 2021, 8:35am UTC](https://discourse.julialang.org/t/why-is-ccall-syntax/72427/5 "2021-12-02T08:35:26Z")

</div>

Yes, I think these oddities are due to the way that lowering processes the `ccall` arguments — apparently it doesn’t produce very good error messages when combined with `@code_lowered`.

For variadic arguments, it’s rather neater to use `@ccall`:

```julia
julia> @ccall printf("%d\n"::Cstring; 12345::Cint)::Cint;
12345

```

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [December 2, 2021, 9:00am UTC](https://discourse.julialang.org/t/why-is-ccall-syntax/72427/6 "2021-12-02T09:00:51Z")

</div>

Is it possible to splat into `@ccall`?

```julia
@ccall printf("%d\n"::Cstring, $((i::Cint for c in 1:5)...))::Cint
ERROR: LoadError: ArgumentError: args in @ccall need type annotations. '$((i::Cint for c = 1:5)...)' doesn't have one.
Stacktrace:

```

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [December 2, 2021, 9:25am UTC](https://discourse.julialang.org/t/why-is-ccall-syntax/72427/7 "2021-12-02T09:25:09Z")

</div>

> [@jzr](#):
>
> Is it possible to splat into `@ccall` ?

You can splat into the AST which `@ccall` is a part of, provided you do it before macro expansion. For example:

```julia
@eval @ccall printf("%d %d %d %d %d\n"::Cstring, $((:($i::Cint) for i in 1:5)...))::Cint

```

But it’s unlikely that it’s a good idea to use `@eval` like this! What’s your surrounding code look like for this use case?

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [December 2, 2021, 9:52am UTC](https://discourse.julialang.org/t/why-is-ccall-syntax/72427/8 "2021-12-02T09:52:12Z")

</div>

> [@c42f](#):
>
> What’s your surrounding code look like for this use case?

This is just for quick interactive hacking.

> [@c42f](#):
>
> it’s unlikely that it’s a good idea to use `@eval` like this!

But I am not familiar with the problems of using `@eval` in this way – what should I know about it?

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [December 2, 2021, 11:12pm UTC](https://discourse.julialang.org/t/why-is-ccall-syntax/72427/9 "2021-12-02T23:12:13Z")

</div>

> [@jzr](#):
>
> But I am not familiar with the problems of using `@eval` in this way – what should I know about it?

For quick hacking in the REPL it’s fine! Also it’s good for top-level evaluation of expressions when doing code generation. For example, if you’re generating a bunch of function definitions to go into your module at precompile time.

The main surprise people have with `@eval` (and `eval`) is that it evaluates the expression at global scope, not in the local scope where the `eval` is called. This is great for making it possible to generate fast code, but it means it’s usually a mistake to use `eval` inside a function.
