Aditi Paliwal
Aditi Paliwal

Reputation: 157

Different behaviour of ++ operator in objective C?

I compiled a program in objective C.

int a = 3;

a = (a++) * (a++);

NSLog(@"a= %d",a);

Return the output: a=11

Whereas in c# the output is 12.

Can anyone explain me the difference in the behaviour of ++ operator in objective C?

Upvotes: 0

Views: 94

Answers (1)

Lily Ballard
Lily Ballard

Reputation: 185671

There's no difference in the ++ operator. The problem is you're invoking undefined behavior. Specifically, you're reading and updating the a variable twice within the same sequence point, which is explicitly undefined in the ANSI C spec. The value you get from that expression may differ from compiler to compiler, it may even differ between versions of the same compiler, or even between invocations of the same compiler version with separate optimizations turned on.

Upvotes: 6

Related Questions