# SIZE — Size In Elements

Source: https://rungs.dev/docs/ladder-logic/instructions/size

> SIZE in rungs.dev counts the elements of an array and stores the count in a tag, so a rung never has to hardcode how long an array is.

Counts the elements of an array and stores the count in the destination tag.

## What it does [#what-it-does]

`SIZE` keeps an array's length out of your logic. A rung written against a hardcoded `12`
silently breaks the day someone resizes the tag to `20`; a rung written against a `SIZE` result
keeps working.

It reads nothing from inside the array — only how long it was declared — so it never faults and
never touches the data.

## Operands [#operands]

| Name          | Type                          | Notes                                        |
| ------------- | ----------------------------- | -------------------------------------------- |
| `array`       | any array tag, or `.DATA`     | The array itself — `Samples` or `Samples[0]` |
| `dimension`   | the number `0`                | Which dimension to measure                   |
| `destination` | `DINT`, `INT`, `SINT`, `REAL` | Where the count goes                         |

> **INFO**
>
> **The whole array, not an element**
>
> `Samples` and `Samples[0]` both mean "the whole array". `Samples[3]` is refused — there is no
> such thing as the size of a run starting part-way along. That is the opposite of
> [`COP`](https://rungs.dev/docs/ladder-logic/instructions/cop-cps), where you must name one element.

`dimension` asks which dimension to measure. Studio supports
[single-dimension arrays only](https://rungs.dev/docs/tags/arrays#restrictions), so it is always `0`.

`SIZE(Msg.DATA, 0, Count)` counts the 82 characters a `STRING` can hold, whatever its `.LEN`
says. The whole `STRING`, `SIZE(Msg, 0, Count)`, is refused: it is not an array.

A narrow destination wraps like any other store: a 300-element array counted into a `SINT`
leaves `44`, not `127`.

## Example — Loop to the End of the Array [#example--loop-to-the-end-of-the-array]

```ladder
XIC(Scan)SIZE(Samples,0,Count);LT(Index,Count)OTE(MoreToDo)
```

`Count` always matches the array, so resizing `Samples` needs no change to the rung.

## Structured Text [#structured-text]

`SIZE` has a Structured Text form as well — see the
[Structured Text SIZE page](https://rungs.dev/docs/structured-text/instructions/size).

## Related [#related]

- [Array instructions overview](https://rungs.dev/docs/ladder-logic/instructions/array-instructions)
- [Arrays](https://rungs.dev/docs/tags/arrays) — declaring one
- [SIZE in Structured Text](https://rungs.dev/docs/structured-text/instructions/size)