Mastering the C Clockwise Spiral Rule for Parsing Declarations
The C ``Clockwise/Spiral Rule''
I introduce a simple three-step technique called the Clockwise Spiral Rule that allows any C programmer to mentally parse even the most complex declarations. By starting with the unknown element and moving in a spiral direction to resolve tokens like arrays, pointers, and functions, you can translate confusing syntax into clear English statements. This method handles everything from basic pointers to intricate function signatures involving const and volatile qualifiers.
There is a technique known as the Clockwise Spiral Rule which enables any C programmer to parse in their head any C declaration!
- dang
Related. Others?
C "clockwise/spiral" rule to understand declarations - https://news.ycombinator.com/item?id=42564861 - Jan 2025 (75 comments)
Clockwise/Spiral Rule - https://news.ycombinator.com/item?id=25494219 - Dec 2020 (16 comments)
The Clockwise/Spiral Rule of C declarations - https://news.ycombinator.com/item?id=12775735 - Oct 2016 (68 comments)
The “Clockwise/Spiral Rule” - https://news.ycombinator.com/item?id=8648287 - Nov 2014 (26 comments)
The Clockwise/Spiral Rule - https://news.ycombinator.com/item?id=6471202 - Sept 2013 (7 comments)
The "Clockwise/Spiral Rule" in C - https://news.ycombinator.com/item?id=5079787 - Jan 2013 (45 comments)
- pcfwik
Being taught this rule in undergrad really hampered my appreciation of C. As I've said in a previous comment, the real key that unlocked understanding C declarations for me is the mantra "declaration follows use." You declare a variable in C in exactly the same way you would use it: if you know how to use a variable, then you know how to read and write a declaration for it. Once I understood this elegant idea, it became hard to enjoy using statically typed languages that eschew it.
It is explained in more detail at this link:
- tancop
My ideal syntax for function pointers is:
fn signal(signum: int, fp: fn(int -> void)) -> fn(int -> void)
It keeps the parameter and return types inside, makes it obvious that it's a function using the fn keyword (or func or whatever), short and readable.
When you add more parameters you get `fn(int, char -> int)`. That's the only sane way to handle it and it also supports multiple return values if you want them.
Bonus: all type modifiers should be prefix like `?*MyStruct` and control flow should be postfix `task.await.match { ... }`. Every language should either have a pipe operator or let you call any function with method syntax. `x |> f |> g` is better than `g(f(x))`.
- WalterBright
D simply reads right to left:
int[]* p; // pointer to array of int
int function(char*) fp; // pointer to function with char* parameter returning an int
- Joker_vD
> "str is an array 10 of pointers to char"
Wow, imagine if it was possible to actually use a language like that do declare the type of the variable like that? Something like
str: array [0..9] of ^Char;
or even
var str [10]*uint8
Just imagine...