Why I cannot store into a variable the return result of an logic expression in C code?
I have a strange behavior of a small piece of C code.
I want to store the result of a boolean expression in a variable but it seems not to work.
Following the code:
#define rtCP_Constant_Value_fklq (uint8_t) 1 //Simulink const
#define rtCP_Constant_Value (uint8_t) 0 //Simulink const
uint16_t rtb_tobit;
volatile unsigned char rtb_y;
uint8_t asr_ena_=14;
rtb_tobit = (1 << rtCP_Constant_Value_fklq);
uint8_t temp = ((uint8_t)rtb_tobit) & asr_ena_;
rtb_y = (temp !=(rtCP_Constant_Value));
I have tested this snippet of code with two compilers, Renesas SH 9_4_1 and gcc-arm non-eabi in an Nucleo eval board.
In both of them the variable rtb_y
is always zero.
The debugger shows that the expression (temp !=(rtCP_Constant_Value))
is true but I cannot understand why, the variable rtb_y
is always equal to zero.
Could someone explain me why? Is this strange behaviour because of C standards, which I used?
c gcc arm
|
show 6 more comments
I have a strange behavior of a small piece of C code.
I want to store the result of a boolean expression in a variable but it seems not to work.
Following the code:
#define rtCP_Constant_Value_fklq (uint8_t) 1 //Simulink const
#define rtCP_Constant_Value (uint8_t) 0 //Simulink const
uint16_t rtb_tobit;
volatile unsigned char rtb_y;
uint8_t asr_ena_=14;
rtb_tobit = (1 << rtCP_Constant_Value_fklq);
uint8_t temp = ((uint8_t)rtb_tobit) & asr_ena_;
rtb_y = (temp !=(rtCP_Constant_Value));
I have tested this snippet of code with two compilers, Renesas SH 9_4_1 and gcc-arm non-eabi in an Nucleo eval board.
In both of them the variable rtb_y
is always zero.
The debugger shows that the expression (temp !=(rtCP_Constant_Value))
is true but I cannot understand why, the variable rtb_y
is always equal to zero.
Could someone explain me why? Is this strange behaviour because of C standards, which I used?
c gcc arm
If you enable more warnings when building (add at least the-Wall
and-Wextra
flags, possibly-pedantic
), the compiler doesn't complain?
– Some programmer dude
Nov 23 '18 at 13:52
I don't know about these boards but avolatile
variable is allowed to change its value in an implementation-defined way: it's possible that on this platform, setting it seems to do nothing because it's allowed to change itself back to zero immediately (perhaps it detects it is set and does something else as an interrupt?).
– Leushenko
Nov 23 '18 at 13:55
@Someprogrammerdude I will try your suggestion.
– xmaze
Nov 23 '18 at 13:58
@Leushenko I dont use interrupts and I used the volatile only to avoid the optimization.
– xmaze
Nov 23 '18 at 14:01
3
Remove;
from the#define
s. Example#define rtCP_Constant_Value_fklq ((uint8_t) 1)
. xmaze, Does that work for you?
– chux
Nov 23 '18 at 15:45
|
show 6 more comments
I have a strange behavior of a small piece of C code.
I want to store the result of a boolean expression in a variable but it seems not to work.
Following the code:
#define rtCP_Constant_Value_fklq (uint8_t) 1 //Simulink const
#define rtCP_Constant_Value (uint8_t) 0 //Simulink const
uint16_t rtb_tobit;
volatile unsigned char rtb_y;
uint8_t asr_ena_=14;
rtb_tobit = (1 << rtCP_Constant_Value_fklq);
uint8_t temp = ((uint8_t)rtb_tobit) & asr_ena_;
rtb_y = (temp !=(rtCP_Constant_Value));
I have tested this snippet of code with two compilers, Renesas SH 9_4_1 and gcc-arm non-eabi in an Nucleo eval board.
In both of them the variable rtb_y
is always zero.
The debugger shows that the expression (temp !=(rtCP_Constant_Value))
is true but I cannot understand why, the variable rtb_y
is always equal to zero.
Could someone explain me why? Is this strange behaviour because of C standards, which I used?
c gcc arm
I have a strange behavior of a small piece of C code.
I want to store the result of a boolean expression in a variable but it seems not to work.
Following the code:
#define rtCP_Constant_Value_fklq (uint8_t) 1 //Simulink const
#define rtCP_Constant_Value (uint8_t) 0 //Simulink const
uint16_t rtb_tobit;
volatile unsigned char rtb_y;
uint8_t asr_ena_=14;
rtb_tobit = (1 << rtCP_Constant_Value_fklq);
uint8_t temp = ((uint8_t)rtb_tobit) & asr_ena_;
rtb_y = (temp !=(rtCP_Constant_Value));
I have tested this snippet of code with two compilers, Renesas SH 9_4_1 and gcc-arm non-eabi in an Nucleo eval board.
In both of them the variable rtb_y
is always zero.
The debugger shows that the expression (temp !=(rtCP_Constant_Value))
is true but I cannot understand why, the variable rtb_y
is always equal to zero.
Could someone explain me why? Is this strange behaviour because of C standards, which I used?
c gcc arm
c gcc arm
edited Nov 26 '18 at 9:26
xmaze
asked Nov 23 '18 at 13:49
xmazexmaze
116
116
If you enable more warnings when building (add at least the-Wall
and-Wextra
flags, possibly-pedantic
), the compiler doesn't complain?
– Some programmer dude
Nov 23 '18 at 13:52
I don't know about these boards but avolatile
variable is allowed to change its value in an implementation-defined way: it's possible that on this platform, setting it seems to do nothing because it's allowed to change itself back to zero immediately (perhaps it detects it is set and does something else as an interrupt?).
– Leushenko
Nov 23 '18 at 13:55
@Someprogrammerdude I will try your suggestion.
– xmaze
Nov 23 '18 at 13:58
@Leushenko I dont use interrupts and I used the volatile only to avoid the optimization.
– xmaze
Nov 23 '18 at 14:01
3
Remove;
from the#define
s. Example#define rtCP_Constant_Value_fklq ((uint8_t) 1)
. xmaze, Does that work for you?
– chux
Nov 23 '18 at 15:45
|
show 6 more comments
If you enable more warnings when building (add at least the-Wall
and-Wextra
flags, possibly-pedantic
), the compiler doesn't complain?
– Some programmer dude
Nov 23 '18 at 13:52
I don't know about these boards but avolatile
variable is allowed to change its value in an implementation-defined way: it's possible that on this platform, setting it seems to do nothing because it's allowed to change itself back to zero immediately (perhaps it detects it is set and does something else as an interrupt?).
– Leushenko
Nov 23 '18 at 13:55
@Someprogrammerdude I will try your suggestion.
– xmaze
Nov 23 '18 at 13:58
@Leushenko I dont use interrupts and I used the volatile only to avoid the optimization.
– xmaze
Nov 23 '18 at 14:01
3
Remove;
from the#define
s. Example#define rtCP_Constant_Value_fklq ((uint8_t) 1)
. xmaze, Does that work for you?
– chux
Nov 23 '18 at 15:45
If you enable more warnings when building (add at least the
-Wall
and -Wextra
flags, possibly -pedantic
), the compiler doesn't complain?– Some programmer dude
Nov 23 '18 at 13:52
If you enable more warnings when building (add at least the
-Wall
and -Wextra
flags, possibly -pedantic
), the compiler doesn't complain?– Some programmer dude
Nov 23 '18 at 13:52
I don't know about these boards but a
volatile
variable is allowed to change its value in an implementation-defined way: it's possible that on this platform, setting it seems to do nothing because it's allowed to change itself back to zero immediately (perhaps it detects it is set and does something else as an interrupt?).– Leushenko
Nov 23 '18 at 13:55
I don't know about these boards but a
volatile
variable is allowed to change its value in an implementation-defined way: it's possible that on this platform, setting it seems to do nothing because it's allowed to change itself back to zero immediately (perhaps it detects it is set and does something else as an interrupt?).– Leushenko
Nov 23 '18 at 13:55
@Someprogrammerdude I will try your suggestion.
– xmaze
Nov 23 '18 at 13:58
@Someprogrammerdude I will try your suggestion.
– xmaze
Nov 23 '18 at 13:58
@Leushenko I dont use interrupts and I used the volatile only to avoid the optimization.
– xmaze
Nov 23 '18 at 14:01
@Leushenko I dont use interrupts and I used the volatile only to avoid the optimization.
– xmaze
Nov 23 '18 at 14:01
3
3
Remove
;
from the #define
s. Example #define rtCP_Constant_Value_fklq ((uint8_t) 1)
. xmaze, Does that work for you?– chux
Nov 23 '18 at 15:45
Remove
;
from the #define
s. Example #define rtCP_Constant_Value_fklq ((uint8_t) 1)
. xmaze, Does that work for you?– chux
Nov 23 '18 at 15:45
|
show 6 more comments
1 Answer
1
active
oldest
votes
It is a really bad idea to use macros in the way you are using them. You need to be very careful, generally, to use parenthesis in the right places. Also you should not include the ; in the macro. For example, this is better:
#define rtCP_Constant_Value_fklq ((uint8_t) 1) //Simulink const
However, it is not really possible to offer any more help for your question than this, because your example will not compile, due to the inclusion of the ;. If you update the question with code that compiles, it may be possible to help further.
Hi, sorry for the syntax error in the message, I can compile this code and debugg. I found the error las nicht. It was an optimization issue of the compiler.
– xmaze
Nov 26 '18 at 9:28
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%2f53447929%2fwhy-i-cannot-store-into-a-variable-the-return-result-of-an-logic-expression-in-c%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
It is a really bad idea to use macros in the way you are using them. You need to be very careful, generally, to use parenthesis in the right places. Also you should not include the ; in the macro. For example, this is better:
#define rtCP_Constant_Value_fklq ((uint8_t) 1) //Simulink const
However, it is not really possible to offer any more help for your question than this, because your example will not compile, due to the inclusion of the ;. If you update the question with code that compiles, it may be possible to help further.
Hi, sorry for the syntax error in the message, I can compile this code and debugg. I found the error las nicht. It was an optimization issue of the compiler.
– xmaze
Nov 26 '18 at 9:28
add a comment |
It is a really bad idea to use macros in the way you are using them. You need to be very careful, generally, to use parenthesis in the right places. Also you should not include the ; in the macro. For example, this is better:
#define rtCP_Constant_Value_fklq ((uint8_t) 1) //Simulink const
However, it is not really possible to offer any more help for your question than this, because your example will not compile, due to the inclusion of the ;. If you update the question with code that compiles, it may be possible to help further.
Hi, sorry for the syntax error in the message, I can compile this code and debugg. I found the error las nicht. It was an optimization issue of the compiler.
– xmaze
Nov 26 '18 at 9:28
add a comment |
It is a really bad idea to use macros in the way you are using them. You need to be very careful, generally, to use parenthesis in the right places. Also you should not include the ; in the macro. For example, this is better:
#define rtCP_Constant_Value_fklq ((uint8_t) 1) //Simulink const
However, it is not really possible to offer any more help for your question than this, because your example will not compile, due to the inclusion of the ;. If you update the question with code that compiles, it may be possible to help further.
It is a really bad idea to use macros in the way you are using them. You need to be very careful, generally, to use parenthesis in the right places. Also you should not include the ; in the macro. For example, this is better:
#define rtCP_Constant_Value_fklq ((uint8_t) 1) //Simulink const
However, it is not really possible to offer any more help for your question than this, because your example will not compile, due to the inclusion of the ;. If you update the question with code that compiles, it may be possible to help further.
answered Nov 23 '18 at 16:28
Dino DiniDino Dini
672
672
Hi, sorry for the syntax error in the message, I can compile this code and debugg. I found the error las nicht. It was an optimization issue of the compiler.
– xmaze
Nov 26 '18 at 9:28
add a comment |
Hi, sorry for the syntax error in the message, I can compile this code and debugg. I found the error las nicht. It was an optimization issue of the compiler.
– xmaze
Nov 26 '18 at 9:28
Hi, sorry for the syntax error in the message, I can compile this code and debugg. I found the error las nicht. It was an optimization issue of the compiler.
– xmaze
Nov 26 '18 at 9:28
Hi, sorry for the syntax error in the message, I can compile this code and debugg. I found the error las nicht. It was an optimization issue of the compiler.
– xmaze
Nov 26 '18 at 9:28
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%2f53447929%2fwhy-i-cannot-store-into-a-variable-the-return-result-of-an-logic-expression-in-c%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
If you enable more warnings when building (add at least the
-Wall
and-Wextra
flags, possibly-pedantic
), the compiler doesn't complain?– Some programmer dude
Nov 23 '18 at 13:52
I don't know about these boards but a
volatile
variable is allowed to change its value in an implementation-defined way: it's possible that on this platform, setting it seems to do nothing because it's allowed to change itself back to zero immediately (perhaps it detects it is set and does something else as an interrupt?).– Leushenko
Nov 23 '18 at 13:55
@Someprogrammerdude I will try your suggestion.
– xmaze
Nov 23 '18 at 13:58
@Leushenko I dont use interrupts and I used the volatile only to avoid the optimization.
– xmaze
Nov 23 '18 at 14:01
3
Remove
;
from the#define
s. Example#define rtCP_Constant_Value_fklq ((uint8_t) 1)
. xmaze, Does that work for you?– chux
Nov 23 '18 at 15:45