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

K

Kenny McCormack

I mean simpler semantics.

I understand, and I don't disagree with anything you say.

But, as you see, simpler semantics often means more complicated
implementation. Funny, that.

--
"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) ...
 
G

Geoff

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.

So your answer is to redefine the question because you can't cope with
the problem and answers as given? You are no longer answering the
question but questioning the question.

As others have pointed out, it is undefined, therefore the proper
answer is: error.

UB means you cannot predict what the result will be because the
standard does not define what implementations are required to do. It
is open to interpretation by the implementation, therefore you cannot
depend on the result from one implementation to another or one version
of an implementation and another.

The implementation in my head says the result should be 11 - 10 in the
original case and 11 + 10 if your redefinition is correct. The
implementation in my head is also inserting a sequence point where
there isn't one under the standard.

Running it in a debugger in VC2010 the answers are 0 and 20, and it is
repeatable for the implementation. Other implementations are free to
compute something entirely different. Again, the results aren't in the
answer set.

For the original, stated problem the Microsoft compiler produces the
disassembly code:

mov eax,dword ptr
add eax,1
mov dword ptr ,eax
mov ecx,dword ptr
sub ecx,1
mov dword ptr ,ecx
mov edx,dword ptr
sub edx,dword ptr
mov dword ptr [result],edx
mov esi,esp
mov eax,dword ptr [result]
push eax
push offset string " Result: %d " (0F9573Ch)
call dword ptr [__imp__printf (0F982B0h)]

and optimizes it as:

push 0
push offset string " Result: %d " (0F420F4h)
call dword ptr [__imp__printf (0F420A0h)]


The puzzle seems to be intended to be a precedence problem where the
tester is seeking to know if the student understands pre-increment and
pre-decrement but the code is erroneous on its face when interpreted
against the current standard.

Why are you redefining the problem? Is it because you can't cope with
the problem statement as written and none of the given answers is
correct under your interpretation of the problem?

When faced with a multiple choice question where one of the answers is
"none of the above" if you need to guess you are probably going to be
correct more often if you choose that one.
 
S

Seebs

Hi,
Please clarify my doubt...

Hi! The usage of "doubt" here is not idiomatic in English, and will
confuse most readers who aren't from India. In non-Indian English,
the word "doubt" applies when someone has told you something but you
do not believe them. It does not apply to other kinds of confusion.
void main( void )

This is wrong.
result = ++i - --i;

This is undefined.
WHY ?????? HOW ????? Please clarify this ........

This is a FAQ. Go read the FAQ. Long story short, the language specification
says that any time you do something like this, *YOUR CODE IS WRONG*. Any
response at all, from refusing to compile the code to producing surprising
values, is acceptable.

-s
 
S

Seebs

I attended some technical test(interview), there I faced this
program.

Then I sincerely hope, for your sake, that you "failed", because that
is a sucky job.
Don't suggest me on the way of writing code. (It's a test program)

It's an invalid test.
Suggest me on the result.

I would personally be happiest with a compiler which refused to compile
it.

If the test did not have an option for "the program is invalid", then the
person who wrote the test did not know C. Do you really want to be employed
by someone who uses a test that bad?

-s
 
J

John Bode

[snip]
The choice "D) Error" is the "least wrong" of the available choices.)

That was my statement as well, but now that I think about it, given
the nature of undefined behavior, the right answer is actually "all of
the above."
 
S

Seebs

Why are you redefining the problem? Is it because you can't cope with
the problem statement as written and none of the given answers is
correct under your interpretation of the problem?

I suspect it's because usually multiple-choice tests give several answers
which are plausible misunderstandings. Since the 19-21 range would be
plausible misunderstandings for a +, but not for a -, that suggests that
the actual interview question was a +.

In short, it's not that "Error" wasn't the most-correct answer (although
perhaps not if they mean by that "the compiler will catch the error and
warn you"), but rather, that the incorrect answers were so unreasonable that
you wouldn't expect anyone to fall for them.

-s
 
A

August Karlstrom

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 ........

My best advice is to compile the program with all warnings enabled. GCC
will issue the following warnings (with the program stored in test.c):

$ gcc -Wall test.c
test.c:1: warning: return type of ‘main’ is not ‘int’
test.c: In function ‘main’:
test.c:5: warning: operation on ‘i’ may be undefined
test.c:7: warning: implicit declaration of function ‘printf’
test.c:7: warning: incompatible implicit declaration of built-in
function ‘printf’

Good luck with your C programming.


/August
 
S

Seebs

Kenny, of course, is delusional. As always.

A puzzle is a question *which has a meaningful answer*. The entire
point of the question is to be answerable but interesting to think about.
What's wrong with it? If you read again the OP's first post you can't
figure out that it is only a puzzle or a test. He talked about a test
in a follow-up below.

What we don't know yet is whether the tester intended "error" as the
answer. If they didn't, then the tester was an idiot.

-s
 
K

Kenny McCormack

Vincenzo Mercuri said:
I think the OP needed a clarification, so they gave him one.
What's wrong with it? If you read again the OP's first post you can't
figure out that it is only a puzzle or a test. He talked about a test
in a follow-up below.

There's this thing called "common sense"...
 
K

Kenny McCormack

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

Totally incorrect as far as I am concerned. To me, programming _is_ solving
a puzzle.[/QUOTE]

Quite seriously, I believe you. I really do.

But I stand by what I said about Kiki.
There really is a difference between you and Kiki.

--
But the Bush apologists hope that you won't remember all that. And they
also have a theory, which I've been hearing more and more - namely,
that President Obama, though not yet in office or even elected, caused the
2008 slump. You see, people were worried in advance about his future
policies, and that's what caused the economy to tank. Seriously.

(Paul Krugman - Addicted to Bush)
 
W

William Hughes

On 9/21/2010 12:45 PM, William Hughes wrote:
[...]


=>  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
[...]
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.

In what way does 11+9 result in 19?

It is the result you get when you make
a mistake. Mea culpa

[If my error was anything more
that just a silly slip it is because

result = i++ + --j;


would make for a better question.]

- William Hughes
 
M

Morris Keesan

On Tue, 21 Sep 2010 04:22:47 -0400, Nick Keighley

....
main returns an int so you need something like

return 0;

Nope. As long as we're nitpicking, C99 5.1.2.2.3 says,
"... reaching the } that terminates the main function returns a value of
0."
 
L

luserXtrog

On 9/21/2010 12:45 PM, William Hughes wrote:
[...]
=>  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 [...]
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.
In what way does 11+9 result in 19?

It is the result you get when you make
a mistake. Mea culpa

[If my error was anything more
that just a silly slip it is because

 result = i++ + --j;

would make for a better question.]

               - William Hughes

I'd make that "i+++ ++j", or even "+i++- -j--".
Make 'em cry! ;-/
 
L

luserXtrog

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

It would certainly avoid the =/== pedogogical challenge.
Why do you feel the need to assign things? Aren't the
numbers happy enough where they are?
 
K

Keith Thompson

Morris Keesan said:
On Tue, 21 Sep 2010 04:22:47 -0400, Nick Keighley

...

Nope. As long as we're nitpicking, C99 5.1.2.2.3 says,
"... reaching the } that terminates the main function returns a value of
0."

Right, you don't *need* the "return 0;" (as long as you're sure your
code will never be compiled with a compiler that doesn't implement
that C99 feature), but adding it doesn't hurt and can avoid problems
if you use an older compiler.
 
N

Nick Keighley

*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.

I was trying to explain in plain english rather than standardese.
Hence "variable" rather than "object". If he walks away from this
exchange with the idea that he can't do multiple ++s in a statement he
might not have quite the right rule but it'll keep him out of trouble,
perhaps more effectivly.

Sometimes you have to lie a little to achieve a simple explanation.

<snip>
 
N

Nick Keighley

(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.

seems a dumb way to learn to program. And how doyou know this is how
he acquired that daft piece of code? His teacher may think he's
setting puzzles I think he's just teaching them to program badly. Or
just teaching them badly.

I think you miss the point (or pretend to). It's Undefined Behaviour
therefore no analysis of the code has any meaning.
 We all understand that they aren't real world code.

are you Sreekanth understands that?

 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.

I've never thought of it as a useful way to program. Debugging is
perhaps puzzle solving.
 
N

Nick Keighley

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

not necessarily. It seems a common variant of english as spoken in
India. Non native speakers of english often like it you "corrct" their
varients (or at least point out they are speaking a varient).
In standard English(*), it is "behavior" that is undefined.

I usually pick the english spelling varient (where my spelling isn't
so eccentric as to be classed as a language varient of its own!)

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

ah, a varient on "a language is a dialect with an army and a navy"
 
N

Nick Keighley

On Tue, 21 Sep 2010 04:22:47 -0400, Nick Keighley  


...



Nope.  As long as we're nitpicking, C99 5.1.2.2.3 says,
"... reaching the } that terminates the main function returns a value of  
0."

a mistake in C99 in my humble opinion
 
G

Geoff

not necessarily. It seems a common variant of english as spoken in
India. Non native speakers of english often like it you "corrct" their
varients (or at least point out they are speaking a varient).


I usually pick the english spelling varient (where my spelling isn't
so eccentric as to be classed as a language varient of its own!)



ah, a varient on "a language is a dialect with an army and a navy"

And the word is variant, not varient.
 

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,954
Messages
2,570,114
Members
46,702
Latest member
VernitaGow

Latest Threads

Top