- Why the Gregorian calendar needs leap years at all
- The complete leap year rule — including the century-year exceptions
- How to encode that rule in a single boolean expression
- Why
1900isn't a leap year but2000is — a classic gotcha - How to test the function against a range of years
Why leap years exist at all
Earth takes about 365.2422 days to orbit the sun — slightly more than 365 days. If a calendar year were exactly 365 days, the seasons would drift by about a quarter of a day per year. After a century, the calendar would be 25 days off — summer would start in what used to be spring. To keep the calendar aligned with the seasons, we add an extra day (February 29th) every few years. That's the leap year.
The simplest rule: a leap year occurs every 4 years. That accounts for the 0.2422 days per year using a rough 0.25 average — close but not exact. The 0.25 would give about 25 leap years per century, but we need about 24.22. So we skip one leap year every 100 years to correct the drift. But even that's not perfect — the correction overcorrects by a tiny amount, so we add back one leap year every 400 years. The result is the complete Gregorian rule.
That rule — "divisible by 4, unless divisible by 100, unless divisible by 400" — encodes hundreds of years of calendar science. It's a great example of a real-world rule that looks simple but has layers of historical refinement. Writing it in code is an exercise in both boolean logic and understanding why each exception exists.
The simplest version of the rule — "divisible by 4" — is wrong for century years. 1900 % 4 == 0, but 1900 was not a leap year (it's divisible by 100 but not by 400). Similarly, 2100 and 2200 will not be leap years. Only 2000, 2400, and other multiples of 400 are leap years among century years. This is the classic trap in leap year logic, and it's why the full rule matters.
💡 In one line: return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); — that's the whole Gregorian rule. If you can parse that expression, you know the rule.
The complete rule — and why it's more than "divisible by 4"
The full Gregorian leap year rule:
- A year is a leap year if it's divisible by 4...
- ...unless it's also divisible by 100...
- ...unless it's also divisible by 400.
Let's rewrite that as a truth table:
| ÷4? | ÷100? | ÷400? | Leap year? | Example |
|---|---|---|---|---|
| No | No | No | No | 2023, 2025 |
| Yes | No | No | Yes | 2024, 2028 |
| Yes | Yes | No | No | 1900, 2100 |
| Yes | Yes | Yes | Yes | 1600, 2000, 2400 |
In C, that rule encodes to a single expression:
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
Read it left to right: "if it's divisible by 4 and not divisible by 100, it's a leap year. Otherwise, if it's divisible by 400, it's a leap year." The two parts cover all four cases in the truth table: the first covers "divisible by 4, not by 100," and the second covers "divisible by 400" (which necessarily implies divisible by 4 and 100). The remaining two cases (not divisible by 4, and divisible by 100 but not 400) fall through and return false.
It's worth noting that the order matters: if you wrote (year % 4 == 0) || (year % 400 == 0) — missing the middle condition — then 1900 would incorrectly be classified as a leap year (since 1900 % 4 == 0). The second part is essential. So is the exclusion year % 100 != 0 inside the first part.
⚠️ The trap: the naive check year % 4 == 0 accepts 1900, 2100, and 2200 as leap years — but they're not. The full rule accounts for the century exception. Testing against a range that includes a century year catches the bug.
A different way to think about it: instead of the boolean expression, use a sequence of if statements — sometimes that's clearer for a first reading:
if (year % 400 == 0) return 1;
if (year % 100 == 0) return 0;
if (year % 4 == 0) return 1;
return 0;
This version is arguably easier to read on first sight. It says: "if divisible by 400, leap year. Else if divisible by 100, not leap year. Else if divisible by 4, leap year. Else not." Each line corresponds to one row of the truth table. Both versions are correct; the boolean-expression version is more compact, the if-chain version is more explicit. Pick whichever your team finds clearer.
Complete C Program
Program: check a single year
#include <stdio.h>
/* Function prototype */
int isLeapYear(int year);
int main(void) {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (isLeapYear(year)) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}
return 0;
}
/* Leap year check — full Gregorian rule */
int isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
And here's a version with the if-chain, which some people find clearer:
Variant: if-chain version
int isLeapYear(int year) {
if (year % 400 == 0) return 1;
if (year % 100 == 0) return 0;
if (year % 4 == 0) return 1;
return 0;
}
This version spells out the four cases explicitly, in the order from most specific to least specific. It's essentially the truth table translated directly into code. Both versions compute the same result — use whichever you prefer.
And a test harness that checks a range of years, including the tricky century cases:
Variant: test a range of years
#include <stdio.h>
int isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int main(void) {
int test_years[] = {1900, 1996, 2000, 2004, 2100, 2023, 2024, 2400};
const char *expected[] = {"not leap", "leap", "leap", "leap",
"not leap", "not leap", "leap", "leap"};
printf("%-6s | %-10s | %s\n", "Year", "Expected", "Result");
printf("-------+------------+-------\n");
for (int i = 0; i < 8; i++) {
int is_leap = isLeapYear(test_years[i]);
printf("%-6d | %-10s | %s\n",
test_years[i], expected[i],
is_leap ? "leap" : "not leap");
}
return 0;
}
The test cases are chosen to hit every branch of the rule: a century year divisible by 400 (2000, 2400), a century year not divisible by 400 (1900, 2100), a typical leap year (1996, 2004, 2024), and a typical non-leap year (2023). If all eight pass, the function handles the rule correctly.
Program Output
A typical run with a leap year:
Enter a year: 2024
2024 is a leap year.
With a common year:
Enter a year: 2023
2023 is not a leap year.
With a century year not divisible by 400 — the classic gotcha:
Enter a year: 1900
1900 is not a leap year.
With a century year divisible by 400:
Enter a year: 2000
2000 is a leap year.
And the test harness output:
Year | Expected | Result
-------+------------+-------
1900 | not leap | not leap
1996 | leap | leap
2000 | leap | leap
2004 | leap | leap
2100 | not leap | not leap
2023 | not leap | not leap
2024 | leap | leap
2400 | leap | leap
Every row matches. The 1900 and 2100 rows are the ones that catch naive implementations — a function that only checks "divisible by 4" would classify them as leap years, which is wrong. Testing against these cases is what verifies the function implements the full rule, not just the first approximation.
Step-by-Step Explanation
Let's look at the one-line rule and then trace through several years.
1. The prototype and definition
int isLeapYear(int year); — same shape as isEven and isPrime. One integer in, one int out (1 for leap, 0 for not). The naming convention isLeapYear reads naturally at the call site: if (isLeapYear(2024)) is clear, whereas if (leapYear(2024)) is slightly more ambiguous about what the return value means.
2. The expression
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) — let's unpack the precedence. % binds tighter than ==, so year % 4 == 0 is (year % 4) == 0. == and != bind tighter than &&, which binds tighter than ||. So the expression is:
((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)
The parentheses in the source make this explicit — a good habit even though the precedence is correct without them.
3. Tracing isLeapYear(2024)
2024 % 4 == 0→ true. (2024 / 4 = 506, no remainder.)2024 % 100 != 0→ true. (2024 / 100 = 20 remainder 24, so the modulo is 24, not 0.)- Left side:
true && true→ true. - Since the left side of
||is true, the right side isn't evaluated. Return 1 (true). ✓
4. Tracing isLeapYear(1900)
1900 % 4 == 0→ true.1900 % 100 != 0→ false. (1900 % 100 = 0, so the condition "not equal to 0" is false.)- Left side:
true && false→ false. - Right side:
1900 % 400 == 0→ false. (1900 % 400 = 300.) - Overall:
false || false→ false. Return 0. ✓
This is the crucial trace. 1900 % 4 == 0 would have returned "leap year" if we'd used the naive rule. The middle clause (1900 % 100 != 0) excludes it, and the right clause (1900 % 400 == 0) confirms it's not rescued by the 400-year exception. So 1900 is correctly classified as a non-leap year.
5. Tracing isLeapYear(2000)
2000 % 4 == 0→ true.2000 % 100 != 0→ false. (2000 % 100 = 0.)- Left side:
true && false→ false. - Right side:
2000 % 400 == 0→ true. (2000 / 400 = 5, exact.) - Overall:
false || true→ true. Return 1. ✓
This is the exception to the exception. 2000 is divisible by 100, so it would be excluded by the middle clause — but it's also divisible by 400, so the right clause rescues it. The rule is designed exactly for this: every 400 years, we skip the skipped leap year, keeping the calendar aligned over centuries.
6. Tracing isLeapYear(2023)
2023 % 4 == 0→ false. (2023 % 4 = 3.)- Left side short-circuits to false (the
&&doesn't evaluate the second operand). - Right side:
2023 % 400 == 0→ false. - Overall:
false || false→ false. Return 0. ✓
7. Why the if-chain and the boolean expression are equivalent
The if-chain version:
if (year % 400 == 0) return 1; // divisible by 400: leap
if (year % 100 == 0) return 0; // divisible by 100 but not 400: not leap
if (year % 4 == 0) return 1; // divisible by 4 but not 100: leap
return 0; // not divisible by 4: not leap
Each line handles one case, and the order ensures we don't miss exceptions. If a year is divisible by 400, the first line returns 1 and the rest don't matter. If it's divisible by 100 but not 400, the first line fails but the second catches it and returns 0. If it's divisible by 4 but not 100, the third line returns 1. If none of those, the final return 0. This is equivalent to the boolean expression — just written out more explicitly. For readers unfamiliar with && and ||, the if-chain is easier to follow.
8. When was the Gregorian rule adopted?
The Gregorian calendar was introduced by Pope Gregory XIII in 1582, replacing the Julian calendar (which used the simpler "divisible by 4" rule). At the time of introduction, the calendar had drifted 10 days out of alignment with the seasons — the reform skipped 10 days to realign. Most Catholic countries adopted it immediately; Protestant countries adopted it over the following centuries (England in 1752, Russia in 1918, Greece in 1923). That's why historical dates can be ambiguous — a date like "October 10, 1582" doesn't exist in the Gregorian calendar, since it was one of the skipped days.
9. Leap year and February 29th
In a leap year, February has 29 days instead of 28, and the year has 366 days instead of 365. This affects any date arithmetic code: computing the day of week, calculating the difference between two dates, or formatting month lengths. That's where isLeapYear becomes a building block — a daysInMonth(int month, int year) function would call isLeapYear(year) to determine February's length. Once you have this function, day-of-week and date-difference algorithms become tractable.
10. Range limits
The rule assumes the Gregorian calendar. For years before 1582, it's historically inaccurate — the Julian calendar was in use, with different leap year rules. For years before 45 BCE (when the Julian calendar was introduced), even the "divisible by 4" rule doesn't apply — Rome used a completely different system. For programming purposes, most code assumes the Gregorian rule applies to all years, which is fine for modern dates but subtly wrong for historical ones. Our function doesn't try to handle this — it just applies the rule, which is what 99% of code wants.
🔍 Try this: temporarily change the function to just return year % 4 == 0; — the naive rule. Run the test harness. You'll see 1900 and 2100 incorrectly classified as leap years. Then restore the full rule and re-run — the tests pass. This is the most direct way to see why the century exception is essential.
Frequently Asked Questions
1. Why isn't a year divisible by 4 always a leap year?
Because the year isn't exactly 365.25 days — it's about 365.2422. If we used 0.25 (divisible by 4), we'd add a leap year every 4 years, which is slightly too much. Over a century, that's 25 extra days instead of 24.22, causing a drift of about 18 hours per century. The century exception (skip the leap year when divisible by 100) corrects for this. The 400 exception (add it back when divisible by 400) corrects for the overcorrection of the century rule. That's the full Gregorian rule.
2. Was 1900 a leap year?
No. 1900 is divisible by 4 and by 100, but not by 400. Under the Gregorian rule, that makes it a non-leap year. February 1900 had 28 days, not 29. The year 2000 was a leap year (divisible by 400), and 2100 will not be. This is one of the most common errors in leap year implementations — the naive "divisible by 4" rule gets it wrong.
3. Was 2000 a leap year?
Yes. 2000 is divisible by 4, by 100, and by 400. The middle rule (skip century years) is overridden by the 400 rule (restore every 400 years). February 2000 had 29 days. This was a notable event — 1900 and 2100 are not leap years, but 2000 is, making it the first leap year most people alive today remember at a century boundary.
4. Is the rule ever going to change?
Possibly. The Gregorian calendar has a slight error even with the 400-year correction — about one day every 3,236 years. Some proposals add an extra rule: a year divisible by 4000 is not a leap year. This isn't currently adopted anywhere, but it's mathematically cleaner. For now, the Gregorian rule is what everyone uses, and it's accurate enough for the next few thousand years.
5. Why does 2024 count as a leap year but 2023 doesn't?
Because 2024 is divisible by 4, while 2023 isn't. 2024 % 4 = 0, and 2024 % 100 = 24 (not 0), so it's not a century year. That's a regular leap year under the rule. February 2024 had 29 days. 2023, being not divisible by 4, had 28 days in February.
6. What's a "common year"?
A year that's not a leap year — i.e., 365 days, with 28 days in February. The terminology contrasts "leap year" (366 days) with "common year" (365 days). Every year is one or the other. The ratio is roughly 1 leap year for every 4 common years, but with the century exceptions, it's exactly 97 leap years per 400 years.
💡 Tip: Once this works, use it to compute the number of days in each month — write a daysInMonth(month, year) function that returns 31, 30, 28, or 29 depending on the month and whether the year is a leap year. That's the natural next step, and it gives you a foundation for date arithmetic (day of week, days between dates, etc.).