What is the output of this C code?#include <stdio.h>int main(){enum {ORANGE = 5, MANGO, BANANA = 4, PEACH};enum {ORANGE = 5, MANGO, BANANA = 4, PEACH};printf("PEACH = %d\n", PEACH); printf("PEACH = %d\n", PEACH); }PEACH = 3PEACH = 4PEACH = 5PEACH = 6
Question
What is the output of this C code?#include <stdio.h>int main(){enum {ORANGE = 5, MANGO, BANANA = 4, PEACH};enum {ORANGE = 5, MANGO, BANANA = 4, PEACH};printf("PEACH = %d\n", PEACH); printf("PEACH = %d\n", PEACH); }PEACH = 3PEACH = 4PEACH = 5PEACH = 6
Solution
The output of this C code will be:
PEACH = 5 PEACH = 5
Explanation:
In C, an enumeration (enum) is a user-defined data type that consists of integral constants. By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1.
In your code, you have two identical enums. In each enum, ORANGE is assigned the value 5. MANGO, which comes after ORANGE, will therefore have the value 6. BANANA is then explicitly assigned the value 4. PEACH, which comes after BANANA, will therefore have the value 5 (one more than BANANA's value).
So, when you print the value of PEACH, it will output 5.
Similar Questions
What is the output of the C Program?
What is the output for the following code?
What will be the output of the following C code?#include <stdio.h>int main() { int i; for (i = 0; i < 5; i++); printf("%d\n", i); return 0;}
What would be the output of the following program? #include <stdio.h> int main( ) { int j=1; while (j <= 255) { printf ( "%c %d\n ", j, j ); j++; } return 0; }
What is the output of this C code?#include <stdio.h>int main(){int a = 2;x = x << 1;printf("%d\n", a);}41Depends on the compilerDepends on the endianness of the machine
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.