Q:

Printing program, please explain output

#include <stdio.h>

void myPrint (int n) {
    printf("%d", n/2);
    if(n > 0)
        myPrint (n - 1);
    printf("%d", n);
}

int main (void) {
    int count = 4;
    myPrint (count);
    return 0;
}

This simple printing programs prints 2110001234, Someone please explain why does it print 01234 at the end. I am not sure why does it add 1 everytime.

A:

At the end you are seeing the calls to myPrint in reverse the order they happened.
Here is one way to look at what is happening during each recursive call.
myPrint(4)
  printf("%d", n/2)  // Prints 2 because 4/2 = 2
  myPrint(n - 1) // Calls myPrint(3)
    printf("%d", n/2)  // Prints 1 because 3/2 = 1
    myPrint(n - 1) // Calls myPrint(2)
      printf("%d", n/2)  // Prints 1 because 2/2 = 1
      myPrint(n - 1) // Calls myPrint(1)
        printf("%d", n/2)  // Prints 0 because 1/2 = 0
        myPrint(n - 1) // Calls myPrint(0)
          printf("%d", n/2)  // Prints 0 because 0/2 = 0
          // Does not execute if statement
          printf("%d", n);  // Prints 0 because n = 0 at this call
        printf("%d", n);  // Prints 1 because n = 1 at this call
      printf("%d", n);  // Prints 2 because n = 2 at this call
    printf("%d", n);  // Prints 3 because n = 3 at this call
  printf("%d", n);  // Prints 4 because n = 4 at this call

A:

myPrint is recursive function which is printing the following grammar:
myPrint(n) -> printf(n/2) ++ myPrint(n-1) ++ printf(n)

for every n>0, while for n=0 the grammar is
myPrint(0) -> printf("00")

So for n = 4:
myPrint(4) -> 
-> "2" ++ myPrint(3) ++ "4" ->
-> "2" ++ ("1" ++ myPrint(2) ++ "3") ++ "4" ->
-> "21" ++ ("1" ++ myPrint(1) ++ "2") ++ "34" ->
-> "211" ++ ("0" ++ myPrint(0) ++ "1") ++ "234" ->
-> "2110" ++ ("00") ++ "1234" ->
-> "2110001234"

Here++ denotes concatenation.

