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

  • SEARCH
  • Home
  • 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 8920791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:22:02+00:00 2026-06-15T06:22:02+00:00

Consider the code: int foo(void) { _Bool b = 1; // is true int

  • 0

Consider the code:

int foo(void)
{
    _Bool b = 1; // is true
    int i = 42;  // mean true in conditions

    if (i == b)
        return 1;
    else if ((_Bool)i == b)
        return 2;
    else
        return 3;
}

What will this function return according to the C standard (C99 or C11)?

P.S. On GCC 4.7.2 with -std=c99 option the function returns 2.

  • 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-15T06:22:03+00:00Added an answer on June 15, 2026 at 6:22 am

    It returns 2.

    The “usual arithmetic conversions” are applied to the operands of the == operator. _Bool is an unsigned type with a conversion rank lower than that of int or unsigned int.

    The “usual arithmetic conversions” are specified in section 6.3.1.8 “Usual arithmetic conversions” of either the C99 or C11 standard (the links are to recent drafts).

    In i == b, since there are no floating-point operands, the “integer promotions” are applied to both operands, promoting the right operand from _Bool to int and yielding the value 1. Now both operands are of the same type, so no further conversion is needed. So i == b is equivalent to 42 == 1, which is false.

    In (_Bool)i == b, the “integer promotions” are still applied to both operands, so both are converted from _Bool to int. (The conversion from int to _Bool and back to int loses information, converting all non-zero values to 1.) Now both operands are of the same type, so no further conversion is needed. So (_Bool)i == b is equivalent to 1 == 1, which is true — and the function returns 2.

    (Note: Due to some carelessness on my part in reading the standard, this is about the third major edit of this answer. I think I’ve got it right this time.)

    In answer to a question in the comments, b == 42u promotes b from _Bool to int, resulting in an int vs. unsigned int comparison. Since the operands have different signedness but the same conversion rank, the signed operand is converted to the unsigned type. Converting 1 from int to unsigned int is trivial, yielding 1u. So the comparison is equivalent to 1u == 42u, which is false.

    (Note that the == operator in C, like the other equality and relational operators, yields a result of type int with the value 0 or 1, not a result of type _Bool.)

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

Sidebar

Related Questions

Consider this code: int main() { int e; prn(e); return 0; } void prn(double
Consider this code: public void example(String s, int i, @Foo Bar bar) { /*
Consider this code: void res(int a,int n) { printf(%d %d, ,a,n); } void main(void)
Consider this code: if(initscr() == NULL) ERROR(Cannot start ncurses mode.\n); keypad(stdscr, TRUE); cbreak(); int
Consider the following code: struct Foo { mutable int m; template<int Foo::* member> void
Code goes first: void foo(int x) { void bar(int); //is this forward-decl legal? bar(x);
Consider the following code template<unsigned int N> void foo(std::bitset<N> bs) { /* whatever */
Consider the following code: int SomeField; void Foo([Pure] Func<int, object> getData) { Contract.Requires(getData !=
This code will produce error in c++ // Foo.cpp const int Foo = 99;
Consider this piece of code: #include <iostream> #include <vector> template<typename A> void foo(A& a)

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.