Aspire Faculty ID #13652 · Topic: JAMIA MILLIA ISLAMIA MCA 2023 · Just now
JAMIA MILLIA ISLAMIA MCA 2023

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.



Previous 10 Questions — JAMIA MILLIA ISLAMIA MCA 2023

Nearest first

Next 10 Questions — JAMIA MILLIA ISLAMIA MCA 2023

Ascending by ID
Ask Your Question or Put Your Review.

loading...