Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7019869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:14:30+00:00 2026-05-27T23:14:30+00:00

I am trying to understand the output of the program printed below. When I

  • 0

I am trying to understand the output of the program printed below. When I look at it, I see that when printnum() is called with an argument of 1, “1” will be printed and then since 1<7, the function will call itself. This process will continue until “6” is printed and then printnum(7) is called. So, now “7” is printed and the if condition is not satisfied, so that code is skipped and we move to the second printf(“%d”, x) function where “7” is printed out again. There is nothing after the second printf(“%d”, x), so why doesn’t everything end there? What makes the program keep going to print the numbers again in descending order?

#include <stdio.h>

int printnum ( int x )
{
  printf("%d", x);

  if ( x < 7 )         
  {
      printnum ( x + 1 );    
  }
  printf("%d",x);         
}

int main()
{
printnum(1);
}

Output:

12345677654321

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:14:31+00:00Added an answer on May 27, 2026 at 11:14 pm

    This happens because the second printf is called after your recursion exits at each level.

    As your final recursive call printfs and ends, control transitions to the function that called it – the second-to-last recursive call. This call then exits the scope of the if statement, and calls printf, and then ends – upon which control transitions to the function that called it – the third-to-last recursive call. Repeat until you are inside the call to printnum(1), whose return takes you back to main.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a CGI program that will output a PNG image to
I am trying to understand why this program doesn't work Expected output: numbers 0-19
Trying to understand, how to redirect program output to a file... Code : import
Greetings P4 folks, I am trying to understand the P4 describe -ds output. I
Just trying to understand that - I have never used it before. How is
I've been trying to understand how Ruby blocks work, and to do that I've
I'm trying to get console output from a program using this library uZpRunConsoleApp .
I was trying a program to understand the behavior of structure variable, Sample code:
I am trying to understand nested loops by getting this program to work. I'd
I have been trying to understand of this following C-program: #include <stdio.h> int arr[]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.