Leap Year Test in K&R

By Susam Pal on 01 Mar 2020

I learnt the following test for leap year early in my programming life from the book The C Programming Language, 2nd ed. (K&R) written by Brian Kernighan and Dennis Ritchie about 18 years ago during my engineering studies. § 2.5 (Arithmetic Operators) of the book uses the following test:

(year % 4 == 0 && year % 100 != 0) || year % 400 == 0

By the way, the following test also works:

year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
Comments