Explain this program step by step | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Explain this program step by step

#include <stdio.h> void say_hello(); int main() { int i; for (i = 0; i < 5; i++) { say_hello(); } return 0; } void say_hello() { static int num_calls = 1; printf("Hello number %d\n", num_calls); num_calls++; }

22nd Mar 2019, 5:04 PM
Shailendra Singh
Shailendra Singh - avatar
2 Answers
+ 3
A for loop: starts at 0, ends at 4 (i < 5). i++ = i+1 (steps) Your loop runs 5 times and calls say_hello() --> this a method/function Whats inside your function? There is an integer and a print statement. num_calls = 1 print hello user and the value of num_calls Then increment num_calls //it is now 2 For loop --> i = 0 --> jump in your method --> jump back in the for loop --> i = 1 --> jump in your method .... and so on until i = 4 You can use the code playground to see the output.
22nd Mar 2019, 6:21 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
I got it the answer is in the function defintion for every loop it prints and increment by 1 and another loop print that value and again incremented by 1 and then used by next loop and so on
22nd Mar 2019, 5:07 PM
Shailendra Singh
Shailendra Singh - avatar