Question:
Increment ProbLem!!!! :(?
Praveen
2012-09-09 03:44:46 UTC
if a=1
then what would be " (++a)+(++a) " ????
I thought the answer would be 5 as a will become because of the first pre increment and 3 because of the second preincrement
but in TurboC, the displaying answer is 6
I don't know why
plz HELP!!!!!!
Five answers:
?
2012-09-09 03:55:13 UTC
First forget Turbo C, 30 years old compiler

Rule of thumb is: if a variable contains n prefix and m postfix operators them first it applies n prefix operators and with the modified value expression is evaluated after that the variable value will be changed by m times.



Thus your expression is given 6. That is, a is becoming 3 after two prefix operators that is used in evaluation expression. thus 3+3 becomes 6
?
2012-09-09 04:12:13 UTC
A normal language like Java would output 5 in any case and on any machine, but C is known for how little is guaranteed.

Java guarantees that

- a will be incremented, and the new value used as the left operand.

- THEN a will be incremented, and the new value used as the right operand.

- THEN the addition is performed.

The compiler may rearrange the instructions ONLY if that does not affect the result.



C guarantees that

- a will be incremented, and its value (at any point after the increment) will be used as the left operand.

- ALSO a will be incremented, and its value (at any point after the increment) will be used as the right operand.

- THEN the addition is performed.

Turns out that the following is a valid execution order:

++a; ++a; a+a;



In fact, the assembly instructions (highest level of optimisation) may end up:

MOV EAX,[a] ; load the variable

INC EAX ; pre-increment 'a' (left operand)

INC EAX ; pre-increment 'a' (right operand)

MOV [a],EAX ; store 'a' at the last possible moment

SHL EAX,1 ; left-shift by 1 = multiply by 2 = add to itself

; now EAX contains 6.
?
2012-09-09 04:56:19 UTC
datz simple...

u r 90% near d answer...

when a variable is found wid any number of increments n dedcrements, they r performed first n the resulting value is substituted in their places...

hence in ur prog,

two times preincrementing a gives 3, so substituting in its places, 3+3 gives 6...

:)
?
2017-03-02 16:58:23 UTC
>> How is this difficulty fastened? it relatively is not, and don't even attempt. it relatively is the character of vehicle-incrementing columns in enormously much each database accessible. The *purely* component that the form of column will oftentimes assure you is that each and each value would be unique, and greater suitable than previous values interior the column. It *by no ability*, in any database I certainly have considered, assure's no gaps. that's not a *undesirable* component. the only maximum serious component an vehicle-increment column can provide is a *ROCK sturdy* assure that the cost would be unique. assume that, yet not the rest. The question i'd ask you is: Why do you decide on this? Why do you decide on the database to pass backfill your identity column with deleted identity values? What difficulty are you *relatively* attempting to sparkling up? i'm one hundred ten% particular that there is a greater effective thank you to do despite you attempt to do here. let us know what you're relatively attempting to end, and the respond will in all probability be ordinary.
Rahul Madanu
2012-09-09 07:40:45 UTC
3

Process returned 1 (0x1) execution time : 0.014 s

printf("%d",a);

not -> printf("%d",&a); -.- i know wat u did !!!!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...