0
#include<stdio.h>

int print1To10(int);

int main()
{
    int N=3; 

    printf("\nNumbers from 1 To 10 are: ");
    print1To10(N);`

    return 0;
}

int print1To10(int N)
{
    if(N)
        printf("%d",print1To10(N-1));
    else
        return 0;
}

Output of this code is found 011,but if I add a space after %d then output becomes 0 2 2.Again If I further give a space after %d the output becomes 0 3 3 and this incrementing goes on and on with incrementing space.Why this?

N:B:I'm a newbie and working on C and code-block IDE.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
M.M. Nayem
  • 11
  • 1
  • 2
    What should/does `print1To10` return if `N != 0`? – G.M. May 18 '22 at 11:18
  • 3
    Your program has [undefined behavior](https://en.wikipedia.org/wiki/Undefined_behavior) because it is using a non-existant return value of the function `print1To10`. – Andreas Wenzel May 18 '22 at 11:19
  • The reason for random value printed could be https://stackoverflow.com/questions/4644860/function-returns-value-without-return-statement – Deepak Patankar May 18 '22 at 11:22
  • The duplicate I linked plus the fact that your return value of that function is the value which gets printed and increases with the return value of printf, which is the number of printed characters. Though that "explanation" is already forbidden speculation of what undefined behaviour causes.... – Yunnosch May 18 '22 at 11:27

0 Answers0