> For the complete documentation index, see [llms.txt](https://mentebinaria.gitbook.io/manual-da-linguagem-gnu-c/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mentebinaria.gitbook.io/manual-da-linguagem-gnu-c/7.-expressoes-de-atribuicao/armadilha-atribuicao-em-subexpressoes.md).

# Armadilha: Atribuição em Subexpressões

Em C, a ordem de cálculo das partes de uma expressão não é fixa. Com exceção de alguns poucos casos especiais, as operações podem ser calculadas em qualquer ordem. Se uma parte da expressão tem uma atribuição para `x` e outra parte da expressão usa `x`, o resultado é imprevisível, pois esse uso pode ser calculado antes ou depois da atribuição.

Aqui está um exemplo de código ambíguo:

```c
x = 20;
printf ("%d %d\n", x, x = 4);
```

Se o segundo argumento, `x`, for calculado antes do terceiro argumento, `x = 4`, o valor do segundo argumento será 20. Se eles forem calculados na outra ordem, o valor do segundo argumento será 4.

Aqui está uma maneira de tornar esse código não ambíguo:

```c
y = 20;
printf ("%d %d\n", y, x = 4);
```

Aqui está outra maneira, com o outro significado:

```c
x = 4;
printf ("%d %d\n", x, x);
```

Esse problema se aplica a todos os tipos de atribuições, e aos operadores de incremento e decremento, que são equivalentes a atribuições. Veja [Ordem de Execução](#user-content-fn-1)[^1] para mais informações sobre isso.

No entanto, pode ser útil escrever atribuições dentro de uma condição `if` ou um teste `while`, junto com operadores lógicos. Veja [Operadores Lógicos e Atribuições](#user-content-fn-1)[^1].

[^1]: Capítulo pendente de tradução


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://mentebinaria.gitbook.io/manual-da-linguagem-gnu-c/7.-expressoes-de-atribuicao/armadilha-atribuicao-em-subexpressoes.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
