C - Printing bool true and not 1

ghidu

Senior member
Feb 28, 2005
331
0
0
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
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Hmmmm...I'm confused. You want a parameter to use in printf to print out true instead of 1 for a boolean variable?
 

cKGunslinger

Lifer
Nov 29, 1999
16,408
57
91
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.
 

ghidu

Senior member
Feb 28, 2005
331
0
0
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"

 

cKGunslinger

Lifer
Nov 29, 1999
16,408
57
91
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.
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Can you tell us what you are trying to accomplish with this or give us some background on the program you want to make?
 

ghidu

Senior member
Feb 28, 2005
331
0
0
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"

 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
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");
 

icelazer

Senior member
Dec 17, 1999
323
0
71
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");
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
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.
 

ghidu

Senior member
Feb 28, 2005
331
0
0
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.
 

igowerf

Diamond Member
Jun 27, 2000
7,697
1
76
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)) ) );
 

cKGunslinger

Lifer
Nov 29, 1999
16,408
57
91
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?
 

cKGunslinger

Lifer
Nov 29, 1999
16,408
57
91
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
 

ghidu

Senior member
Feb 28, 2005
331
0
0
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.

 

cKGunslinger

Lifer
Nov 29, 1999
16,408
57
91
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.
 

itachi

Senior member
Aug 17, 2004
390
0
0
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]);
 

igowerf

Diamond Member
Jun 27, 2000
7,697
1
76
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.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
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.
 
Sep 29, 2004
18,656
67
91
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.
 

TerryMathews

Lifer
Oct 9, 1999
11,464
2
0
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.
 

itachi

Senior member
Aug 17, 2004
390
0
0
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.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
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]);