Question 1Medium
Functions can return enumeration constants in c?
- True
- False
- Depends on the compiler
- Depends on the standard
Answer: A. True
Explanation
If we have something like the following in a header file:
enum Foo
{
BAR,
BAZ
};
how do we declare a function that returns an enum of type foo? Can I just do something like the following:
Foo testFunc()
{
return Foo.BAR;
}Step-by-step solution
- In C, an enumeration constant is an integer constant.
- A function can return any integer type, so it can return an enum constant.
- This is allowed by the standard in all compilers, so the answer is True.
Why the other options are wrong
- B. Enum values are integers, and functions can return integers.
- C. The rule is set by the standard, so it does not depend on the compiler.
- D. The standard allows it, so the answer does not depend on the standard.
Tip: Enum constants are just int values in C.