Deprecated: Implicit conversion from float-string "1684550268.511" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1684550268.511" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1684550268.511" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1684550268.511" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1684550268.511" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 594

Deprecated: Implicit conversion from float-string "1686131481.314" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1686131481.314" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1686131481.314" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1686131481.314" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1686131481.314" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 594

Deprecated: Implicit conversion from float-string "1688469411.176" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1688469411.176" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1688469411.176" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1688469411.176" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1688469411.176" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 594

Deprecated: Implicit conversion from float-string "1689648955.372" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1689648955.372" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1689648955.372" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1689648955.372" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1689648955.372" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 594

Deprecated: Implicit conversion from float-string "1684578555.960" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1684578555.960" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1684578555.960" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1684578555.960" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1684578555.960" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 594

Deprecated: Implicit conversion from float-string "1687676654.925" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1687676654.925" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1687676654.925" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1687676654.925" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1687676654.925" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 594
Programming in C: C Programming Recursion
edited by
1,140 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 ??

edited by

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 votes
1 votes
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 ".

Related questions


Deprecated: Implicit conversion from float-string "1547289821.899" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1547289821.899" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1547289821.899" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1547289821.899" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803
666
views
1 answers
0 votes
Laxman Ghanchi asked May 19, 2023
666 views
#include<stdio.h>void print(int n){ printf("Hello "); if(n++ == 0) return ; print(n); n++;}int main(){ void print(); print(-4);}
917
views
0 answers
1 votes
Na462 asked Jan 12, 2019
917 views
Number of times # will be printed on foo(7) ?