please explain how the compiler calculates the following expressions and which variable get what value
1...assume x=1 i=2 j=3 k=4
x-=k*=j/=i%=5; (please explain how this works)
2... x=5 i=4 j=3 k=2
x*=1 + (i%= 1+(j/=-1+ ++k)
3....i=j=10;
i%=j=(j=5)%(i=3);
thanks
Is there some one who can help me with some complex math Expressions in 'C' programming.(details given below)
in C the evaluation for an expression goes like this:
Evaluate the right part of expression and then the left one
and i=5 returns 5
so:
1. x-=k*=j/=i%=5;
- i%=5 -%26gt; 2 and i=2
- j/=i%=5 -%26gt; j/=2 -%26gt; 1 and j = 1
- k*=j/=i%=5 -%26gt; k*= 1 -%26gt; 4
- x-=k*=j/=i%=5 -%26gt; x-=4 -%26gt; -3 and x= -3
2.x*=1 + (i%= 1+(j/=-1+ ++k)
- ++k - %26gt; 3 and k=3
- j/=-1+ ++k -%26gt; j/=-1 + 3 -%26gt; j/= 2 -%26gt; 1
-... i%= 1+(j/=-1+ ++k) -%26gt; 0
- x*=1 + (i%= 1+(j/=-1+ ++k)) -%26gt; x*= 1+0
- x = 5
3.i%=j=(j=5)%(i=3)
- i=3 -%26gt; 3 and i become 3
- j=5 -%26gt; same
- (j=5)%(i=3) 0 -%26gt; 2
- j=(j=5)%(i=3) -%26gt; j=2 -%26gt; 2 and j become 2
- i%=j=(j=5)%(i=3) -%26gt; i%= 2 -%26gt; i = 3 % 2 = 1
Reply:1...assume x=1 i=2 j=3 k=4
x-=k*=j/=i%=5;
Let's split this up, it's a simple set of operators:
"X-=" x minus equals: This means subtract a value from X. Example is X-=5: This means subtract 5 from whatever the value of X is.
"K*=" K multiplied equals - as above, only with multiplication.
"J/=" J F-Slash equals - As above only with division
"i%=" I Percent Equals - Modulus. Show the remainder after a division. Example 10%3=1 (keep removing a value until a lesser value is left)
2
You can use the same as previous - there's a new symbol here though:
"++" - Increment by 1. If the "++" is before a variable, we PREINCREMENT, otherwise we POSTINCREMENT, this determines the time to increment based on other operations.
3
There's nothing new here.
Take all of these apart using this explanation, and you should have no problems. There's also a website:
http://www.cplusplus.com
for help.
Run it through a compiler yourself, and use printf or cout statements to display variable values, it's an important part of programming to do this "lazy" debugging.
2... x=5 i=4 j=3 k=2
x*=1 + (i%= 1+(j/=-1+ ++k)
3....i=j=10;
i%=j=(j=5)%(i=3);
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment