1
#include <stdio.h>

int c=0;

int check (int c)
{
    c=c+1;
    if (c<6)
        return check(c);
    printf("%d\n", c);
}

int main()
{
    printf("%d\n",check(c));
}

The above code gives me

6
2

as the output. 6 is from the printf function inside the check function but I did'nt get why 2. If I remove the printf function inside the check function I get

5

as the output. Can anyone explain why this happens?

7
  • 1
    Not returning a value from a non-void function is undefined behaviour, for which there may not be a good explanation. (But what probaly has happened in this case is that the return value from printf, namely the number of characters printed, is returned.) Commented Oct 28, 2017 at 15:50
  • 1
    What does the compiler tell you? warning C4715: 'check': not all control paths return a value. You have undefined behaviour. Commented Oct 28, 2017 at 15:51
  • @WeatherVane Compiler doesn't give any warning. It compiled without any errors. Commented Nov 3, 2017 at 19:58
  • @MOehm Maybe it's true. But why always 2 even I change the condition to c<8? Can you help me solve that condition? Commented Nov 3, 2017 at 20:03
  • @Darshan it is not a compiler error but a compiler warning. Please find a way to enable all warnings. Commented Nov 3, 2017 at 20:26

2 Answers 2

1

You are invoking undefined behavior. So it behaves oddly.

You must have a proper return statement in the check function as you are supposed to return an int.

Also having printf or not having it - doesn't change anything- it's still UB.(in this case)

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer.I got it - it's UB. But why always 2 . it's 2 even though I change the condition to c<8. Since it was always 2, I thought there might be a reason.
@Darshan.: Sorry for late reply. I was busy in the weekend. I guess you got it answered. Thanks.
Yep. Thanks for it
0

I made a few modifications to your posted code so that it becomes obvious what is happening. It looks like the compiler outsmarted you, and prepended return in front of printf("%d\n", c); So the 2 that you are seeing is the number of characters printed when you print 6\n on the last iteration of check(). The 2is then passed all the way down the stack to the original call inside the main, eventually ending up on your Console window. See this reference

[printf returns the] number of characters transmitted to the output stream or negative value if an output error or an encoding error (for string and character conversion specifiers) occurred.

Consider the following code. It exhibits identical behaviour to your posted code.

#include <stdio.h>

int check(int c)
{
    int ret = 0;
    c = c + 1;
    if (c < 6)
    {
        ret = check(c);
        return ret;//helps inspect ret
    }
    //looks like the compiler inserts that return statement 
    ret = printf("%d\n", c);// 2 <- number of characters in "6\n".
    return ret;
}

int main()
{
    int c = 0;
    int ret = 0; 
    ret = check(c);//ret comes out == 2. This is the return value of printf
    printf("%d\n", ret);
    getchar();
    return 0;//don't forget this guy!
}

Disclaimer: Even though it appears that this is what is happening, and most likely it probably is, It is not a shortcut and I would not count on this happening every time. The behaviour is still undefined! The best thing to do is to return from functions normally.

2 Comments

Thanks for the answer. My purpose was to understand what happens when I don't return valid value. Since I started getting 2, even though I change the condition of c, I thought there might be some valid explanation.
@Darshan, you're welcome. I don't think it's possible for anyone to provide a valid explanation since returning a recursive call is undefined behaviour as coderredoc explained in his answer. There is no point trying to find rhyme or reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.