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

Time:10-24

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;
}

CodePudding user response:

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.

CodePudding user response:

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.

CodePudding user response:

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");

  • Related