• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C - Printing bool true and not 1

ghidu

Senior member
I want a boolean function to return true or false and not 1 or 0. I mean I'm printing the value and I want to look like "it's true" and not "it's 1". and I don't want to use IF in the main program.
Thanks
 
Hmmmm...I'm confused. You want a parameter to use in printf to print out true instead of 1 for a boolean variable?
 
Originally posted by: ghidu
I want a boolean function to return true or false and not 1 or 0. I mean I'm printing the value and I want to look like "it's true" and not "it's 1". and I don't want to use IF in the main program.
Thanks
BOOLEAN is a binary type that returns 0 for FALSE, and anything else (typically 1) for TRUE. It is *not* a character-string. You *will* have to do some manipulation in order to associate the actual BOOLEAN values to text/string output.
 
Originally posted by: cKGunslinger
Originally posted by: ghidu
I want a boolean function to return true or false and not 1 or 0. I mean I'm printing the value and I want to look like "it's true" and not "it's 1". and I don't want to use IF in the main program.
Thanks
BOOLEAN is a binary type that returns 0 for FALSE, and anything else (typically 1) for TRUE. It is *not* a character-string. You *will* have to do some manipulation in order to associate the actual BOOLEAN values to text/string output.
Maybe you can shed some light on this. The function is boolean and has to return "true" or "false"

 
Originally posted by: ghidu
Originally posted by: cKGunslinger
Originally posted by: ghidu
I want a boolean function to return true or false and not 1 or 0. I mean I'm printing the value and I want to look like "it's true" and not "it's 1". and I don't want to use IF in the main program.
Thanks
BOOLEAN is a binary type that returns 0 for FALSE, and anything else (typically 1) for TRUE. It is *not* a character-string. You *will* have to do some manipulation in order to associate the actual BOOLEAN values to text/string output.
Maybe you can shed some light on this. The function is boolean and has to return "true" or "false"
Yes, "logical" TRUE and FALSE, not the actual ascii characters T-R-U-E.
 
Can you tell us what you are trying to accomplish with this or give us some background on the program you want to make?
 
Originally posted by: xtknight
Can you tell us what you are trying to accomplish with this or give us some background on the program you want to make?
Let's just say it checks if a number is even or odd.
The example looks like: For the number ....... , the function returns "true"

 
I don't think there's any printf parameter that will print out "true" when a 0 or 1 is specified.

The "true" or "TRUE" you type in your code is replaced by a 1 when the program is compiled. Likewise the "false" and "FALSE" are replaced by a 0. The program doesn't know the difference between FALSE and 0.

This is all you need to do.

if (num%2) // or if string ends with 0,2,4,6,8->even, string ends with 1,3,5,7,9->odd. may save processing for large integers.
printf("number is odd\n");
else
printf("number is even\n");
 
Blech, Poor Programming Pratice...

if (num%2 == 1) // or if string ends with 0,2,4,6,8->even, string ends with 1,3,5,7,9->odd. may save processing for large integers.
printf("number is odd\n");
else
printf("number is even\n");
 
Blech, Poor Programming Pratice...

Not really, anyone who actually knows C should be able to determine what "if (num%2)" means and the compiler should generate the same code for both.
 
Originally posted by: xtknight
I don't think there's any printf parameter that will print out "true" when a 0 or 1 is specified.

The "true" or "TRUE" you type in your code is replaced by a 1 when the program is compiled. Likewise the "false" and "FALSE" are replaced by a 0. The program doesn't know the difference between FALSE and 0.

This is all you need to do.

if (num%2) // or if string ends with 0,2,4,6,8->even, string ends with 1,3,5,7,9->odd. may save processing for large integers.
printf("number is odd\n");
else
printf("number is even\n");

Thanks guys, but what I have to do is print "The number is even - " TRUE ;or FALSE; (return of the boolean function.

EDIT: actually I think there is a parameter for printf, but I'm using cout and I don't remember what parameter, it's been a long time.
 
You can't really avoid an if...then statement, but if you want to keep your code clean looking, just make a function that returns "TRUE" or "FALSE" depending on the boolean you pass to it. Then you could do something like this:

printf("Number is even %s", boolToString(myBoolean));

If you're using a function to determine whether a value is even or odd, you can nest the function calls even further:

printf("Number is even %s", boolToString( isEven(value) ) );
printf("Number is odd %s", boolToString( !(isEven(value)) ) );
 
Originally posted by: ghidu
Originally posted by: xtknight
I don't think there's any printf parameter that will print out "true" when a 0 or 1 is specified.

The "true" or "TRUE" you type in your code is replaced by a 1 when the program is compiled. Likewise the "false" and "FALSE" are replaced by a 0. The program doesn't know the difference between FALSE and 0.

This is all you need to do.

if (num%2) // or if string ends with 0,2,4,6,8->even, string ends with 1,3,5,7,9->odd. may save processing for large integers.
printf("number is odd\n");
else
printf("number is even\n");

Thanks guys, but what I have to do is print "The number is even - " TRUE ;or FALSE; (return of the boolean function.

May I ask your programming background? If you can't see how to modify the simple examples given into doing what you want, I must assume you're not in a CS major. Can we get some more info so as to better have an idea how to proceed with help?

My suggestion is also:

result = functionThatReturnsBoolean();

printf ("The number is even: ");

if (FALSE == result)
{
printf(" FALSE. \n");
}

else
{
printf(" TRUE. \n");
}

Or, if you really do have some artificial reason why you can't use if/then/else logic, how about this:

char textArray[2][10];

textArray[0] = "FALSE\0";
textArray[1] = "TRUE\0;

result = functionThatReturnsBoolean();

printf ("The number is even: %s.\n", textArray[result] );

Syntax is probably not correct, but you get the basic idea, no?
 
Originally posted by: ghidu

Thanks guys, but what I have to do is print "The number is even - " TRUE ;or FALSE; (return of the boolean function.

EDIT: actually I think there is a parameter for printf, but I'm using cout and I don't remember what parameter, it's been a long time.
printf parameters
 
May I ask your programming background? If you can't see how to modify the simple examples given into doing what you want, I must assume you're not in a CS major. Can we get some more info so as to better have an idea how to proceed with help?

My suggestion is also:

result = functionThatReturnsBoolean();

printf ("The number is even: ");

if (FALSE == result)
{
printf(" FALSE. \n");
}

else
{
printf(" TRUE. \n");
}

Or, if you really do have some artificial reason why you can't use if/then/else logic, how about this:

char textArray[2][10];

textArray[0] = "FALSE\0";
textArray[1] = "TRUE\0;

result = functionThatReturnsBoolean();

printf ("The number is even: %s.\n", textArray[result] );

Syntax is probably not correct, but you get the basic idea, no?[/quote]

What I asked is if there is a way to print the string TRUE insted of 1 from the value returned by the boolean function without an if/else because that I already knew how to do it and I didn't ask for help to modify the examples.

 
Originally posted by: ghidu
What I asked is if there is a way to print the string TRUE insted of 1 from the value returned by the boolean function without an if/else because that I already knew how to do it and I didn't ask for help to modify the examples.

And you have received your answer many times over:

The short answer is "No." The longer answer is "Maybe, depending on what other restrictions you have."

"printf" has no built-in option, that I am aware of, for displaying Boolean values as ascii text in the form of "TRUE" and "FALSE." You can, however, use other techniques to manipulate the data into what you want, as has been sugested above.
 
it's best not to antagonize people who are trying to help you.

char *bstr [] = {"TRUE", "FALSE"};
...
int num;
...
printf ("The number if odd - %s", bstr[num & 1]);
printf ("The number is even - %s", bstr[(num + 1) & 1]);
 
Originally posted by: itachi
it's best not to antagonize people who are trying to help you.

char *bstr [] = {"TRUE", "FALSE"};
...
int num;
...
printf ("The number if odd - %s", bstr[num & 1]);
printf ("The number is even - %s", bstr[(num + 1) & 1]);

Slick.
 
Assuming that "n" is some integer, and you want to print "TRUE" if it's even, and "FALSE" if it's odd:

(n % 2) ? printf("FALSE") : printf("TRUE");

I have no clue what this is actually useful for, though.
 
I read the thread.

THERE IS NO WAY TO PRINT "TRUE" OR "FALSE" IN C/C++ WITHOUT USING A CONDITIONAL OPERATION!

Sorry, but you seem to not be hearing what others have lready told you. Your only other option is to create a Boolean class and overload the operators in it to dispaly what you want.
 
Originally posted by: IHateMyJob2004
I read the thread.

THERE IS NO WAY TO PRINT "TRUE" OR "FALSE" IN C/C++ WITHOUT USING A CONDITIONAL OPERATION!

Sorry, but you seem to not be hearing what others have lready told you. Your only other option is to create a Boolean class and overload the operators in it to dispaly what you want.

You could make a two-element array and typecast the bool into an int. Something like this:

bool check;
check = whatever();
string[2] validity {"True","False"};
cout << validity[(int)check];

Note this is not 100% compliant C++ code, you can do your own homework.
 
Originally posted by: TerryMathews
You could make a two-element array and typecast the bool into an int. Something like this:

bool check;
check = whatever();
string[2] validity {"True","False"};
cout << validity[(int)check];

Note this is not 100% compliant C++ code, you can do your own homework.
c++ doesn't specify booleans as 1-bit values.. so '(int) check' could return 0xff for all you know, which would be sign-extended to -1.
 
stirng strState[] = {"FALSE","TRUE")
bool returnState;

.... Code to determine return state

// converts the logical to an integer and uses as an index for strState
printf ("%s",strState[(int)returnState]);
 
Back
Top