The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
0 votes
137 views
Consider the following C code:
include <stdio.h>
int fun()
{
static int num=25;
return num--;
}
int main()
{
for(fun( ); fun();fun())
printf("%d", fun( ));
return O;
}
The sum of the values printed by the above program will be

 

asked in Programming by Active (3.5k points)
edited by | 137 views
0
100?
0

yes 100     how to solve these types of questions

0
first time when for loop is executed it become

1. for(fun=25;fun()//here decrement will take place so now value is 24;fun())

   printf ("%d",fun()   //here decrement will take place so now value is 23)

2. now condition check so value become 22

3 now again for loop for(fun=22;fun()  //here decrement will take place so now value is 21; fun())

 printf ("%d",fun()   //here decrement will take place so now value is 20)

 

nd so on

1 Answer

+1 vote
It won't do anything , it will have compilation error.

Used : instead of ;

If we consider it without errors , equivalent while loop can be written as :-

fun(); //initialization

while(fun()//termination condition when fun() gets 0) {

print("%d",fun());

fun();

}

which will print :-

2320171411852
answered by Loyal (5.5k points)
0
But we are performing 4 fun() in each iteration..so the sequence should be 22,18,14,10,6,2, right?
0
4 . How? initialization happens only once in for loop

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
49,808 questions
54,481 answers
188,249 comments
74,521 users