# Does Zig guarantee left-to-right order of evaluation?

**URL:** <https://ziggit.dev/t/does-zig-guarantee-left-to-right-order-of-evaluation/2684>\
**Category:** Help\
**Tags:** language\
**Created:** [January 2, 2024, 2:51pm UTC](https://ziggit.dev/t/does-zig-guarantee-left-to-right-order-of-evaluation/2684 "2024-01-02T14:51:22Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![mlugg](https://ziggit.dev/user_avatar/ziggit.dev/mlugg/32/437_2.png) [@mlugg](https://ziggit.dev/u/mlugg)\
**Post date:** [January 3, 2024, 5:17am UTC](https://ziggit.dev/t/does-zig-guarantee-left-to-right-order-of-evaluation/2684/3 "2024-01-03T05:17:56Z")

</div>

> [@IntegratedQuantum](#):
>
> However, given that zig needs to be compatible to C in some degree (code can be translated from C to zig in some capacity to import headers into zig) I would guess that this behavior will stay.

As briefly mentioned by OP, this behavior is not guaranteed in C. Regarding evaluation order, the C standard, roughly speaking, states that side effects will never be reordered past a “sequence point” (\*). For instance, C’s comma operator `,` is defined as a sequence point, so `a,b` is guaranteed to perform any side effects of `a` before evaluating `b`. Similarly, the end of a statement (i.e. a semicolon) is a sequence point. Most standard operators, like `+` and `=`, are _not_ sequence points in C, so the compiler is free to reorder the side effects of sub-expressions as it wishes.

Nowadays, this restriction seems rather less necessary: compilers are generally smart enough to figure out when they can reorder side effects, elide loads, etc if it would help with optimization. As such, whilst I do not know if it has been formally defined at any point, I do expect Zig to ultimately include the status quo behavior in its spec, where sequencing of side effects always happens in the order of the source code (which I can confirm is guaranteed in the current compiler implementation, and in fact would be quite difficult for us to change due to how the compiler pipeline is structured).

\*: It’s technically defined differently since C11, but the new definition is basically equivalent.

---

_[View the full topic](https://ziggit.dev/t/does-zig-guarantee-left-to-right-order-of-evaluation/2684)._
