RE: Introduction to C Programming [Part 2: your first program] 01-20-2015, 07:49 PM
#9
(01-20-2015, 02:18 AM)0xDEAD10CC Wrote: Tell this to the C99 standard (that you obviously haven't read) before you pretend to know what you're talking about
bool Is an alias for _Bool, and is large enough to hold 1 and 0.
_Bool is a one byte storage unit, the same as a char. It is large enough to hold a value between 0 and 255 (or -128 to +127).
C99 6.3.1.2:
Quote:When any scalar value is converted to _Bool, the result is 0 if the value compares equal
to 0; otherwise, the result is 1.
That implies that it is a single bit field, or can only hold the value 0 or 1, but in reality, the compiler is actually messing with any scalar. the following will output 1:
Code:
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
_Bool a = 5;
printf("%d\n", a);
return 0;
}Which would imply that it was single bit. But in that case, then setting it to the value 4 should force it to output zero, but it comes out to be 1 again.
Code:
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
_Bool a = 5;
char *b = (char *)&a;
*b = 5;
printf("%d\n", a);
return 0;
}now it comes out to be 5. Since the largest number a 1 bit field can hold is 1 (2^1-1), this disproves that it is only large enough for 0 and 1. Testing this once more, with negative numbers.
Setting the value to -5 results in 251 being printed, so we know that -5 is the value
11111011
which, in unsigned binary is the value 251. And just for a test, let's throw in 256 just to see if it is 0 (if the field is larger than one byte, it will be 256, else 0).
Funny thing:
Quote:warning: overflow in implicit constant conversionWell, lets run it just to see..hey, look at that, it is in fact 0.
So, we can conclude that _Bool is EXACTLY the same data structure as unsigned char.
Any more snotty comments you wish to add or can you go back to playing on the playground wrapped in bubble wrap and talking to walls?
I'm closing this thread now. It seems that programming threads tend to spark semantic trolls, and I am tired of cleaning up the mess.
(This post was last modified: 01-20-2015, 08:39 PM by phyrrus9.)


























![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)