Aspire's Library

A Place for Latest Exam wise Questions, Videos, Previous Year Papers,
Study Stuff for MCA Examinations - NIMCET

Previous Year Question (PYQs)



What is the output of following C program?

void e(int x)
{
    if (x > 0)
    {
        e(- -x);
        printf("%2d", x);
        e(- -x);
    }
}

int main()
{
    e(3);
    return 0;
}





Solution

Solution:

In C, the tokens `- -x` are parsed as two unary minuses with a space, i.e. $-(-x)=x$.
So both recursive calls are `e(x)`, not `e(--x)`.

For `x=3`, the function calls itself **with the same positive value** forever:
`e(3) → e(3) → e(3) → ...` and never reaches a base case.

This causes infinite recursion (stack overflow) at run time.





Online Test Series,
Information About Examination,
Syllabus, Notification
and More.

Click Here to
View More


Online Test Series,
Information About Examination,
Syllabus, Notification
and More.

Click Here to
View More

Ask Your Question or Put Your Review.

loading...