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 8614953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:15:03+00:00 2026-06-12T05:15:03+00:00

I am really new to programming (I’m an electronics and comm. engineer) and I

  • 0

I am really new to programming (I’m an electronics and comm. engineer) and I am not able to figure out why one program works and the other one doesn’t.

I’d like to get a good understanding of recursive functions before going any further with my learning. I would appreciate any help regarding this.

I know the difference between the x++ and –x. But in this context of this program, I thought both of these programs should run the same way. But they don’t.

void rec(int x)
{
    if(x>0)
        rec(x--);
    printf("%d",x);
}

int main()
{
    rec(4);
    _getch();

} /*This doesn't work. And shows a stackoverflow */

void rec(int x)
{
    if(x>0)
        rec(--x);
    printf("%d",x);
}

int main()
{
    rec(4);
    _getch();

} /*This gives a proper output as expected*/ 
/*The output is 00123*/

Thanks!

P.S: Forgive me if this is a trivial or stupid question, but I am stuck on this and I thought this is the best place I can look for help.

  • 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-06-12T05:15:04+00:00Added an answer on June 12, 2026 at 5:15 am
    void rec(int x) {
        if (x>0)
            rec(x--);
        printf("%d",x);
    }
    

    This will recurse forever (or at least until you exhaust your stack space). x-- means use the current value of xand then decrement it.

    In other words, let’s call rec with the parameter 20. That’s greater than zero, so it will in turn call rec again with the current value of 20 then decrement x (but effectively after the call returns.

    Hence you’re forever calling rec with the value of 20, which is why you’re blowing out your stack.

    If you change x-- to --x, it decrements x before passing it to the function, hence it will go 20, 19, 18, ... 1, 0, at which point it will run back up the stack printing all those values.

    If you had put a printf ("before: %d\n", x) before the if statement, you would have seen a lot of 20 lines output to the screen.

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

Sidebar

Related Questions

I am not that really new in Programming but in Python I am really
I'm really new to Windows Forms programming and not quite sure what's the right
I'm really new to programming... I set up a class to give supporting information
I'm really new to programming, so I'm having trouble explaining this -- please forgive.
This is a kind of a newb question but I'm really new to programming
Hey there, I'm a designer thats really new to programming with xcode or Objective-C
I am really new to the programming but I am studying it. I have
Ok guys, I'm really new at python (and programming itself) so sorry for my
I'm new in programming windows phone 7 but get really tired cuz i spend
I really need your help for this. I am relatively new to programming and

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.