Question

1 Determine the output for the following code: #include<stdio.h> int main static int num; fornum : ++ num: num++) -- printf(

please help me asap

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the solution if you have any doubts then you can write in the comment section.

Please give feedback.

Solution:

(1)

static int num;
if we don't assign value to static then by default its value become 0;

Now we consider for loop:

for loop contains 4 steps in execution:

for(initialization;condition check;updation)

{

statement execution;

}

Step 1: initialization step execute.

Step 2: condition check, if condition true then repeat step 3 and 4

Step 3: statement execution

Step 4:updation(increment / decrement)

So we will find our output by using above 4 steps:

for(num++;++num;num++)

{
printf("%d",num);
if(num==8)
break;

}

step1: num++ now num=1

step2: ++num now num=2 it is true so we will repeat step 3 and 4

step3:

printf("%d",num); it will print 2

num != 8 so continue with loop

step 4: updation num++ now num=3

step 5: condition check ++num now num=4

step6:

printf("%d",num); it will print 4

num != 8 so continue with loop

step 7: updation num++ now num=5

step 8: condition check ++num now num=6

step9:

printf("%d",num); it will print 6

num != 8 so continue with loop

step 10: updation num++ now num=7

step 11: condition check ++num now num=8

step12:

printf("%d",num); it will print 8

here num==8 so break from loop

So final output will be:- 2 4 6 8

(2)

line 1: int test;

line 2: for(test=0;test<=5;test++);

line 3: printf("%d",test);

In line 1 a variable test is declared;

In line 2 a for loop is present; but loop is ended with a semicolon ( ; )

it means step 3 is missing according to our rules which I define above.

so for loop execution:

step 1: test=0;

step2: test<=5 true so repeat step 4 until it is true (step3 is not present according to our rules)

step3: nothing

step4: updation test++; so test=1;

step 5: test<=5 true so repeat

step 6: updation test++; so test=2;

step 7: test<=5 true so repeat

step 8: updation test++; so test=3;

step 9: test<=5 true so repeat

step 10: updation test++; so test=4;

step 11: test<=5 true so repeat

step 12: updation test++; so test=5;

step 13: test<=5 true so repeat

step 14: updation test++; so test=6;

step 15: test<=5 which is false now so break from for loop with test=6;

So now we will reach to line 3: printf("%d",test); so it will print 6

So final Output: 6

Thank You!


  

Add a comment
Know the answer?
Add Answer to:
please help me asap 1 Determine the output for the following code: #include<stdio.h> int main static...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT