Skip to content
Tech News
← Back to articles

The C ``Clockwise/Spiral Rule''

read original more articles
Why This Matters

The 'Clockwise/Spiral Rule' offers a simple, systematic method for C programmers to interpret complex declarations quickly and accurately. This technique enhances understanding and reduces errors, benefiting both learners and seasoned developers in the tech industry. Mastering this rule can streamline coding workflows and improve code readability across projects.

Key Takeaways

The ``Clockwise/Spiral Rule''

By David Anderson

There is a technique known as the ``Clockwise/Spiral Rule'' which enables any C programmer to parse in their head any C declaration!

There are three simple steps to follow:

Starting with the unknown element, move in a spiral/clockwise direction; when ecountering the following elements replace them with the corresponding english statements: [X] or [] => Array X size of... or Array undefined size of... (type1, type2) => function passing type1 and type2 returning... * => pointer(s) to... Keep doing this in a spiral/clockwise direction until all tokens have been covered. Always resolve anything in parenthesis first!

Example #1: Simple declaration

+-------+ | +-+ | | ^ | | char *str[10]; ^ ^ | | | +---+ | +-----------+

``str is an...

We move in a spiral clockwise direction starting with `str' and the first character we see is a `[' so, that means we have an array, so... ``str is an array 10 of...

Continue in a spiral clockwise direction, and the next thing we encounter is the `*' so, that means we have pointers, so... ``str is an array 10 of pointers to...

... continue reading