diesen Code vor:?: Unerwartetes Verhalten - obwohl die Bedingung erfüllt ist, die? Teil nicht ausgeführt
// Example program
#include <stdio.h>
typedef enum{
enum_1,
enum_2
}my_enum;
/*
* this is not how the function works but when I debug
* it will always returns enum_1 (verified when using the debugger)
* so I hope this is enough
*/
int fun_1(int arg_1){return enum_1;}
const int const_1 = 10;
const int const_2 = 20;
int main()
{
int arg_1 = 0;
int var_1 = fun_1(arg_1);
int var_2 = (var_1 == (enum_1
|| enum_2))
? const_1*10 // I expect this result
: const_2*10; // instead I get this. why?
printf("%d\n" , var_2);
if(var_1==enum_1)
var_2 = const_1*10; // here it works fine
printf("%d\n" , var_2);
return 0;
}
ich diesen Code bin Debuggen und var_1 wird erwartet, dass der Wert von enum_1 zu bekommen. Es tut, aber irgendwie gibt der Operator "?:" Mir das erwartete Ergebnis nicht - es weist const_1*10
zu var_2
aber const_2*10
nicht zu. Wenn ich im debug mitfahre und zur if-Anweisung gehe, bekomme ich das erwartete Ergebnis. Warum das?
Ich bin mit Windriver Werkbank auf Windows 8.1
EDIT
änderte ich das Beispiel auf eine ausführbare Version, und wie durch tkausl erwähnt, das Problem (var_1 == (enum_1 || enum_2))
wurde anstelle von (var_1 == enum_1 || var_1 == enum_2))
Sie kümmern sich um ein [___MCVE___] (http://stackoverflow.com/help/mcve)? –
'(enum_1 || enum_2)' erzeugt ein bool. –
Ihr Vergleich ist falsch. check @tkausls Antwort –