find value

P

pwnalwandi

hi everyone out there
can anyone give me the value of a(wn executed saperatly)
initially a=5
a+=(a++)+(++a)
a-=(--a)-(a--)
support ur answers with reasons (precedence,assoivity)
thanks in advance.
 
J

junky_fellow

hi everyone out there
can anyone give me the value of a(wn executed saperatly)
initially a=5
a+=(a++)+(++a)
a-=(--a)-(a--)
support ur answers with reasons (precedence,assoivity)
thanks in advance.

You are trying to modify the variable "a" more than once without an
intervening sequence point. The behaviour is undefined and may produce
different values of variable
"a" on different implementations.
For more details, search for "sequence points" in this
newsgroup. You will find enough information.

Its always better not to use such expressions if there
are other better/simpler ways of doing the same.
 
S

Suman

give me the values saperately(2 values of a)
42 & 42. But there may be slight variations depending on the
temperature.

0. Learn some basic rules when you are going to post to a
newsgroup. There is enough information in this newsgroup only,
so search.
1. Check the FAQ. 3.2 should be of particular interest to you.
That said, read the whole of section 3, and if you still have some
time, the whole FAQ.
 
J

junky_fellow

give me the values saperately(2 values of a)
It could be anything 17,18 or 19.

a+= a++ + ++a;

Its not guaranteed that which of subexpression a++ or
++a would be evaluated first. Suppose, that some implementation
does a++ first and then ++a. Taking initial value of a=5,

a++ would take a initial value 5 before incrementing. Now,
since there is no sequence point after a++, its not guaranteed
if the location where "a" is stored has the updated value or not.
So,while evaluating ++a, "a" could be either 5 or it could be 6. Also,
when ++a is evaluated, again there's
is no guarantee if location "a" has been updated with the
incremented value. So, when you add (a++ + ++a) to a, you
are not sure what would be the value of a. It could be 5,6 or
even 7.
The only sequence point in this statement is "semicolon" after
which all the side effects due to this statement would have
been settled.
 
K

Keith Thompson

You are trying to modify the variable "a" more than once without an
intervening sequence point. The behaviour is undefined and may produce
different values of variable
"a" on different implementations.
For more details, search for "sequence points" in this
newsgroup. You will find enough information.

Its always better not to use such expressions if there
are other better/simpler ways of doing the same.

It's always better not to use such expressions.
 
K

Keith Thompson

It could be anything 17,18 or 19.

a+= a++ + ++a;

Its not guaranteed that which of subexpression a++ or
++a would be evaluated first. Suppose, that some implementation
does a++ first and then ++a. Taking initial value of a=5,

a++ would take a initial value 5 before incrementing. Now,
since there is no sequence point after a++, its not guaranteed
if the location where "a" is stored has the updated value or not.
So,while evaluating ++a, "a" could be either 5 or it could be 6. Also,
when ++a is evaluated, again there's
is no guarantee if location "a" has been updated with the
incremented value. So, when you add (a++ + ++a) to a, you
are not sure what would be the value of a. It could be 5,6 or
even 7.
The only sequence point in this statement is "semicolon" after
which all the side effects due to this statement would have
been settled.

Sorry, but that's nonsense. Modifying an object twice between
sequence points invokes undefined behavior. That doesn't mean that
the expression can evaluated in any of several orders; it means the
behavior is undefined. The result can be 5, 6, 7, 42, sqrt(-1), a
week from next Wednesday, dental floss, or the third plxnrfq on the
left.

Most of these are unlikely in real life, of course, but you really can
get results that are inconsistent with any order of evaluation.
 
F

Flash Gordon

give me the values saperately(2 values of a)

Try actually reading the archives for the group. junky_fellow was
correct that you will find all the information you need if you search
for "sequence points" on this group.

Also, even when using Google provide context otherwise people often will
have no idea what you are responding to. Again, if you read the group
you will find lost of posts telling you have to do this.

Before you post in ANY group, read the FAQ and a weeks worth of postings
to find out what the group is about. You question is also answered in
the FAQ as much as it can be sensibly answered.

Your post which junky_fellow answered was:
hi everyone out there
can anyone give me the value of a(wn executed saperatly)
initially a=5
a+=(a++)+(++a)
a-=(--a)-(a--)
support ur answers with reasons (precedence,assoivity)
thanks in advance.

The answer is:
When the first statement is executed a daemon flies out of your nose.
The preceded is an ancient post in this group which you can find by
searching for nasal daemons in the group and selecting the earliest posts.
When the second statement is executed the daemon will put a stake
through your heart. No precedent is needed because nasal daemons can do
anything they feel like doing (and anything they don't feel like doing)
at any time.

In other words, anything can happen because, as you were told, the
behaviour is undefined.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,915
Members
47,456
Latest member
JavierWalp

Latest Threads

Top