in Programming in C edited by
1,134 views
3 votes
3 votes

 

#include<stdio.h>
void print(int n)
{
    printf("Hello ");
    if(n++ == 0) return ;
    print(n);
    n++;
}

int main()
{
    void print();
    print(-4);
}

How many times printf execute?? And How ??

in Programming in C edited by
1.1k views

3 Comments

In function definition of print you passed the parameter ( print (int n) ) but in main you havent give the value  ,  Also in main function you have called the print function which is wrong we dont call function like this by giving return value ..
0
0
btw this is the only  function definition and u can do it  in anywhere in the main out side the main it doesn’t matter  and by default function definition is extern and this means u are telling to the compiler that there is a function in this file or may be another file this is completely right code .
0
0
5 times its printed for -4,-3-2-1 and 0.
in function we have printf before the condition checking
so after printing we are checking the condition
0
0

3 Answers

2 votes
2 votes

 

 

Explanation:

  • It defines a recursive function named print which takes an integer parameter n.
  • The function prints "Hello " to the console.
  • It then checks if n++ (post-increment) is equal to 0.
    • If the condition is satisfied, the function immediately returns.
  • Otherwise, the function calls itself recursively with the incremented value of n and increments n again.
  • In the main function:
    • There is a redundant declaration of the print function without any arguments.
    • The print function is called with the argument -4.
  • The execution proceeds as follows:
    • "Hello " is printed to the console.
    • The condition n++ == 0 is evaluated, but not satisfied.
    • The print function is called recursively with the value of n incremented to -3.
    • "Hello " is printed again.
    • The condition n++ == 0 is evaluated, but not satisfied.
    • The print function is called recursively with the value of n incremented to -2.
    • This process continues until n reaches 0, with "Hello " being printed at each step.
    • When n finally reaches 0, the condition n++ == 0 is satisfied, and the function returns without making any further recursive calls.
  • The output of the program will be "Hello Hello Hello Hello Hello ".
  • It's important to note that the behavior of the code assumes n starts with a negative value and is decremented by 1 in each recursive call.
edited by
1 vote
1 vote
The `print` function in the given code is recursive, which means it calls itself repeatedly until a certain condition is met. Let's analyze the code step by step to determine how many times `printf` is executed.

1. In the `main` function, the `print` function is declared as `void print();`. However, this declaration is unnecessary since the `print` function is already defined above.

2. The `print` function is called with the argument `-4`.

3. Inside the `print` function:
   - The string "Hello " is printed using `printf`.
   - The condition `n++ == 0` is evaluated. Since the value of `n` is initially `-4`, the condition evaluates to false (`-4++` is not equal to 0).

4. The `print` function is recursively called with the argument `n` incremented by 1.

5. Inside the second call of the `print` function:
   - The string "Hello " is printed again.
   - The condition `n++ == 0` is evaluated. This time, `n` is `-3` (since it was incremented by 1 in the previous call). The condition again evaluates to false (`-3++` is not equal to 0).

6. The `print` function is recursively called again with `n` incremented by 1.

7. This recursive process continues until `n` reaches 0. The `print` function is called a total of 5 times (including the initial call). The value of `n` in each call will be: -4, -3, -2, -1, 0.

8. When `n` becomes 0, the condition `n++ == 0` evaluates to true, and the function returns without any further recursive calls.

In conclusion, the `printf` statement is executed 5 times in total, each time printing the string "Hello ".

1 comment

I think you didn’t notice n++ after the recursive call in print() block. The final value of n would be 1.

 

0
0
0 votes
0 votes
5 times