Output of C Program | Set 22

Predict the output of following C programs.

Question 1

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
  
int main()
{
    enum channel {star, sony, zee};
    enum symbol {hash, star};
  
    int i = 0;
    for(i = star; i <= zee; i++)
    {
        printf("%d ", i);
    }
  
    return 0;
}

chevron_right


Output:

compiler error: redeclaration of enumerator 'star'

In the above program, enumartion constant ‘star’ appears two times in main() which causes the error. An enumaration constant must be unique within the scope in which it is defined. The following program works fine and prints 0 1 2 as the enumaration constants automatically get the values starting from 0.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
  
int main()
{
    enum channel {star, sony, zee};
  
    int i = 0;
    for(i = star; i <= zee; i++)
    {
        printf("%d ", i);
    }
  
    return 0;
}

chevron_right


Output:

0 1 2



Question 2

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
  
int main()
{
    int i, j;
    int p = 0, q = 2;
  
    for(i = 0, j = 0; i < p, j < q; i++, j++)
    {
      printf("GeeksforGeeks\n");
    }
  
    return 0;
}

chevron_right


Output:

GeeksforGeeks
GeeksforGeeks

Following is the main expression to consider in the above program.

filter_none

edit
close

play_arrow

link
brightness_4
code

i < p, j < q

chevron_right


When two expressions are separated by comma operator, the first expression (i < p) is executed first. Result of the first expression is ignored. Then the second expression (j < q) is executed and the result of this second expression is the final result of the complete expression (i < p, j < q). The value of expression 'j < q' is true for two iterations, so we get "GeeksforGeeks" two times on the screen. See this for more details.

Please write comments if you find any of the answers/explanations incorrect, or you want to share more information about the topics discussed above



My Personal Notes arrow_drop_up


Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.