Sudeep
Sudeep

Reputation: 23

Evaluation Order of Operands

What will be the output of the following program in C?I am basically confused whether main() will call t1() or t2() first.

#include <stdio.h>

int a=40;
int t1()
{
    int a=20;
    return a;
}
int t2()
{
    int a=30;
    return a;
}
int main()
{
    int k=t1() + t2();
    printf("%d",k);
    return 0;
}

Upvotes: 0

Views: 59

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 477620

Since all the functions return the values of local variables, your code is identical to this:

int a = 40;

int t1() { return 20; }
int t2() { return 30; }

int main() { printf("%d", t1() + t2()); }

Or, even simpler:

int a = 40;
int main() { printf("%d", 20 + 30); }

Or simpler yet:

int a = 40;
int main() { fputs("50", stdout); }

In the first version, it is both unspecified and irrelevant which function call subexpression is evaluated first.

Upvotes: 4

Related Questions