C Programming - Arrays - Discussion
Discussion Forum : Arrays - Find Output of Program (Q.No. 1)
                   
                                       
                                1.
What will be the output of the program ?
 
                                    #include<stdio.h>
int main()
{
    int a[5] = {5, 1, 15, 20, 25};
    int i, j, m;
    i = ++a[1];
    j = a[1]++;
    m = a[i++];
    printf("%d, %d, %d", i, j, m);
    return 0;
}
Answer: Option
                                                    Explanation:
                                                Step 1: int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared as an integer array with a size of 5 and it is initialized to
a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25 .
Step 2: int i, j, m; The variable i,j,m are declared as an integer type.
Step 3: i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2
Step 4: j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3.
Step 5: m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++ so i=3)
Step 6: printf("%d, %d, %d", i, j, m); It prints the value of the variables i, j, m
Hence the output of the program is 3, 2, 15
Discussion:
40 comments Page 1 of 4.
                
                        Sanika Ashok Kamble said: 
                         
                        1 month ago
                
                I think it's 2, 3, 15.
                
                        Anandesh said: 
                         
                        10 months ago
                
                2,3,20 is the answer.
                
                        Tharun parsa said: 
                         
                        2 years ago
                
                The answer is 3,2,15.
Because
i=++a[1]=++1 (since a[1]=1) then i=2
i=2,and a[1]=2 (because pre-increment )
j=a[1]++=2++ (since a[1]=2 here)
j=2 and a[1]=3
m=a[i++] that is i=2 and only i value will be taken in the square bracket then it will be incremented
m=a[2++]
which means m=a[2 ]and then i value increases by 1 then i =3
So, m=a[2]=15
Hence the output is 3,2,15.
                Because
i=++a[1]=++1 (since a[1]=1) then i=2
i=2,and a[1]=2 (because pre-increment )
j=a[1]++=2++ (since a[1]=2 here)
j=2 and a[1]=3
m=a[i++] that is i=2 and only i value will be taken in the square bracket then it will be incremented
m=a[2++]
which means m=a[2 ]and then i value increases by 1 then i =3
So, m=a[2]=15
Hence the output is 3,2,15.
                     (6)
                
            
                        Shantaras said: 
                         
                        3 years ago
                
                Sir,
M=a (2) taken why not a (3). (because I is incremented).
                M=a (2) taken why not a (3). (because I is incremented).
                     (3)
                
            
                        Jaydeep B said: 
                         
                        3 years ago
                
                I think the correct answer would be 2, 3, 15.
                
                     (17)
                
            
                        Kaviya.p said: 
                         
                        4 years ago
                
                @Rosh.
The value of j is not incremented the a[1] value is incremented see its because of post incremented.
                The value of j is not incremented the a[1] value is incremented see its because of post incremented.
                     (1)
                
            
                        Rosh said: 
                         
                        5 years ago
                
                @Mohanapriya @Rupinderjit Singh
It was quit understandable explanation but still, I have a doubt j and m both are using post increment then why only m is incremented (when we reach to print statement ). Why not j?
Please explain about it.
                It was quit understandable explanation but still, I have a doubt j and m both are using post increment then why only m is incremented (when we reach to print statement ). Why not j?
Please explain about it.
                        Mainak said: 
                         
                        7 years ago
                
                I am not understanding step 5. Please explain it clearly.
                
                        R.Amirthavarshini said: 
                         
                        7 years ago
                
                I think 2, 3, 20 is the answer.
                
                     (4)
                
            
                        Dev said: 
                         
                        7 years ago
                
                Thanks @Gayathri.
                Post your comments here:
 
            
        Quick links
                            Quantitative Aptitude
                                    
                                    Verbal (English)
                                    
                                    Reasoning
                                    
                                Programming
                                    
                                    Interview
                                    
                                     Placement Papers