i = 10; result = ++i - --i; How result become ZERO

L

Lakshmi Sreekanth

Hi,
Please clarify my doubt...

void main( void )
{
int result = 0, i = 10;

result = ++i - --i;

printf (" Result: %d ", result);
}

Output: Result: 0

WHY ?????? HOW ????? Please clarify this ........
 
I

Ian Collins

Hi,
Please clarify my doubt...

void main( void )
{
int result = 0, i = 10;

result = ++i - --i;

printf (" Result: %d ", result);
}

Output: Result: 0

WHY ?????? HOW ????? Please clarify this ........

Chance.
 
N

Nick Keighley

Please clarify my doubt...

in standard english you have a "question" not a "doubt"
void main( void )

that should be
int main (void)


main *always* returns an int
{
        int result = 0, i = 10;

        result = ++i  -  --i;

undefined behaviour. The standard doesn't define the behaviour of a
statement with multiple ++ or -- in it. See the comp.lang.c FAQ for
reasons or google it.

Why are you writing crazy code like this?
        printf (" Result: %d ", result);

main returns an int so you need something like

return 0;
}

Output:   Result: 0

WHY ?????? HOW ????? Please clarify this ........

the compiler can do anything it damn well pleases as this in Undefined
Behaviour
 
N

Nick Keighley

in standard english you have a "question" not a "doubt"


that should be
int main (void)

main *always* returns an int



undefined behaviour. The standard doesn't define the behaviour of a
statement with multiple ++ or -- in it.

*that operate on the same variable*

(technically the same "object" (storage location)
 
M

Mark Bluemel

Hi,
Please clarify my doubt...

void main( void )
{
int result = 0, i = 10;

result = ++i - --i;

printf (" Result: %d ", result);
}

Output: Result: 0

WHY ?????? HOW ????? Please clarify this ........

We might - but first, please clarify some things for us :-

* What did you expect to happen?
* Why did you expect this?
 
A

August Karlstrom

We might - but first, please clarify some things for us :-

* What did you expect to happen?
* Why did you expect this?

And above all, why would you want to write such a a statement in the
first place?


/August
 
L

Lakshmi Sreekanth

And above all, why would you want to write such a a statement in the
first place?

/August


I attended some technical test(interview), there I faced this
program.
=> For that Program, *20* was not there as a option. After the test I
executed the program, the result is: 20
Options in that test are: A ) 19, B ) 21, C )
10, D ) Error

Don't suggest me on the way of writing code. (It's a test program)
Suggest me on the result.

TR,
Sreekanth
 
I

Ian Collins

I attended some technical test(interview), there I faced this
program.
=> For that Program, *20* was not there as a option. After the test I
executed the program, the result is: 20
Options in that test are: A ) 19, B ) 21, C )
10, D ) Error

Don't suggest me on the way of writing code. (It's a test program)
Suggest me on the result.

Everyone who's replied already has - the result is undefined.

The result could your PC eating your cat.
 
J

James Dow Allen

in standard english you have a "question" not a "doubt"

(He presumably seeks a clear answer,
not clarification of his own question!)
undefined behaviour.

In standard English(*), it is "behavior" that is undefined.

Addressing OP's doubt, may I recommend that understanding
the behavior of valid programs is a better use of your
time than guessing the output of invalid programs?

James Dow Allen

(* - Yes, she's your Queen; but "we" have more nuclear weapons.)
 
B

BartC

I attended some technical test(interview), there I faced this
program.
=> For that Program, *20* was not there as a option. After the test I
executed the program, the result is: 20

I thought you said the result was 0.
Options in that test are: A ) 19, B ) 21, C )
10, D ) Error

The answer's (D) Error (in the source code) since the result is not
predictable.
 
O

osmium

Lakshmi said:
I attended some technical test(interview), there I faced this
program.
=> For that Program, *20* was not there as a option. After the test I
executed the program, the result is: 20
Options in that test are: A ) 19, B ) 21, C )
10, D ) Error

Don't suggest me on the way of writing code. (It's a test program)
Suggest me on the result.

As a not very interested bystander, it would seem the test is wrong. It
strikes me as a very poor question to include in a multiple choice test; the
code should not have been written, as the FAQ makes clear. It is barely
*conceivable* that they wanted "Error" as an answer. In US English that
would make little sense, but there is English and there is English, as you
well know.

There must be a huge number of applicants in India when they have to resort
to such a crude means of testing.
 
J

John Bode

I attended some technical test(interview), there I faced this
program.
=> For that Program, *20*  was not there as a option. After the test I
executed the program, the result is: 20
Options in that test are:      A ) 19,          B ) 21,        C )
10,       D ) Error

Don't suggest me on the way of writing code. (It's a test program)
Suggest me on the result.

TR,
Sreekanth- Hide quoted text -

- Show quoted text -

D is the least wrong answer, but "undefined behavior" is not
necessarily the same thing as an error. Here's how the language
standard defines "undefined behavior":
3.4.3
1 undefined behavior
behavior, upon use of a nonportable or erroneous program construct or
of erroneous data, for which this International Standard imposes no
requirements

2 NOTE Possible undefined behavior ranges from ignoring the situation
completely with unpredictable results, to behaving during translation
or program execution in a documented manner characteristic of the
environment (with or without the issuance of a diagnostic message),
to terminating a translation or execution (with the issuance of a
diagnostic message).

3 EXAMPLE An example of undefined behavior is the behavior on integer
overflow.

Undefined behavior doesn't *have* to result in an error condition;
statements like "result = ++i - --i;" will simply give different
results on different platforms (or even different results on the
*same* platform depending on compiler settings and even the
surrounding code).

Now, *why* does the statement "result = ++i - --i;" result in
undefined behavior? Because the standard says so:
6.5 Expressions

[snip]

2 Between the previous and next sequence point an object shall have
its stored value modified at most once by the evaluation of an
expression.72) Furthermore, the prior value shall be read only to
determine the value to be stored.73)

[snip]

73) This paragraph renders undefined statement expressions such as
i = ++i + 1;
a[i++] = i;
while allowing
i = i + 1;
a = i;


A sequence point is a point in the program's execution where all the
side effects from the previous operation have been applied and before
any side effects from the next operation are applied.

For the statement

result = ++i - --i;

there are 3 side effects: assigning the value of "++i - --i" to
result, adding 1 to i, and subtracting 1 from i. In this case, the
sequence point occurs at the end of the statement. Thus, i is being
modified more than once between sequence points, which makes the
expression undefined per footnote 73.

A very important thing to remember with the ++ and -- operators is
that the side effects don't have to be applied immediately; the only
requirement is that they be applied before the next sequence point.
So for an expression like

a = ++b - --c

don't assume that b will be updated before c, or that either b or c
will be updated before the result is assigned to a.

An online version of the language standard is available at:

http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf
 
J

John Bode

[snip]
As a not very interested bystander, it would seem the test is wrong.  It
strikes me as a very poor question to include in a multiple choice test; the
code should not have been written, as the FAQ makes clear.  It is barely
*conceivable*  that they wanted "Error" as an answer.  In US English that
would  make little sense, but there is English and there is English, as you
well know.

There must be a huge number of applicants in India when  they have to resort
to such a crude means of testing.

Based on the questions and code I've seen here and elsewhere from
students and beginning programmers in India, I've come to the
following conclusions:

1. Indian universities and tech schools are using *very* out of date
materials and tools (Turbo C appears to be the most advanced
compiler available);

2. The reference materials are of uniformly poor quality and teach
the
kind of bad practice and wrong concepts that infested code written
in the US back in the early '90s;

3. These bad practices and wrong concepts have become
institutionalized
in the Indian software industry.

I'm not surprised by seeing an interview question like that at all.
 
K

Keith Thompson

Nick Keighley said:
*that operate on the same variable*

(technically the same "object" (storage location)
[...]

That's not quite the rule. It applies to multiple modifications of the
same object *between sequence points*, not within a statement.

Perhaps a more important point is this: whatever

result = ++i - --i;

was intended to do, there's certainly a much simpler and way to express
that intent, probably one of these:

result = -1;
result = 0;
result = 1;

depending on what you (wrongly) think the original line actually means.

C99 6.5p2 states the actual rule:

Between the previous and next sequence point an object shall
have its stored value modified at most once by the evaluation
of an expression. Furthermore, the prior value shall be read
only to determine the value to be stored.

But if you write clear code, you rarely have to worry about that.
 
W

William Hughes

I attended some technical test(interview), there I faced this
program.
=> For that Program, *20*  was not there as a option. After the test I
executed the program, the result is: 20
Options in that test are:      A ) 19,          B ) 21,        C )
10,       D ) Error

Don't suggest me on the way of writing code. (It's a test program)
Suggest me on the result.

TR,
Sreekanth

Based on the possible answers, I suspect that the original
program was

int result = 0, i = 10;
result = ++i + --i;

(note the subtraction has become an addition). As has
been pointed out this invokes undefined behaviour
so there is no "correct answer". However, if the question were
about the program

int result = 0, i = j = 10;
result = ++i + --j;

the question has a correct answer, A. I suspect
that this is the answer that was considered to be
correct.

- William Hughes
 
K

Kenny McCormack

Keith Thompson said:
Perhaps a more important point is this: whatever

result = ++i - --i;

was intended to do, there's certainly a much simpler and way to express
that intent, probably one of these:

(etc, etc)

Unfrickin' believable. You guys totally miss the point.

I can just imagine that if someone came to you asking for help solving a
jigsaw puzzle, you'd snottily tell them that they shouldn't have cut it
up into little pieces in the first place.

The whole point of questions like this is that they are puzzles. We all
understand that they aren't real world code. Just as we understand that
the best way to have an intact picture is not to have cut it up in the
first place.

I take it you never enjoyed puzzles much. Probably never understood
what the point of it was.

--
"The anti-regulation business ethos is based on the charmingly naive notion
that people will not do unspeakable things for money." - Dana Carpender

Quoted by Paul Ciszek (pciszek at panix dot com). But what I want to know
is why is this diet/low-carb food author doing making pithy political/economic
statements?

Nevertheless, the above quote is dead-on, because, the thing is - business
in one breath tells us they don't need to be regulated (which is to say:
that they can morally self-regulate), then in the next breath tells us that
corporations are amoral entities which have no obligations to anyone except
their officers and shareholders, then in the next breath they tell us they
don't need to be regulated (that they can morally self-regulate) ...
 
A

August Karlstrom

Don't suggest me on the way of writing code. (It's a test program)
Suggest me on the result.

The expression ++i - --i is not well defined (like division by zero in
mathematics). In spite of this you got the result 20 but if you used a
different compiler you might have gotten the result -234087 for instance.

This mess of an expression is accepted by the compiler since C allows
expressions with side effects. C would be a much simpler language if it
didn't have expression operators with side effects. Sequence points
would then be a non-issue.

(Flame suit on...)


/August
 
K

Kenny McCormack

August Karlstrom said:
This mess of an expression is accepted by the compiler since C allows
expressions with side effects. C would be a much simpler language if it
didn't have expression operators with side effects. Sequence points
would then be a non-issue.

My sense is that you are using the word "simpler" as a stand-in for
"better". Which is certainly your right, of course.

But I would argue that more complex languages - that is, languages that
don't easily map to assembler, as C does - are more restrictive in terms
of expressions and side effects.

And, as everyone knows, C's auto-increment and decrement operators come
to us directly from the PDP11 instruction set.

--
(This discussion group is about C, ...)

Wrong. It is only OCCASIONALLY a discussion group
about C; mostly, like most "discussion" groups, it is
off-topic Rorsharch [sic] revelations of the childhood
traumas of the participants...
 
A

August Karlstrom

My sense is that you are using the word "simpler" as a stand-in for
"better". Which is certainly your right, of course.

I mean simpler semantics.
But I would argue that more complex languages - that is, languages that
don't easily map to assembler, as C does - are more restrictive in terms
of expressions and side effects.

That may be the case but I can't see why the mapping to assembler would
be more difficult if some parts of the syntax was to be invalidated. Is

x = ++y;

easier to map to assembler than

y++;
x = y;
And, as everyone knows, C's auto-increment and decrement operators come
to us directly from the PDP11 instruction set.

You can still map an increment/decrement statement to an assembler
instruction even if the increment/decrement feature is not defined as an
expression.


/August
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top