What can be assumed about the representation of true?
This program returns 0
in my machine:
#include <stdbool.h>
union U {
_Bool b;
char c;
};
int main(void) {
union U u;
u.c = 3;
_Bool b = u.b;
if (b == true) {
return 0;
} else {
return 1;
}
}
AFAICT, _Bool
is an integer type that can at least store 0
and 1
, and true
is the integral constant 1
. On my machine, _Bool
has a sizeof(_Bool) == 1
, and CHAR_BITS == 8
, which means that _Bool
has 256 representations.
I can't find much in the C standard about the trap representations of _Bool
, and I can't find whether creating a _Bool
with a representation different from 0
or 1
(on implementations that support more than two representations) is ok, and if it is ok, whether those representations denote true or false.
What I can find in the standard is what happens when a _Bool
is compared with an integer, the integer is converted to the 0
representation if it has value 0
, and to the 1
representation if it has a value different than zero, such that the snippet above ends up comparing two _Bool
s with different representations: _Bool[3] == _Bool[1]
.
I can't find much in the C standard about what the result of such a comparison is. Since _Bool
is an integer type, I'd expect the rules for integers to apply, such that the equality comparison only returns true if the representations are equal, which is not the case here.
Since on my platform this program returns 0
, it would appear that this rule is not applying here.
Why does this code behave like this ? (i.e. what am I missing? Which representations of _Bool
are trap representations and which ones aren't? How many representations can represent true
and false
? What role do padding bits play into this? etc. )
What can portable C programs assume about the representation of _Bool
?
c boolean
|
show 10 more comments
This program returns 0
in my machine:
#include <stdbool.h>
union U {
_Bool b;
char c;
};
int main(void) {
union U u;
u.c = 3;
_Bool b = u.b;
if (b == true) {
return 0;
} else {
return 1;
}
}
AFAICT, _Bool
is an integer type that can at least store 0
and 1
, and true
is the integral constant 1
. On my machine, _Bool
has a sizeof(_Bool) == 1
, and CHAR_BITS == 8
, which means that _Bool
has 256 representations.
I can't find much in the C standard about the trap representations of _Bool
, and I can't find whether creating a _Bool
with a representation different from 0
or 1
(on implementations that support more than two representations) is ok, and if it is ok, whether those representations denote true or false.
What I can find in the standard is what happens when a _Bool
is compared with an integer, the integer is converted to the 0
representation if it has value 0
, and to the 1
representation if it has a value different than zero, such that the snippet above ends up comparing two _Bool
s with different representations: _Bool[3] == _Bool[1]
.
I can't find much in the C standard about what the result of such a comparison is. Since _Bool
is an integer type, I'd expect the rules for integers to apply, such that the equality comparison only returns true if the representations are equal, which is not the case here.
Since on my platform this program returns 0
, it would appear that this rule is not applying here.
Why does this code behave like this ? (i.e. what am I missing? Which representations of _Bool
are trap representations and which ones aren't? How many representations can represent true
and false
? What role do padding bits play into this? etc. )
What can portable C programs assume about the representation of _Bool
?
c boolean
1
A union will cause both c and b to occupy the same area of memory, assigning 3 to char means the value of b will also be 3, true if NOT false, so it should equate to -1.
– SPlatten
Dec 30 '18 at 17:27
1
I could be wrong, but your program smells like undefined behavior to me
– Basile Starynkevitch
Dec 30 '18 at 17:32
1
In order to avoid integer overflows, any assignments to a _Bool that are not 0 (false) are stored as 1 (true). That being said, your code seems to be creating it's own confusion here, since the code saysif (true) return false
, and then you question why you are getting afalse
response. However, I believe that @interjay actually has the correct answer, and you have an undefined behavior due to your union causing the_Bool
to return the lowest bit. This seems to imply that the Union is only useful to identify if a value is odd or even.
– Claies
Dec 30 '18 at 17:39
1
No, equality comparisons return true if the values are equal. Representations are irrelevant for this purpose
– n.m.
Dec 30 '18 at 17:44
1
My answer to Is it safe to memset bool to 0? has a lot of potentially useful background information.
– Shafik Yaghmour
Dec 30 '18 at 18:03
|
show 10 more comments
This program returns 0
in my machine:
#include <stdbool.h>
union U {
_Bool b;
char c;
};
int main(void) {
union U u;
u.c = 3;
_Bool b = u.b;
if (b == true) {
return 0;
} else {
return 1;
}
}
AFAICT, _Bool
is an integer type that can at least store 0
and 1
, and true
is the integral constant 1
. On my machine, _Bool
has a sizeof(_Bool) == 1
, and CHAR_BITS == 8
, which means that _Bool
has 256 representations.
I can't find much in the C standard about the trap representations of _Bool
, and I can't find whether creating a _Bool
with a representation different from 0
or 1
(on implementations that support more than two representations) is ok, and if it is ok, whether those representations denote true or false.
What I can find in the standard is what happens when a _Bool
is compared with an integer, the integer is converted to the 0
representation if it has value 0
, and to the 1
representation if it has a value different than zero, such that the snippet above ends up comparing two _Bool
s with different representations: _Bool[3] == _Bool[1]
.
I can't find much in the C standard about what the result of such a comparison is. Since _Bool
is an integer type, I'd expect the rules for integers to apply, such that the equality comparison only returns true if the representations are equal, which is not the case here.
Since on my platform this program returns 0
, it would appear that this rule is not applying here.
Why does this code behave like this ? (i.e. what am I missing? Which representations of _Bool
are trap representations and which ones aren't? How many representations can represent true
and false
? What role do padding bits play into this? etc. )
What can portable C programs assume about the representation of _Bool
?
c boolean
This program returns 0
in my machine:
#include <stdbool.h>
union U {
_Bool b;
char c;
};
int main(void) {
union U u;
u.c = 3;
_Bool b = u.b;
if (b == true) {
return 0;
} else {
return 1;
}
}
AFAICT, _Bool
is an integer type that can at least store 0
and 1
, and true
is the integral constant 1
. On my machine, _Bool
has a sizeof(_Bool) == 1
, and CHAR_BITS == 8
, which means that _Bool
has 256 representations.
I can't find much in the C standard about the trap representations of _Bool
, and I can't find whether creating a _Bool
with a representation different from 0
or 1
(on implementations that support more than two representations) is ok, and if it is ok, whether those representations denote true or false.
What I can find in the standard is what happens when a _Bool
is compared with an integer, the integer is converted to the 0
representation if it has value 0
, and to the 1
representation if it has a value different than zero, such that the snippet above ends up comparing two _Bool
s with different representations: _Bool[3] == _Bool[1]
.
I can't find much in the C standard about what the result of such a comparison is. Since _Bool
is an integer type, I'd expect the rules for integers to apply, such that the equality comparison only returns true if the representations are equal, which is not the case here.
Since on my platform this program returns 0
, it would appear that this rule is not applying here.
Why does this code behave like this ? (i.e. what am I missing? Which representations of _Bool
are trap representations and which ones aren't? How many representations can represent true
and false
? What role do padding bits play into this? etc. )
What can portable C programs assume about the representation of _Bool
?
c boolean
c boolean
edited Dec 30 '18 at 17:25
gnzlbg
asked Dec 30 '18 at 17:19
gnzlbggnzlbg
2,65723077
2,65723077
1
A union will cause both c and b to occupy the same area of memory, assigning 3 to char means the value of b will also be 3, true if NOT false, so it should equate to -1.
– SPlatten
Dec 30 '18 at 17:27
1
I could be wrong, but your program smells like undefined behavior to me
– Basile Starynkevitch
Dec 30 '18 at 17:32
1
In order to avoid integer overflows, any assignments to a _Bool that are not 0 (false) are stored as 1 (true). That being said, your code seems to be creating it's own confusion here, since the code saysif (true) return false
, and then you question why you are getting afalse
response. However, I believe that @interjay actually has the correct answer, and you have an undefined behavior due to your union causing the_Bool
to return the lowest bit. This seems to imply that the Union is only useful to identify if a value is odd or even.
– Claies
Dec 30 '18 at 17:39
1
No, equality comparisons return true if the values are equal. Representations are irrelevant for this purpose
– n.m.
Dec 30 '18 at 17:44
1
My answer to Is it safe to memset bool to 0? has a lot of potentially useful background information.
– Shafik Yaghmour
Dec 30 '18 at 18:03
|
show 10 more comments
1
A union will cause both c and b to occupy the same area of memory, assigning 3 to char means the value of b will also be 3, true if NOT false, so it should equate to -1.
– SPlatten
Dec 30 '18 at 17:27
1
I could be wrong, but your program smells like undefined behavior to me
– Basile Starynkevitch
Dec 30 '18 at 17:32
1
In order to avoid integer overflows, any assignments to a _Bool that are not 0 (false) are stored as 1 (true). That being said, your code seems to be creating it's own confusion here, since the code saysif (true) return false
, and then you question why you are getting afalse
response. However, I believe that @interjay actually has the correct answer, and you have an undefined behavior due to your union causing the_Bool
to return the lowest bit. This seems to imply that the Union is only useful to identify if a value is odd or even.
– Claies
Dec 30 '18 at 17:39
1
No, equality comparisons return true if the values are equal. Representations are irrelevant for this purpose
– n.m.
Dec 30 '18 at 17:44
1
My answer to Is it safe to memset bool to 0? has a lot of potentially useful background information.
– Shafik Yaghmour
Dec 30 '18 at 18:03
1
1
A union will cause both c and b to occupy the same area of memory, assigning 3 to char means the value of b will also be 3, true if NOT false, so it should equate to -1.
– SPlatten
Dec 30 '18 at 17:27
A union will cause both c and b to occupy the same area of memory, assigning 3 to char means the value of b will also be 3, true if NOT false, so it should equate to -1.
– SPlatten
Dec 30 '18 at 17:27
1
1
I could be wrong, but your program smells like undefined behavior to me
– Basile Starynkevitch
Dec 30 '18 at 17:32
I could be wrong, but your program smells like undefined behavior to me
– Basile Starynkevitch
Dec 30 '18 at 17:32
1
1
In order to avoid integer overflows, any assignments to a _Bool that are not 0 (false) are stored as 1 (true). That being said, your code seems to be creating it's own confusion here, since the code says
if (true) return false
, and then you question why you are getting a false
response. However, I believe that @interjay actually has the correct answer, and you have an undefined behavior due to your union causing the _Bool
to return the lowest bit. This seems to imply that the Union is only useful to identify if a value is odd or even.– Claies
Dec 30 '18 at 17:39
In order to avoid integer overflows, any assignments to a _Bool that are not 0 (false) are stored as 1 (true). That being said, your code seems to be creating it's own confusion here, since the code says
if (true) return false
, and then you question why you are getting a false
response. However, I believe that @interjay actually has the correct answer, and you have an undefined behavior due to your union causing the _Bool
to return the lowest bit. This seems to imply that the Union is only useful to identify if a value is odd or even.– Claies
Dec 30 '18 at 17:39
1
1
No, equality comparisons return true if the values are equal. Representations are irrelevant for this purpose
– n.m.
Dec 30 '18 at 17:44
No, equality comparisons return true if the values are equal. Representations are irrelevant for this purpose
– n.m.
Dec 30 '18 at 17:44
1
1
My answer to Is it safe to memset bool to 0? has a lot of potentially useful background information.
– Shafik Yaghmour
Dec 30 '18 at 18:03
My answer to Is it safe to memset bool to 0? has a lot of potentially useful background information.
– Shafik Yaghmour
Dec 30 '18 at 18:03
|
show 10 more comments
1 Answer
1
active
oldest
votes
Footnote 122 in the C11 standard says:
While the number of bits in a _Bool object is at least CHAR_BIT, the width (number of sign and value bits) of a _Bool may be just 1 bit.
So on a compiler where _Bool
has only one value bit, only one of the bits of the char
will have effect when you read it from memory as a _Bool
. The other bits are padding bits which are ignored.
When I test your code with GCC, the _Bool
member gets a value of 1 when assigning an odd number to u.c
and 0 when assigning an even number, suggesting that it only looks at the lowest bit.
Note that the above is true only for type-punning. If you instead convert (implicit or explicit cast) a char
to a _Bool
, the value will be 1 if the char
was nonzero.
The question is whether_Bool
has only 1 bit of values in GCC. If it does, then all other representations are trap representations and the behavior of my example is undefined. If it does not, then_Bool
has many "true" and "false" representations, instead of just 2.
– gnzlbg
Dec 31 '18 at 9:16
2
@gnzlbg It does have only 1 bit of value in GCC (you can check this by trying to create a bitfield with a_Bool b : 2
field, which fails). But the other representations are not necessarily trap representations: The unused bits are padding bits, which can have any value. So the 0 and 1 values have many possible representations. It's possible for the compiler to define certain combinations of padding bits as trap representations, but it doesn't look like GCC does so.
– interjay
Dec 31 '18 at 10:43
Thanks, that makes sense.
– gnzlbg
Dec 31 '18 at 12:23
FYI I just filled gcc.gnu.org/bugzilla/show_bug.cgi?id=88662 because it does look like GCC assumes that _Bool representations are either 0x0 or 0x1.
– gnzlbg
Jan 2 at 13:54
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53979781%2fwhat-can-be-assumed-about-the-representation-of-true%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Footnote 122 in the C11 standard says:
While the number of bits in a _Bool object is at least CHAR_BIT, the width (number of sign and value bits) of a _Bool may be just 1 bit.
So on a compiler where _Bool
has only one value bit, only one of the bits of the char
will have effect when you read it from memory as a _Bool
. The other bits are padding bits which are ignored.
When I test your code with GCC, the _Bool
member gets a value of 1 when assigning an odd number to u.c
and 0 when assigning an even number, suggesting that it only looks at the lowest bit.
Note that the above is true only for type-punning. If you instead convert (implicit or explicit cast) a char
to a _Bool
, the value will be 1 if the char
was nonzero.
The question is whether_Bool
has only 1 bit of values in GCC. If it does, then all other representations are trap representations and the behavior of my example is undefined. If it does not, then_Bool
has many "true" and "false" representations, instead of just 2.
– gnzlbg
Dec 31 '18 at 9:16
2
@gnzlbg It does have only 1 bit of value in GCC (you can check this by trying to create a bitfield with a_Bool b : 2
field, which fails). But the other representations are not necessarily trap representations: The unused bits are padding bits, which can have any value. So the 0 and 1 values have many possible representations. It's possible for the compiler to define certain combinations of padding bits as trap representations, but it doesn't look like GCC does so.
– interjay
Dec 31 '18 at 10:43
Thanks, that makes sense.
– gnzlbg
Dec 31 '18 at 12:23
FYI I just filled gcc.gnu.org/bugzilla/show_bug.cgi?id=88662 because it does look like GCC assumes that _Bool representations are either 0x0 or 0x1.
– gnzlbg
Jan 2 at 13:54
add a comment |
Footnote 122 in the C11 standard says:
While the number of bits in a _Bool object is at least CHAR_BIT, the width (number of sign and value bits) of a _Bool may be just 1 bit.
So on a compiler where _Bool
has only one value bit, only one of the bits of the char
will have effect when you read it from memory as a _Bool
. The other bits are padding bits which are ignored.
When I test your code with GCC, the _Bool
member gets a value of 1 when assigning an odd number to u.c
and 0 when assigning an even number, suggesting that it only looks at the lowest bit.
Note that the above is true only for type-punning. If you instead convert (implicit or explicit cast) a char
to a _Bool
, the value will be 1 if the char
was nonzero.
The question is whether_Bool
has only 1 bit of values in GCC. If it does, then all other representations are trap representations and the behavior of my example is undefined. If it does not, then_Bool
has many "true" and "false" representations, instead of just 2.
– gnzlbg
Dec 31 '18 at 9:16
2
@gnzlbg It does have only 1 bit of value in GCC (you can check this by trying to create a bitfield with a_Bool b : 2
field, which fails). But the other representations are not necessarily trap representations: The unused bits are padding bits, which can have any value. So the 0 and 1 values have many possible representations. It's possible for the compiler to define certain combinations of padding bits as trap representations, but it doesn't look like GCC does so.
– interjay
Dec 31 '18 at 10:43
Thanks, that makes sense.
– gnzlbg
Dec 31 '18 at 12:23
FYI I just filled gcc.gnu.org/bugzilla/show_bug.cgi?id=88662 because it does look like GCC assumes that _Bool representations are either 0x0 or 0x1.
– gnzlbg
Jan 2 at 13:54
add a comment |
Footnote 122 in the C11 standard says:
While the number of bits in a _Bool object is at least CHAR_BIT, the width (number of sign and value bits) of a _Bool may be just 1 bit.
So on a compiler where _Bool
has only one value bit, only one of the bits of the char
will have effect when you read it from memory as a _Bool
. The other bits are padding bits which are ignored.
When I test your code with GCC, the _Bool
member gets a value of 1 when assigning an odd number to u.c
and 0 when assigning an even number, suggesting that it only looks at the lowest bit.
Note that the above is true only for type-punning. If you instead convert (implicit or explicit cast) a char
to a _Bool
, the value will be 1 if the char
was nonzero.
Footnote 122 in the C11 standard says:
While the number of bits in a _Bool object is at least CHAR_BIT, the width (number of sign and value bits) of a _Bool may be just 1 bit.
So on a compiler where _Bool
has only one value bit, only one of the bits of the char
will have effect when you read it from memory as a _Bool
. The other bits are padding bits which are ignored.
When I test your code with GCC, the _Bool
member gets a value of 1 when assigning an odd number to u.c
and 0 when assigning an even number, suggesting that it only looks at the lowest bit.
Note that the above is true only for type-punning. If you instead convert (implicit or explicit cast) a char
to a _Bool
, the value will be 1 if the char
was nonzero.
edited Dec 30 '18 at 17:49
answered Dec 30 '18 at 17:39
interjayinterjay
84.2k16202211
84.2k16202211
The question is whether_Bool
has only 1 bit of values in GCC. If it does, then all other representations are trap representations and the behavior of my example is undefined. If it does not, then_Bool
has many "true" and "false" representations, instead of just 2.
– gnzlbg
Dec 31 '18 at 9:16
2
@gnzlbg It does have only 1 bit of value in GCC (you can check this by trying to create a bitfield with a_Bool b : 2
field, which fails). But the other representations are not necessarily trap representations: The unused bits are padding bits, which can have any value. So the 0 and 1 values have many possible representations. It's possible for the compiler to define certain combinations of padding bits as trap representations, but it doesn't look like GCC does so.
– interjay
Dec 31 '18 at 10:43
Thanks, that makes sense.
– gnzlbg
Dec 31 '18 at 12:23
FYI I just filled gcc.gnu.org/bugzilla/show_bug.cgi?id=88662 because it does look like GCC assumes that _Bool representations are either 0x0 or 0x1.
– gnzlbg
Jan 2 at 13:54
add a comment |
The question is whether_Bool
has only 1 bit of values in GCC. If it does, then all other representations are trap representations and the behavior of my example is undefined. If it does not, then_Bool
has many "true" and "false" representations, instead of just 2.
– gnzlbg
Dec 31 '18 at 9:16
2
@gnzlbg It does have only 1 bit of value in GCC (you can check this by trying to create a bitfield with a_Bool b : 2
field, which fails). But the other representations are not necessarily trap representations: The unused bits are padding bits, which can have any value. So the 0 and 1 values have many possible representations. It's possible for the compiler to define certain combinations of padding bits as trap representations, but it doesn't look like GCC does so.
– interjay
Dec 31 '18 at 10:43
Thanks, that makes sense.
– gnzlbg
Dec 31 '18 at 12:23
FYI I just filled gcc.gnu.org/bugzilla/show_bug.cgi?id=88662 because it does look like GCC assumes that _Bool representations are either 0x0 or 0x1.
– gnzlbg
Jan 2 at 13:54
The question is whether
_Bool
has only 1 bit of values in GCC. If it does, then all other representations are trap representations and the behavior of my example is undefined. If it does not, then _Bool
has many "true" and "false" representations, instead of just 2.– gnzlbg
Dec 31 '18 at 9:16
The question is whether
_Bool
has only 1 bit of values in GCC. If it does, then all other representations are trap representations and the behavior of my example is undefined. If it does not, then _Bool
has many "true" and "false" representations, instead of just 2.– gnzlbg
Dec 31 '18 at 9:16
2
2
@gnzlbg It does have only 1 bit of value in GCC (you can check this by trying to create a bitfield with a
_Bool b : 2
field, which fails). But the other representations are not necessarily trap representations: The unused bits are padding bits, which can have any value. So the 0 and 1 values have many possible representations. It's possible for the compiler to define certain combinations of padding bits as trap representations, but it doesn't look like GCC does so.– interjay
Dec 31 '18 at 10:43
@gnzlbg It does have only 1 bit of value in GCC (you can check this by trying to create a bitfield with a
_Bool b : 2
field, which fails). But the other representations are not necessarily trap representations: The unused bits are padding bits, which can have any value. So the 0 and 1 values have many possible representations. It's possible for the compiler to define certain combinations of padding bits as trap representations, but it doesn't look like GCC does so.– interjay
Dec 31 '18 at 10:43
Thanks, that makes sense.
– gnzlbg
Dec 31 '18 at 12:23
Thanks, that makes sense.
– gnzlbg
Dec 31 '18 at 12:23
FYI I just filled gcc.gnu.org/bugzilla/show_bug.cgi?id=88662 because it does look like GCC assumes that _Bool representations are either 0x0 or 0x1.
– gnzlbg
Jan 2 at 13:54
FYI I just filled gcc.gnu.org/bugzilla/show_bug.cgi?id=88662 because it does look like GCC assumes that _Bool representations are either 0x0 or 0x1.
– gnzlbg
Jan 2 at 13:54
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53979781%2fwhat-can-be-assumed-about-the-representation-of-true%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
A union will cause both c and b to occupy the same area of memory, assigning 3 to char means the value of b will also be 3, true if NOT false, so it should equate to -1.
– SPlatten
Dec 30 '18 at 17:27
1
I could be wrong, but your program smells like undefined behavior to me
– Basile Starynkevitch
Dec 30 '18 at 17:32
1
In order to avoid integer overflows, any assignments to a _Bool that are not 0 (false) are stored as 1 (true). That being said, your code seems to be creating it's own confusion here, since the code says
if (true) return false
, and then you question why you are getting afalse
response. However, I believe that @interjay actually has the correct answer, and you have an undefined behavior due to your union causing the_Bool
to return the lowest bit. This seems to imply that the Union is only useful to identify if a value is odd or even.– Claies
Dec 30 '18 at 17:39
1
No, equality comparisons return true if the values are equal. Representations are irrelevant for this purpose
– n.m.
Dec 30 '18 at 17:44
1
My answer to Is it safe to memset bool to 0? has a lot of potentially useful background information.
– Shafik Yaghmour
Dec 30 '18 at 18:03