Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

#include <stdbool.h>

Name: Anonymous 2011-10-14 14:26

The header stdbool.h in the C Standard Library for the C
programming language contains four macros for a Boolean data
type. This header was introduced in C99, which was adopted as
an ANSI standard in May 2000. 11 years ago.


#ifndef _STDBOOL_H
#define _STDBOOL_H

#define bool _Bool
#define true 1
#define false 0
#define __bool_true_false_are_defined 1

#endif


Spread the word!

Name: Anonymous 2011-10-14 14:29

yeah okay, so what's _Bool supposed to be

Name: Anonymous 2011-10-14 14:29

C99 is still not fully supported. i will stick with C98 thank you

Name: Anonymous 2011-10-14 14:39

how bool is better than int?

Name: Anonymous 2011-10-14 14:49

>>2
Built-in compiler defined type. Identifier names with an underscore as a prefix are reserved as for future language expansions and compiler and library implementors. However, bool was not a reserved identifier in C89.

stdbool.h was created so that C99, and future versions of C such as C1x would remain backwards compatible with C89 code. For example, many C developers would create their own bool typedefs. On new projects, you can optional include stdbool.h. On existing projects, things work as per normal.

>>3
C99 is mostly full supported, except with Microsoft's shitty compiler toolchain.

>>4
Better semantics and type-safety. Plus bool can be implemented as a single byte (with ICC, GCC and Clang you choose this with a compiler flag), but int has to be at least 16-bits according to the language standard, as the minimum allowed values for INT_MAX and UINT_MAX in limits.h are 32767 and 65535 respectively. In practice with modern compilers and on most current platforms, int is a 32-bit integer type--but you already knew that.

Name: Anonymous 2011-10-14 14:50

>>4

sizeof(_Bool)

Name: Anonymous 2011-10-14 15:02

>>5

Out of curiosity, I always wondered why people didn't use char instead of int for representing booleans in pre-C99 code.

Name: VIPPER 2011-10-14 15:59

bool can be implemented as a single byte
How is that supposed to work?

Name: Anonymous 2011-10-14 17:28

>>5,7
Interestingly, the spec doesn't place an upper bound on the size of _Bool; the size must be enough to hold 1 or [/code]0[/code].

Name: Anonymous 2011-10-14 18:06

>>9

It wouldn't make sense to put an upper bound on sizeof(_Bool), because that could give a performance penalty due to alignment issues. Thus, there's no relative performance bonuses of using _Bool instead of anything else for that purpose; instead, I suppose the only advantage of _Bool is a little more explicit semantics associated with the type.

Name: Anonymous 2011-10-14 18:16

>>10
TBH the main reason I use bool in my code is so the filthy VC-using republicans will get an error when they try to compile it.

Name: Anonymous 2011-10-14 22:53

>>11

I lol'd cuz that has happened to me.

Name: Anonymous 2011-10-15 4:03

>>11
Agreed. I used to rely on stdint.h for this purpose, but VC10 finally got stdint.h so now we need to use more esoteric things like stdbool.h and tgmath.h.

Name: Anonymous 2011-10-15 4:09

>>7
Probably because they're faster to use, and losing 1-3 bytes of memory isn't exactly a huge loss.

Name: Anonymous 2011-10-15 5:08

>>8
1 = true
0 = false
>>7
I do.

Coming from an asm background, I don't see much need for bool as I'd extend my functions to return more than true/false (e.g. instead of returning true/1 for error, return a non-zero error reason.)

Name: Anonymous 2011-10-15 5:13

>>5
type-safety.
Only faggots care about typesafety. Because typesafe languages always miss `eval` function, they have no ability for reproduction, meaning they are faggots of programming languages. So, if your language doesnt have `eval`, you are a fag.

Name: Anonymous 2011-10-15 5:16

Hey fags, what is the type of (eval (read))? Is it "typesafe"?

Name: FrozenVoid 2011-10-15 5:19

If your code uses Bool its defective Sepples shit.
You don't need any "Boolshit" in any program. Bitfields with 1bit storage already provide all you need for flags and boolean switches. An int can have 32 1-bit bitfields which would takes 32 to 128 bytes as Bools in C++.

Name: VIPPER 2011-10-15 5:23

>>15
What i was really wondering was if using _bool has any performance advantages over checking a char for non-0 value.

Name: VIPPER 2011-10-15 5:24

>>17
Its a cons.

Name: Anonymous 2011-10-15 5:25

>>20
wrong

Name: FrozenVoid 2011-10-15 5:28

>>19
 you can write bitmasks to check the entire int at once,
While you check 32 bools each time separately, i would check many ints(even more with SSE,POPCNT and clever bithacking)
And it would be easy to toggle them at once:
http://graphics.stanford.edu/~seander/bithacks.html#ConditionalSetOrClearBitsWithoutBranching

Name: Anonymous 2011-10-15 5:36

Name: Anonymous 2011-10-15 5:36

Name: Anonymous 2011-10-15 5:39

Name: Anonymous 2011-10-15 5:40

Name: Anonymous 2011-10-15 6:36

>>19
I don't think the standard mentions anything about that, a specific compiler might be clever about it however. But as far as I know optimization isn't quite there yet.

Name: Anonymous 2011-10-15 10:48

#define false 0
#define true !false

Name: Anonymous 2011-10-17 17:51

>>28
Sir, you are a genius.

Don't change these.
Name: Email:
Entire Thread Thread List