I have had this type of an experience many times: Using printf() in a recursive function and the output is a random number. However, using `fflush(stdout)` fixes the output. Why does this happen? Example:

The following code is a segment tree implementation. The rangeQuery() function returns the maximum element in the given range

```
    node rangeQuery(node* tree, int root, int lml, int rml, int u, int v)
    {
        //query [u,v] where all descendants of root are in [lml,rml]

        fflush(stdout);     //<<<<------------------------------HERE
        if (u<=lml && rml<=v)
            return tree[root];

        int m=(lml+rml)/2, lc=root*2, rc=root*2+1;
        tree[root].split(tree,tree[lc],tree[rc]);

        node l,r;

        if (u <= m)
            l = rangeQuery(tree, lc, lml, m, u, v);
        if (v > m)
            r = rangeQuery(tree, rc, m+1, rml, u, v);

        tree[root].merge(tree,tree[lc],tree[rc]);

        node res;
        res.merge(tree, l, r);
        return res;
    }
    int main()
    {
        //....
        int opt,a,b;
        scanf("%d%d%d",&opt,&a,&b);
        a--;
        b--;
        node newNode;
        newNode = rangeQuery(tree, 1, 1<<n, (1<<(n+1))-1, a+(1<<n), b+(1<<n));
        printf("%d",newNode.val);
        //....
    }

```

The complete code can be found here: <http://ideone.com/cTT0X3>

As can be seen above, removing fflush() from the rangeQuery() function produces a garbled output, whereas using fflush() produces the correct output(3). Any help is appreciated.