I've compiled the following code:
typedef enum E_STATUS_A
{
STATUS_A_FIRST = 0,
STATUS_A_SECOND
}E_STATUS_A;
typedef enum E_STATUS_B
{
STATUS_B_FIRST = 0,
STATUS_B_SECOND = 15
}E_STATUS_B;
E_STATUS_B bar()
{
E_STATUS_B val = STATUS_B_SECOND;
return val;
}
E_STATUS_A foo()
{
int val = 0;
val = bar();
return val;
}
int main()
{
foo();
return 0;
}
After return from function bar()
then val
value is 15
and not STATUS_B_SECOND
.
I got several questions:
I assume I receive back from
bar
theenum
STATUS_B_SECOND
andval
gets the value15
because of an implicit cast fromE_STATUS_B
toint
, correct? If so why is it happening, and why my visual studio (2019) compiler is not notifying me on that?If I change the
foo
code to:
E_STATUS_A foo()
{
E_STATUS_A val = 0;
val = bar();
return val;
}
Then still, I get back from bar
an enum
of STATUS_B_SECOND
however val
itself is 15
, is it because of an implicit cast from STATUS_B_SECOND
to int
? If so, I guess it doesn't matter what's the definition of val
in foo
, as if there won't be exact fit from the return bar
value type to the val
type, it will always be casted as int
(or some other primitive type that is closed to the returned value from bar
)?
- If I keep in the function
foo
val
type asE_STATUS_A
and I change theE_STATUS_B
enum
to
typedef enum E_STATUS_B
{
STATUS_B_FIRST = 0,
STATUS_B_SECOND
}E_STATUS_B;
And run the same code, I get STATUS_B_SECOND
returned from foo
however val
itself is now of type E_STATUS_A
and its value is STATUS_A_SECOND
. This kind of confuses me, as from my previous code, when STATUS_B_SECOND = 15
I got just 15
as a returned value, and now I get implicit cast for E_STATUS_A
type. Is that correct? If so, how is the implicit cast chooses which type to cast, and why didn't just cast to int
, and moreover, why visual studio didn't raise me a warning?...