GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

C Calling Function

#include<stdio.h>
void convention(int,int,int);
int main(){
    int a=5;
    convention(a,++a,a++);
    return 0;
}
void  convention(int p,int q,int r){
    printf("%d %d %d",p,q,r);
}

 

Output of ABove Program : 7 7 5

I cannot understood the flow of above program how it works please any one explain how the result is 7 7 5

 

asked Feb 4, 2017 by Muniyasamy V

2 Answers

hi,

in this program,

p , q,r   are  taking values  a,++a,a++ respectivally .

a= 5 

++a means first increase the value of a ,then use

a++ means first use value of a then increase

thus r=5 then increase value by 1. a=6 now

q=7  b/c value is increase first by 1 means a=6+1 =7 then use/assign.

ais updated now 7 in memory so p=7.

 

answered Feb 7, 2017 by manisha saini 2
convention(a,++a,a++);

"Basically, compiler reads from right to left !"

First a++ comes in buffer but "a++" means first it takes the value of a for 'r' and increments !

So, at first a is 5. It, prints the value of 'r' as 5 then increments a to "6".

Now, for "++a" this will increments a first and then it takes the whole value for 'q'.

So, a becomes 7 after increments and then it takes the value of 'q' as '7'

Finally, a value will be taken directly for 'p'

So, it prints p value as '7' (Since there is no operation on 'a').

 

answered Feb 7, 2017 by Chippagiri Saicharan
...