29 votes 29 votes The value of $j$ at the end of the execution of the following C program: int incr (int i) { static int count = 0; count = count + i; return (count); } main () { int i, j; for (i = 0; i <= 4; i++) j = incr (i); } is: $10$ $4$ $6$ $7$ Programming in C gatecse-2000 programming programming-in-c easy + – Kathleen asked Sep 14, 2014 • edited Jun 15, 2018 by Milicevic3306 Kathleen 15.1k views answer comment Share Follow See all 0 reply Please log in or register to add a comment.
Best answer 36 votes 36 votes Answer: (A) At $i=0, j=0$ At $i=1, j=1$ At $i=2, j=3$ At $i=3, j=6$ At $i=4, j=10$ Rajarshi Sarkar answered Jun 25, 2015 • edited May 31, 2021 by Lakshman Bhaiya Rajarshi Sarkar comment Share Follow See all 5 Comments See all 5 5 Comments reply Chhotu commented Oct 24, 2017 reply Follow Share $\frac{(n)*(n+1)}{2}$ Put n = 4. 2 votes 2 votes Pronomita Dey 1 commented Jan 10, 2018 reply Follow Share This is because count being a static variable is not reinitialized at every call post the first call of incr(i) 9 votes 9 votes subbus commented Apr 23, 2020 reply Follow Share A static local variable is a place to hide data form other functions but retain it for future calls of the same function. 2 votes 2 votes Kiyoshi commented Jun 22, 2022 i edited by Kiyoshi Jun 22, 2022 reply Follow Share In main function inside the for loop incr(i) function call is done for each value of i from 0 to 4 and in the incr(function definition) just adding all of them (because of count variable is static and it retains previously result).So, if i goes from 0 to 4(inside the main) then incr(function definition) last value returned will be –0+1+2+3+4 = $\frac{4*5}{2}$ = 10Similarly, for i goes from 0 to n incr last value returned will be –0+1+2+….n = $\frac{n*(n+1)}{2}$ 0 votes 0 votes 24aaaa23 commented Jul 4, 2023 reply Follow Share ans will be 10 0+1+2+3+4 0 votes 0 votes Please log in or register to add a comment.
0 votes 0 votes They crux of the problem is, static int count = 0; will get executed only once. Now, for i=0, j =0 i=1, j=1 i=2, j=3 i=3, j=6, i=4, j=10. j=10, is the required answer to the given problem himanshud2611 answered Jun 24, 2023 himanshud2611 comment Share Follow See all 0 reply Please log in or register to add a comment.