0

I want to assign true to the boolean variable leapYear if the integer variable year is a leap year (A leap year is a multiple of 4, and if it is a multiple of 100, it must also be a multiple of 400.)in a c program

Here's the code I tried.

bool leapYear;
int year;

printf("Enter a year ");
scanf("%d", &year);
if (year %4 = 0 || year %100 = 0 || year %400 = 0)
    printf("true");

I tried to compile the code I wrote but it gives an error saying unknown type name 'bool'.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Alex
  • 57
  • 1
  • 3
  • 12

3 Answers3

5

You have to include the header file <stdbool.h>

In this file bool is defined as a macro and expands to the C standard unsigned integer type _Bool.

Any value that is not equal to 0 is converted to 1 and assigned to a variable of the type _Bool.

To set for example a variable to true You could just write

bool leapYear = 1;

The header file also contains macros for true and false.

Here is a demonstrative program

#include <stdio.h>
#include <stdbool.h>

int main(void) 
{
    bool leapYear = true;

    printf( "leapYear = %u\n", leapYear );

    return 0;
}

Its output is

leapYear = 1

If you do not want to include the header then you could just use the standard integer type _Bool.

For example

#include <stdio.h>

int main(void) 
{
    _Bool leapYear = 1;

    printf( "leapYear = %u\n", leapYear );

    return 0;
}

Or even the type int like

int leapYear = 1;

In C logical operations return integer 1 if an expression is true or 0 otherwise.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

You would need to #include <stdbool.h> to use booleans, but in C we usually use an integer. The way it works is 0 is false, anything else is true.

In your case,

int leapYear = 0;    // default to false
... 
leapYear = 1;    // set to true. Any non-zero value works.
Zachary Oldham
  • 838
  • 1
  • 5
  • 21
  • 2
    `bool` *is* a standard type since C99 – Eugene Sh. Jul 16 '19 at 17:27
  • @EugeneSh. sorry, wrong terminology. What I meant was it is not included by default, you have to include it yourself. – Zachary Oldham Jul 16 '19 at 17:28
  • 3
    `_Bool` is a standard type that is available without inclusion of any headers. So still, yes, booleans are "included by default". They need to be, because `_Bool` has special rules that don't apply to any other type. `bool` is just an alias for `_Bool`, provided for convenience by stdbool.h. – John Bollinger Jul 16 '19 at 17:42
  • @JohnBollinger It is my understanding that using `_Bool` is considered bad practice – Zachary Oldham Jul 16 '19 at 17:54
  • 3
    Dunno where you got that idea, @ZacharyOldham. If anything, I'd be inclined to put it the other way around -- using `bool` in C is potentially problematic because it risks colliding with code that provides its own definition for that symbol, and such code isn't particularly uncommon. But that's entirely beside the point. `_Bool`s are booleans. The `bool` of stdbool.h is the same type, but you don't need the header to have booleans. (And `_Bool` *is* an integer.) – John Bollinger Jul 16 '19 at 18:21
1

As everyone has pointed out, your main problem was failure to #include <stdbool.h>. Also, your use of = instead of == in the conditional. I, personally, would not try to use a single conditional because the rules for leap year are complex and hard to translate into a single statement. I'd use a function for clarity:

#include <stdbool.h>

bool is_leap(int year) {
    if (0 == (year % 400)) return true;
    if (0 == (year % 100) && year > 1582) return false;
    if (0 == (year % 4)) return true;
    return false;
}

int main(int argc, char *argv[]) {
    . . .
    bool leap = is_leap(2019);
    . . .
}
Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
  • Off by 1. 1700 is often considered a non-leap year [ref](https://www.quora.com/Why-are-years-such-as-1700-1800-1900-not-leap-years-even-though-they-are-divisible-by-4-but-1600-and-2000-are). Suggest `0 == (year % 100) && year >= 1700` or `0 == (year % 100) && year > 1582` – chux - Reinstate Monica Jul 17 '19 at 02:39