segmentation fault (core dumped) Error get prints before executing 'printf' statements
<p>I am trying to merge two already sorted linked lists <code>temp1</code> and <code>temp2</code>. I am getting segmentation fault for this example :</p>
<p>Input</p>
<pre><code>temp1 : 1-&gt;NULL

temp2 : 2-&gt;3-&gt;4-&gt;5-&gt;6-&gt;NULL
</code></pre>
<p>Output</p>
<p><img src="https://i.stack.imgur.com/GYOu4.png" alt="1st Output" /></p>
<p>But if i remove &quot;\n&quot; from <code>printf</code> statements then it directly prints segmentation fault.</p>
<p><img src="https://i.stack.imgur.com/AwzcZ.png" alt="2nd Output" /></p>
<p>Why this is happening?</p>
<p>Code :</p>
<pre><code>list *merge(list * temp1, list * temp2)
{
    list *x, *y, *z;

    x = temp1;
    y = temp2;
    print(x);
    print(y);

    z = (list *) malloc(sizeof(list));

    if (x-&gt;key &lt; y-&gt;key)
    {
        z-&gt;key = temp1-&gt;key;
        x = x-&gt;next;
    }
    else
    {
        z-&gt;key = temp2-&gt;key;
        y = y-&gt;next;
    }
    z-&gt;next = NULL;

    while (x || y)
    {
        printf(&quot;asdf\n&quot;);
        if (x == NULL)
        {
            z = append(z, y-&gt;key);
            y = y-&gt;next;
            printf(&quot;x\n&quot;);
        }
        if (y == NULL)
        {
            z = append(z, x-&gt;key);
            x = x-&gt;next;
            printf(&quot;y\n&quot;);
        }
        if (x &amp;&amp; y)
        {
            printf(&quot;x &amp;&amp; y\n&quot;);
            if (x-&gt;key &lt; y-&gt;key)
            {
                z = append(z, x-&gt;key);
                x = x-&gt;next;
            }
            else
            {
                z = append(z, y-&gt;key);
                y = y-&gt;next;
            }
        }
    }
    print(z);
    return z;
}
</code></pre>
<p>Thanks in advance.</p>
<p>I have already removed the segmentation fault. I am asking why it gets delayed when i remove the '\n' from printf statements?</p>