C Program to Check a Leap Year
C Program to Check a Leap Year
A school exercise needs a precise leap year decision, including the equality case rather than guessing from a typical value.
A Gregorian year is leap when divisible by 400, or when divisible by 4 but not by 100.
Before this exercise, review operators and conversions and decisions and conditions. The worked program below applies those ideas to this specific problem.
Explain
Explain why leap year uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Use 1900 and explain which part of the rule changes the result.
Leap Year: follow the data
Given data
A school exercise needs a precise leap year decision, including the equality case rather than guessing from a typical value.
Rule
A Gregorian year is leap when divisible by 400, or when divisible by 4 but not by 100.
Observed result
Result: leap
Understand the operation
A century boundary overrides the usual divisible-by-four rule unless the century is also divisible by 400. Group the alternatives explicitly.
A condition selects a label after the input has been represented as the intended type. The result of a comparison is not a replacement for validating the range of external input.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
int main(void) {
int value = 2000;
printf("Result: %s\n", value % 400 == 0 || (value % 4 == 0 && value % 100 != 0) ? "leap" : "not leap");
return 0;
}
Expected output
Result: leap
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int value = 2000;
printf("Result: %s\n", value % 400 == 0 || (value % 4 == 0 && value % 100 != 0) ? "leap" : "not leap");
return 0;
}
value starts at 2000.
The predicate value % 400 == 0 || (value % 4 == 0 && value % 100 != 0) selects the label leap; the changed sample 1900 selects not leap.
Follow the worked calculation
Start with the sample
value starts at 2000.
Apply the rule
The predicate value % 400 == 0 || (value % 4 == 0 && value % 100 != 0) selects the label leap; the changed sample 1900 selects not leap.
Check the result
Result: leap
Result: leap
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int value = 2000;
printf("Result: %s\n", value % 400 == 0 || (value % 4 == 0 && value % 100 != 0) ? "leap" : "not leap");
return 0;
}
Use 1900 and explain which part of the rule changes the result.
Worked answer
The complete answer uses 1900; following the condition produces not leap. Check the condition rather than infer the answer from the input’s appearance.
C17 · complete program
#include <stdio.h>
int main(void) {
int value = 1900;
printf("Result: %s\n", value % 400 == 0 || (value % 4 == 0 && value % 100 != 0) ? "leap" : "not leap");
return 0;
}
Expected output
Result: not leap
Run in C compiler →Recognize the important boundaries
This is the Gregorian-calendar rule for a positive year; historical local calendar adoption is not modeled.
Use == for equality and = for assignment. Adding an assignment inside a condition can silently replace the value you intended to test.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
A Gregorian year is leap when divisible by 400, or when divisible by 4 but not by 100.
Continue with combining and interpreting conditions.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.