Question

Question 3) What is the output of the following code? (4 marks) #include <stdio.h> int main(void) Output: int i; for (i = 0;i

please explain very well.Thanks

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

The output of the given program will be:

1 12 13 14 1

Explanation:

Static variable:

The static variable is initialized only once by using constant literals only and it has its previous value in their previous scope and this variable is not initialized again and again. If it is not initialized by the programmer or explicitly then it will be initialized with 0 by default.

The lifetime of a static variable is the whole program and it is stored in the data segment.

So, in the given program, the static variable is initialized with zero in the first iteration and it is incremented again and again in the next iteration.

But a normal variable is initialized again and again, so this will result value 1 in each of the iterations.

The first iteration output will be:

1 1

The second iteration output will be:

2 1

The third iteration output will be:

3 1

The fourth iteration output will be:

4 1

So, the output of the given program will be:

1 12 13 14 1

Add a comment
Know the answer?
Add Answer to:
please explain very well.Thanks Question 3) What is the output of the following code? (4 marks)...
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