Learn C
#include "operators/op_special.h"

sizeof, Ternary & Comma in C

#concept

Beyond arithmetic live C's signature operators. sizeof reports a type's or object's byte size at compile time — the foundation of portable allocation (malloc(n * sizeof *p)) and array-length math (sizeof arr / sizeof arr[0], valid only before decay). The ternary ?: is an expression-level if/else for tiny branches, and the comma operator evaluates left-to-right yielding the last value — mostly seen in for-loop headers.

Keep ternaries one level deep (nested ternaries are a readability hazard) and treat comma outside for-headers as a smell — sequence points are better expressed as separate statements.

#how it works
1

sizeof is compile-time

sizeof(int) is typically 4; sizeof on an array gives total bytes. Zero runtime cost — the compiler substitutes a constant of type size_t.

2

Ternary for tiny branches

printf("%s\n", n == 1 ? "item" : "items"); picks a value inline where if/else would need four lines.

3

Comma sequences

for (i = 0, j = n - 1; i < j; ++i, --j) runs two counters — the legitimate home of the comma operator.

4

Don't nest ternaries

Two levels deep, rewrite as if/else — future readers (including you) will thank present you.

#example
#include <stdio.h>
 
int main(void) {
int arr[5] = {0};
size_t n = sizeof arr / sizeof arr[0];
int temp = 22;
printf("%zu, %s\n", n, temp > 25 ? "warm" : "cool");
return 0;
}
Output
5, cool
1sizeof arr is 20 bytes and sizeof arr[0] is 4, counting 5 elements at compile time (note: no parentheses needed on expressions).
2temp > 25 is false for 22, so the ternary yields "cool".
3%zu prints the size_t count correctly — %d would be undefined behavior on some platforms.
#use cases
Counting static arrays portably (before they decay to pointers)
Sizing malloc calls as n * sizeof *p so types stay in sync
Inline labels and plurals with a single readable ternary
Dual-counter loops with comma-separated init and step

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C yourself — building beats watching.