【发布时间】:2019-07-16 17:23:19
【问题描述】:
如果整数变量year是闰年,我想给布尔变量leapYear赋值(闰年是4的倍数,如果是100的倍数,也必须是400的倍数。 )在一个c程序中
这是我尝试过的代码。
bool leapYear;
int year;
printf("Enter a year ");
scanf("%d", &year);
if (year %4 = 0 || year %100 = 0 || year %400 = 0)
printf("true");
我试图编译我编写的代码,但它给出了一个错误,提示未知类型名称'bool'。
【问题讨论】:
-
与问题无关:你所有的
=都应该是== -
请
#include <stdbool.h> -
另外,你的闰年逻辑是错误的,它对所有 4 的倍数都返回 true,它不能正确地处理世纪异常。见stackoverflow.com/questions/3029417/…
-
使用_Bool inatead
-
即使您使用
_Bool b = true;,您仍然需要#include <stdbool.h>才能接受true。
标签: c boolean boolean-expression