Why I cannot store into a variable the return result of an logic expression in C code?












1















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?










share|improve this question

























  • 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 #defines. Example #define rtCP_Constant_Value_fklq ((uint8_t) 1). xmaze, Does that work for you?

    – chux
    Nov 23 '18 at 15:45


















1















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?










share|improve this question

























  • 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 #defines. Example #define rtCP_Constant_Value_fklq ((uint8_t) 1). xmaze, Does that work for you?

    – chux
    Nov 23 '18 at 15:45
















1












1








1








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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 #defines. 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











  • 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 #defines. 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 #defines. 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 #defines. Example #define rtCP_Constant_Value_fklq ((uint8_t) 1). xmaze, Does that work for you?

– chux
Nov 23 '18 at 15:45














1 Answer
1






active

oldest

votes


















0














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.






share|improve this answer
























  • 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











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
});


}
});














draft saved

draft discarded


















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









0














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.






share|improve this answer
























  • 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
















0














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.






share|improve this answer
























  • 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














0












0








0







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

To store a contact into the json file from server.js file using a class in NodeJS

Redirect URL with Chrome Remote Debugging Android Devices

Dieringhausen