c语言中leap的含义
在C语言中,leap关键字用于判断一个年份是否为闰年。
闰年定义
一个闰年包含366天,比非闰年多一天。为了使日历与地球围绕太阳运行的时间保持同步,每四年增加一个闰年。根据公历,一个闰年的判定规则如下:
立即学习“C语言免费学习笔记(深入)”;
能被4整除,但不能被100整除。 能被400整除。leap的使用
在C语言中,可以使用leap关键字来检查一个年份是否为闰年。leap关键字返回一个布尔值:如果年份是闰年,则返回true,否则返回false。
语法格式如下:
<code class="c">#include <stdio.h> #include <stdbool.h> bool leap(int year);</code>
其中:
#include示例
以下是一个示例代码,演示了如何使用leap关键字:
<code class="c">#include <stdio.h>
#include <stdbool.h>
bool leap(int year) {
if (year % 4 == 0 && year % 100 != 0) {
return true;
} else if (year % 400 == 0) {
return true;
} else {
return false;
}
}
int main() {
int year;
printf("请输入年份:");
scanf("%d", &year);
if (leap(year)) {
printf("%d是闰年。\n", year);
} else {
printf("%d不是闰年。\n", year);
}
return 0;
}</code> 