UP HOME

Black Magic in C

[2026-03-15 Sun]

Recently I was revisiting some C quirks and stumbled upon a few small tricks that highlight how flexible (and sometimes confusing) the language can be.

Infinite Loop Using unsigned char Overflow

#include <stdio.h>

int main() {
    for (unsigned char i = 0; i < 256; i++) {
        printf("%u ", i);
    }
}

Looks normal… but never ends.

Why ?

An unsigned char typically stores values from 0 to 255. When i reaches 255 and increments 255 +1 -> 0; Unsigned integers in C use modular arithmetic, meaning they wrap around instead of overflowing. So i never becomes 256.

That makes the condition i < 256 always True which results in an infinite loop

Infinite Loop Using for(;;)

for (;;) {
  // runs forever
 }

This works because the syntax for a for is semantically described by the C Standard as follows (§6.8.5.3 ISO/IEC 9899)

The statement

for ( clause-1 ; expression-2 ; expression-3 ) statement

behaves as follows:

  • The expression expression-2 is the controlling expression that is evaluated before each execution of the loop body. The expression expression-3 is evaluated as a void expression after each execution of the loop body. If clause-1 is a declaration, the scope of any variables it declares is the remainder of the declaration and the entire loop, including the other two expressions; it is reached in the order of execution before the first evaluation of the controlling expression. If clause-1 is an expression, it is evaluated as a void expression before the first evaluation of the controlling expression.
  • Both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a nonzero constant.

So for(;;) is equivalent to while(1)

The Subscript Operator []

The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2)))

This makes it totally valid to write c code like this:

#include <stdio.h>

int main() {
    int a[] = {10, 20, 30};
    printf("%d\n", 1[a]);
}

This prints 20.

This works because array indexing in C is defined as a[i] ≡ *(a + i)

And addition is commutative: *(a + i) ≡ *(i + a)

So i[a] is perfectly valid.

Why These Tricks Matter

They reveal some deep truths about C:

  • C trusts the programmer
  • Syntax is more flexible than it appears
  • Arrays are just pointer arithmetic in disguise

C doesn't try to protect you.

It gives you power,and lets you surprise yourself.

Recommendation

Another great video from Tsoding: https://youtu.be/gtk3RZHwJUA

In this talk, he explains a clever trick where metadata about a dynamic array is stored right before the pointer itself. This allows the array to behave like a normal pointer while still keeping track of useful information like its size and capacity.

It's a neat example of how low-level memory layouts can be used to build higher-level abstractions in C.


Thanks for reading!

Subscribe to get future posts via email

(Or grab the rss feed )


Add a comment


Comments (0)


Copyright © 2026 Abderrahmane Faiz

learn_in_public@afaiz.dev