已知当前日期和出生日期计算年龄c语言
时间: 2023-09-07 16:17:53 浏览: 91
你好,以下是C语言代码示例,可以计算出从出生日期到当前日期的年龄。
```
#include <stdio.h>
#include <time.h>
int main() {
time_t current_time;
struct tm *current_date;
int birth_year, birth_month, birth_day;
int current_year, current_month, current_day;
int age;
// 获取当前日期和时间
time(¤t_time);
current_date = localtime(¤t_time);
current_year = current_date->tm_year + 1900;
current_month = current_date->tm_mon + 1;
current_day = current_date->tm_mday;
// 获取出生日期
printf("请输入你的出生年月日(格式为YYYY/MM/DD):");
scanf("%d/%d/%d", &birth_year, &birth_month, &birth_day);
// 计算年龄
age = current_year - birth_year;
if (current_month < birth_month || (current_month == birth_month && current_day < birth_day)) {
age--;
}
printf("你今年%d岁了\n", age);
return 0;
}
```