Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8378709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:59:21+00:00 2026-06-09T15:59:21+00:00

Under C++ or <stdbool.h> from C99, how is the less-than operator < defined for

  • 0

Under C++ or <stdbool.h> from C99, how is the less-than operator < defined for boolean values?

Alternatively, explain the behaviour of this code:

#ifndef __cplusplus
#include <stdbool.h>
#endif
#include <stdio.h>

int main() {
    bool b = -1;
    if(b < true) {
        printf("b < true\n");
    }
    if(b < false) {
        printf("b < false\n");
    }
    if(true < false) {
        printf("true < false\n");
    }
    if(false < true) {
        printf("false < true\n");
    }
}

Under MSVC version 10, compiled as C++ code, GCC 4.6.3-ubuntu5 compiled as C code and G++ 4.6.3-1ubuntu5 compiled as C++ code, all you get is

false < true

That is, the following inequalities are all false:

(bool)-1 < true
(bool)-1 < false
true < false

And the following is true:

false < true
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:59:22+00:00Added an answer on June 9, 2026 at 3:59 pm

    In C++ (and I suspect in C as well), bools compare exactly as if
    false were 0 and true were 1. And if the type is bool, no
    values other than true and false are possible.

    When comparing bool to other numeric types, it will convert to int,
    again with false converting to 0 and true converting to 1.

    Edit: Both C++ and stdbool.h in C99 also force boolean values to be either 0 (false) or 1 (true) – bool b = -1; sets the value of b to 1. Since 1 < 1 and 1 < 0 are both false, the inequalities in the question are correct.

    Edit: (by James) Except that the above edit isn’t really correct, at
    least for C++. A bool doesn’t have a value of 0 or 1, it has a value
    of false or true. It’s only when it is promoted to int that the
    conversion creates the values of 0 and 1.

    And as Konrad has pointed out, there is no conparison of bool values.
    The “usual arithmetic conversions” occur for the comparison operators,
    which means integral promotion on both of the operands, which means
    bool converts to int (as does char or short… or an enum).

    All of which is rather technical. In practice, you can remember that
    false < true, or you can consider false is 0 and true is 1,
    whichever works best for you. The only important thing to remember is
    that a bool can have no other values.

    (Interestingly, I don’t think that the bit patterns of a bool are
    imposed by the standard. An implementation could use the bit patterns
    0x55 and 0xAA, for example, as long as all conversions to an
    integral type gave 0 and 1, conversion to bool always gave the
    appropriate value, etc. Including zero initialization of static
    variables.)

    And one final note: bool b = -1; sets b to -1 != 0 (which is
    true, not 1, but of course, true will convert to 1 in any
    numeric context.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Under what situations would this code error out with System.StackOverflowException? Accounts.Sort((x, y) => string.Compare(x.AccountId,
This is the code #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h>
Under what exact circumstances do @SessionAttributes get cleared? I've discovered some confusing behaviour when
under Netbeans ,I created a project containing an interface i_object This interface represents the
Under what circumstances will the False part of the following code be executed? x
Under Windows 7 I created a custom autostart entry for an usb device. This
Under my project folder, I find the following folder: Bin\Debug\ This folder has DLLs
Under this grammar: ^ + - * / < > = <= >= and
Under MSVC 9.0, this fails. Under g++ this compiles. If we take out the
Under my VS2010 solution I've this situation: WEBSITE Library1 Library2 On global.asax.cs I initialize

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.