The value of j at the end of the execution of the following C programint 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);}41067
Question
The value of j at the end of the execution of the following C programint 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);}41067
Solution
The function incr is a C function that takes an integer i as an argument and increments a static integer count by i. The static keyword means that count retains its value between function calls.
The main function initializes two integers i and j, and then enters a for loop that iterates from i = 0 to i = 4. On each iteration, it calls incr(i) and assigns the result to j.
Let's break down the execution of the program:
- On the first iteration,
i = 0, soincr(0)is called. This adds0tocount(which is initially0), socountremains0andjis set to0. - On the second iteration,
i = 1, soincr(1)is called. This adds1tocount(which is currently0), socountbecomes1andjis set to1. - On the third iteration,
i = 2, soincr(2)is called. This adds2tocount(which is currently1), socountbecomes3andjis set to3. - On the fourth iteration,
i = 3, soincr(3)is called. This adds3tocount(which is currently3), socountbecomes6andjis set to6. - On the fifth and final iteration,
i = 4, soincr(4)is called. This adds4tocount(which is currently6), socountbecomes10andjis set to10.
So, at the end of the execution of the program, the value of j is 10.
Similar Questions
What is output of below program?int main(){int i,j,count;count=0;for(i=0; i<5; i++);{for(j=0;j<5;j++);{ count++;}}printf("%d",count);return 0;} (A) 55(B) 54(C) 0(D) 1
What is the output of the following code?#include <stdio.h>int main() { for (int i = 0; i < 5; i++) { for (int j = 5; j > 0; j--) { if (i > j) continue; printf("%d%d ", i, j); } printf("\n"); } 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; }
Consider the C program shown below -int sumOdd(int *a, int n){ if(n <= 0) return 0; else if (*a % 2 != 0) return *a + sumOdd (a + 1, n - 1); return sumOdd (a + 1, n - 1);}int main(){ int a[] = {1,2, 3, 4, 5 ,6}; printf("%d\n", sumOdd(a, 6)); return 0;}The output of this program is -912158
What will the result of num1 variable after execution of the following statements? int j = 1, num1 = 4; while (++j <= 10) { num1++; } write the output of the code
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.