C - Printing bool true and not 1

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

Nothinman

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

Some people find the ?: notation easier to read, but there's the little fact that the compiler will treat that exactly as an if/then/else statement.

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

And that begs the question, is it better to waste the memory for the arrays or to just deal with a simple conditional?
 

TerryMathews

Lifer
Oct 9, 1999
11,464
2
0
I was just pointing out that there was another option, it was just a smart response when a person said that the method he/she listed was the only way to do it without using a conditional.

Also someone said that typecasting bool like I did is undefined (paraphrasing). It might be, but I've typecast bool in VC++ 6 in the standard namespace before without trouble. Of course, really anything you do in C++ that's slick/fast/cool is undefined in one flavor or another.

Outside of these three methods (conditional, logical operators, if/then/else statements), I really don't think there's anything else you can do. Even if you go at it using a function, whatever function you create would have to fall back to one of those three methods.

I'd love to be proven wrong too. :)
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: TerryMathews
I was just pointing out that there was another option, it was just a smart response when a person said that the method he/she listed was the only way to do it without using a conditional.

Also someone said that typecasting bool like I did is undefined (paraphrasing). It might be, but I've typecast bool in VC++ 6 in the standard namespace before without trouble. Of course, really anything you do in C++ that's slick/fast/cool is undefined in one flavor or another.

Outside of these three methods (conditional, logical operators, if/then/else statements), I really don't think there's anything else you can do. Even if you go at it using a function, whatever function you create would have to fall back to one of those three methods.

I'd love to be proven wrong too. :)

See above :p

 

itachi

Senior member
Aug 17, 2004
390
0
0
Originally posted by: Nothinman
And that begs the question, is it better to waste the memory for the arrays or to just deal with a simple conditional?
storing it as an array wouldn't waste that much memory. the only thing that would need to be stored is the base address for each string.
nope. any non-zero value is interpreted as true, and any zero value as false.
I was just pointing out that there was another option, it was just a smart response when a person said that the method he/she listed was the only way to do it without using a conditional.

Also someone said that typecasting bool like I did is undefined (paraphrasing). It might be, but I've typecast bool in VC++ 6 in the standard namespace before without trouble. Of course, really anything you do in C++ that's slick/fast/cool is undefined in one flavor or another.

Outside of these three methods (conditional, logical operators, if/then/else statements), I really don't think there's anything else you can do. Even if you go at it using a function, whatever function you create would have to fall back to one of those three methods.

I'd love to be proven wrong too.
namespace has nothing to do with it.. C++ doesn't specify that a boolean is a 1-bit value.. all it states is that the amount of memory required to store it is 8-bits. it's not "slick/fast/cool", you're converting between completely different data types. char/short/int/long are all integral data types, bool isn't.

and you have been proven wrong.. i posted a solution that requires no conditionals on the previous page.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
It might be, but I've typecast bool in VC++ 6 in the standard namespace before without trouble.

No one ever said that the VC6 compiler was very good at conforming to any standards, infact a lot of people say the opposite.

storing it as an array wouldn't waste that much memory. the only thing that would need to be stored is the base address for each string.

And the string itself which would be on the stack instead of in the data segment of the binary with the printf calls. In this case, no it won't matter. But it's an ugly work around for no gain.

 

imported_plasmasnake

Junior Member
Aug 28, 2005
17
0
0
My two cents: Mod is an extremely expensive operation, and unnecessary in this case. Use "(num & 1)" for true when odd, or negate for even. =D
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Uhm - did everybody miss it? There is a stream format specifier - boolalpha - that does exactly what the OP is looking for.
 

cKGunslinger

Lifer
Nov 29, 1999
16,408
57
91
Originally posted by: Armitage
Uhm - did everybody miss it? There is a stream format specifier - boolalpha - that does exactly what the OP is looking for.

I sure missed it. :eek:
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Uhm - did everybody miss it? There is a stream format specifier - boolalpha - that does exactly what the OP is looking for.

IIRC that's only for C++, the OP asked for C. He didn't mention that he was really using C++ until 1/2 way through the thread.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Not to slam C/C++ but ADA will do exactly what he was asking with built in functionality for enumerations (which is really what TRUE/FALSE are)
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: Nothinman
Uhm - did everybody miss it? There is a stream format specifier - boolalpha - that does exactly what the OP is looking for.

IIRC that's only for C++, the OP asked for C. He didn't mention that he was really using C++ until 1/2 way through the thread.

AFAIK, C doesn't have a bool type.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
AFAIK, C doesn't have a bool type.

Now that I think about it, you're probably right. But even so the OP should know which language he's trying to use =)
 

TerryMathews

Lifer
Oct 9, 1999
11,464
2
0
Originally posted by: itachi

I'd love to be proven wrong too.
namespace has nothing to do with it.. C++ doesn't specify that a boolean is a 1-bit value.. all it states is that the amount of memory required to store it is 8-bits. it's not "slick/fast/cool", you're converting between completely different data types. char/short/int/long are all integral data types, bool isn't.

and you have been proven wrong.. i posted a solution that requires no conditionals on the previous page.[/quote]

I'm telling you, you can typecast bool to int in VC6. SP5 to be specific.

And, no I haven't been proven wrong. What is your solution, if not logical operators? You've got (words not notation), num and true, and (num or true) and true. Those are logical operators.

I still say that any solution to this problem would utilize one of the three methods listed, whether you use a function (boolalpha) or not. Just because you don't use logical operators or conditionals doesn't mean the function you're calling doesn't.

EDIT: And, just so we're clear... My challenge was intended to get someone to point out a fourth unique method to solve this problem. Something none of us had thought about.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
I'm telling you, you can typecast bool to int in VC6. SP5 to be specific.

You can typecast virtually anything to anything, especially in C, but if you do that in a big-endian machine it won't work.
 

itachi

Senior member
Aug 17, 2004
390
0
0
Originally posted by: TerryMathews
I'm telling you, you can typecast bool to int in VC6. SP5 to be specific.
and i'm telling you that that's a horrible idea to spread. who cares if it worked for you, you're typecasting a type that's not integral by nature to an integral type.
And, no I haven't been proven wrong. What is your solution, if not logical operators? You've got (words not notation), num and true, and (num or true) and true. Those are logical operators.
&& || ! are logical, ~ ^ & | are bitwise (binary) operators. so yea, you have been proven wrong.
 

TerryMathews

Lifer
Oct 9, 1999
11,464
2
0
Originally posted by: itachi|| ! are logical, ~ ^ & | are bitwise (binary) operators. so yea, you have been proven wrong.

They're logical operators in boolean algebra.

You're life must be so incredibly boring to get joy from trying to nit-pick mistakes.

And, even if you were correct, I would've made a semantic error not a syntaxial one. Still not an incorrect statement. You want to prove me wrong? Provide a fourth solution. Stop trying to nit-pick what I posted because you know when you get down to brass tacks that I'm right. You can't attack the crux of any of my arguments so you just pick away at the details.