C99 boolean data type?

68,984

Solution 1

Include <stdbool.h> header

#include <stdbool.h>

int main(void){
  bool b = false;
}

Macros true and false expand to 1 and 0 respectively.

Section 7.16 Boolean type and values < stdbool.h >

  • 1 The header <stdbool.h> defines four macros.
  • 2 The macro
    • bool expands to _Bool.
  • 3 The remaining three macros are suitable for use in #if preprocessing directives. They are
    • true : which expands to the integer constant 1,
    • false: which expands to the integer constant 0, and
    • __bool_true_false_are_defined which expands to the integer constant 1.
  • 4 Notwithstanding the provisions of 7.1.3, a program may undefine and perhaps then redefine the macros bool, true, and false.

Solution 2

Please do check out the answer here on this related thread found on DaniWeb.

extracted and quoted here for convenient reference:-


usage of new keywords in c99

_Bool: C99's boolean type. Using _Bool directly is only recommended if you're maintaining legacy code that already defines macros for bool, true, or false. Otherwise, those macros are standardized in the <stdbool.h> header. Include that header and you can use bool just like you would in C++.

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

int main ( void )
{
  bool b = true;

  if ( b )
    printf ( "Yes\n" );
  else
    printf ( "No\n" );

  return 0;
}

Share:
68,984
eonil
Author by

eonil

Favorite words: "Make it work, make it right, make it fast" — by Kent Beck? "...premature optimization is the root of all evil (or at least most of it) in programming." - from The Art of Computer Programming, by Donald Knuth. "Yes, but your program doesn't work. If mine doesn't have to work, I can make it run instantly and take up no memory." — from Code Complete, by Steve Mcconnell. "Flat is better than nested." — from The Zen of Python, by Tim Peters "Making decisions is slow." — from The Ninja build system manual, author unknown. "A little copying is better than a little dependency." - from Go Proverbs, by Rob Pike Preferred tools: macOS, iOS, Ubuntu. Rust, Swift, VIM, Xcode. SQLite, PostgreSQL, Redis. And a few more trivial stuffs. Feel free to fix my grammar. I always appreciate!

Updated on November 09, 2020

Comments

  • eonil
    eonil over 3 years

    What's the C99 boolean data type and how to use it?