Sunday 30 October 2011

Predict the output or error(s) for the following:

main()

{

int c=- -2;


printf("c=%d",c);


}

Answer:

c=2;

Explanation:

Here unary minus (or negation) operator is used twice. Same maths  rules applies, ie. minus * minus= plus.

Note: However you cannot give like --2. Because -- operator can  only be applied to variables as a decrement operator (eg., i--). 2 is a constant and not a variable.

1 comment: