1

Why does the int hum2() function returning a boolean type? Shouldn't the function return the type I defined it as. Like Float function returns float value of Double function returns a double value.

#include<stdio.h>

int hum2()
{
    return true;
}
bool hum(){

    return false;
}



int main()
{

    printf("%d\n",hum2());
    printf("%d\n",hum());

    return 0;
}
10
  • 2
    Automatic type promotion. Bool converts to int Commented Oct 24, 2021 at 9:21
  • 1
    What makes you think that it does return boolean? Commented Oct 24, 2021 at 9:23
  • 2
    The answer depends on the language you are using, because C did not initially know about boolean or bool. So please pick the language you want an answer for and do not tag the other one. I assume that you ask about C++ and not C, because the shown code does compile as C++ but not as C. E.g. here tutorialspoint.com/compile_c_online.php and here tutorialspoint.com/compile_cpp_online.php Commented Oct 24, 2021 at 9:24
  • 1
    I don't understand the question. Why do you think hum2 is returning a boolean type here? It's clearly returning an int. Commented Oct 24, 2021 at 9:36
  • 1
    In C++ with cout etc you can have it print out the bool if you turn on the boolalpha option en.cppreference.com/w/cpp/io/manip/boolalpha Commented Oct 24, 2021 at 10:11

3 Answers 3

1

For starters to make the program a correct C program you need to include the header stdbool.h.

#include <stdbool.h>

In this header true and false are macros that expand to integer constants correspondingly 1 and 0.

And the macro bool expands to the integer type _Bool.

So there is neither conversion takes place in your program except in the call of printf where an object of the type _Bool is promoted to the type int.

In C++ the boolean literals true and false are converted to values of the return type of the first function. The literal true is converted to the integer value 1 and the literal false is converted to the integer value 0.

From the C++ 14 Standard (4.5 Integral promotions)

6 A prvalue of type bool can be converted to a prvalue of type int, with false becoming zero and true becoming one.

Thus either your program is compiled using a C compiler or a C++ compiler in any case the functions returns integer values if the functions return type is int.

Sign up to request clarification or add additional context in comments.

3 Comments

One complement : in C, 0 is false, any other value is true.(eg: 42 is true)
@PtitXav Just like in C++, any value other than 0 is true including negative numbers.
@Ptitxav: It's quite a different topic. Vlad is speaking about what are results of converting boolean values false and true to the int type, whilst your comment is about what int values are converted to false and to true.
1

Why does function return bool when it is defined as an int?

Well, why it returns is because that's the way you wrote it, but I guess you're asking, how/why does this work?

And the answer is that for convenience, C is perfectly willing to perform a number of implicit conversions between values of different types. This is usually a good thing, and once you get used to it, you'll appreciate it.

Let's look at the lines you were probably puzzled about.

int hum2()
{
    return true;
}

If function hum2 is declared as returning int, how can you return this Boolean value from it? Well, deep down, a Boolean is just a number, generally either 0 or 1. So there's no problem returning that number as an int. The compiler typically doesn't even warn you. This is partly because, once upon a time, C did not even have a separate Boolean type, and everybody used int (or some other integer type) to store their 1/0 true/false values.

The situation here is kind of like declaring a function

double one()
{
    return 1;
}

Here function one is declared as returning type double, yet we return an int. How does this work? Well, again, the compiler is perfectly willing to perform an implicit conversion.

The other line you were probably puzzled about was this one:

printf("%d\n", hum());

Now, function hum was declared as returning bool, so how can you get away with printing it as a %d? Don't the extra arguments you pass to printf, and the %-specifiers you use in the format string, have to match exactly?

Yes, they do have to match, but there are three additional factors. First, there is no %-specifier for bool. Second, there's also an additional set of rules that comes into play, because printf is special. Third, since a Boolean value is actually just a number, 0 or 1, we will find that underneath, type bool is actually going to be implemented as an integer type, int or short int or char.

Since printf accepts a variable number of arguments, something called the default argument promotions come into play. These say that every integer type smaller than int is automatically promoted to int, and also (though it doesn't matter here), that type float is promoted to double. So function hum's return value gets promoted from bool to int, and it's perfectly fine to print it with %d.

Comments

-1

Here, as you are using the integer specifier (%d) in printf("%d\n", hum()); , the boolean value is implicitly getting casted to integer. Since with printf there is no special specifier for boolean, outputting true or false directly to console cannot be possible.

But you can use std::cout with std::boolapha flag to get the desired output

std::cout << std::boolalpha << hum() << std::endl ;

or the not so good option with printf("%s", hum() ?"true":"false");

1 Comment

%d in printf does not cause a conversion of an argument to an integer. Also, there is no such thing as an implicit cast. A cast is an explicit operator ((type) in C or C++, and static_cast<type>(expression) and similar forms in C++) that performs a conversion. That makes it an explicit conversion. There are implicit conversions. %d does not cause an implicit conversion of any argument.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.