Expressions
(C-FAQ's)
Q1: Can you explain
iNum
= iNum++ * iNum++ expression result
is undefined , but
iNum
= iNum++ || iNum++ result is always
same?
Output:
According
to the C standard an object's stored value can be modified only once between
two sequence points.
Sequence points occurs at following places:-
1) at the end of full expression( which is not a sub expression ).
2) at && , || and ?: operators.
3) at a function call, after evaluation of all the arguments,
just before the actual call.
So first expression is being modified twice
between sequence points, that is why behavior is undefined. But the second
expression is getting modified once before sequence point || and once after
that.
Q2: Can you rewrite the following expression?
iNum1 <=
50 ? iNum2=100 : iNum2=200;
Output:
The above expression can be rewrite as :-
iNum2
= iNum1 <= 50 ? 100:200 ;
Q3: Can you rewrite the following expression?
iNum1
<=50 ? iNum2=200: iNum3=200;
Output:
The above expression can be rewrite as:-
*( ( iNum1 <= 50 ) ?
&iNum2 : &iNum3 ) = 200 ;
Q4: What is the output of the following code?
int main()
{
int
iNum=2;
printf("%d
%d\n ", ++iNum, ++iNum );
return
0;
}
A. 3 4
B. 4 3
C. 4 4
D. Output may vary from compiler to
compiler.
Output:
D. Output
may vary from compiler to compiler
4 4 (In GCC Compiler)
No comments:
Post a Comment