News for hackers. Stuff that matters.
Re:Did Ken, Ritchie and Brian choose wrong with NUL-terminat... by SAI_Peregrinus on Tuesday January 16, 2018 @02:29PM (#16161425)
Most CPUs have a flags register, and typically have a "zero" flag which is set when the result of the last operation was zero. Zero is special in the vast majority of hardware designs. Checking for null (zero) instead of another specific value often saves a few cycles. That's where the optimization of having all FOR loops count down towards zero comes from, the check saves a cycle or two each iteration on some CPUs. The same thing happens when reading from a buffer, the load instruction will set the Zero flag when the terminating null is read.

The difference doesn't matter much on modern (non-embedded) processors, but it did make sense at the time C was designed. It matches the most common hardware design pattern better than the alternatives.

There are no comments on this comment yet.