Increment operator

R

Richard Heathfield

Keith Thompson said:
Just out of curiosity, what output does your implementation produce
for the following?

#include <stdio.h>
int main(void)
{
int i = 3;
if (0) {
i = i++;
}
printf("%d\n", i);
return 0;
}

3

Why, what were you expecting? :)
 
K

Keith Thompson

Richard Heathfield said:
Keith Thompson said:

3

Why, what were you expecting? :)

Either 3 or, just possibly, "a suffusion of yellow". The latter would
have been the result of a certain easily made mistake in what I think
you're doing. I'm glad to see you didn't make that particular
mistake.
 
R

Richard Tobin

Kenneth Brody said:
#include <stdio.h>

int main(void)
{
int i;
i = ( 3 || 4 );
printf("3 or 4 is %d\n",i);
exit(0);
}

If you #include <iso646.h> then you can write "i = (3 or 4);"

-- Richard
 
K

Kelsey Bjarnason

[snips]

No you didn't. You got 3 or 4.

Really? Could you please quote the part of the standard that requires the
code above to produce a result of either 3 or 4?
 
R

Richard

Kelsey Bjarnason said:
[snips]

No you didn't. You got 3 or 4.

Really? Could you please quote the part of the standard that requires the
code above to produce a result of either 3 or 4?

Hardly likely to provide an "either or" is it?

Could you provide me any compiler where the output is NOT 3 or 4 please?
 
K

Kenny McCormack

[snips]

No you didn't. You got 3 or 4.

Really? Could you please quote the part of the standard that requires the
code above to produce a result of either 3 or 4?

Really? Could you please quote the biblical verse that requires the
price of tea in China to be between $30 and $50 a ton?

Or:

How do you spell non-sequitor?
 
R

Richard Heathfield

[RJH] Undefined behaviour is not /required/ to be bizarre
behaviour. Incidentally, I ran the program at home and got
the output: "a suffusion of yellow".

[RGR] No you didn't. You got 3 or 4.

[KB] Really? Could you please quote the part of the standard
that requires the code above to produce a result of either
3 or 4?

[RGR] Hardly likely to provide an "either or" is it?

The C Standard does actually require the implementation to choose
between two alternatives on occasion. This is not one of those
occasions.
Could you provide me any compiler where the output is NOT 3 or 4
please?

I have already shown output that is neither 3 nor 4, produced by the
program specified upthread when compiled using a conforming
implementation here at my home. Your inability to believe me is your
problem, not mine.
 
K

Kenny McCormack

Richard Heathfield said:
I have already shown output that is neither 3 nor 4, produced by the
program specified upthread when compiled using a conforming
implementation here at my home. Your inability to believe me is your
problem, not mine.

Yes, Alberto.
 
R

Richard

Richard Heathfield said:
[RJH] Undefined behaviour is not /required/ to be bizarre
behaviour. Incidentally, I ran the program at home and got
the output: "a suffusion of yellow".

[RGR] No you didn't. You got 3 or 4.

[KB] Really? Could you please quote the part of the standard
that requires the code above to produce a result of either
3 or 4?

[RGR] Hardly likely to provide an "either or" is it?

The C Standard does actually require the implementation to choose
between two alternatives on occasion. This is not one of those
occasions.
Could you provide me any compiler where the output is NOT 3 or 4
please?

I have already shown output that is neither 3 nor 4, produced by the
program specified upthread when compiled using a conforming
implementation here at my home. Your inability to believe me is your
problem, not mine.

You do not disclose what your "compile" command did. So you are being
typically supercilious and obstructive or are fibbing for whatever
reason. I suspect the reason is to be high and mighty about what
"undefined behaviour" means.

As I said - if you can show me one compiler that doesn't produce 3 or 4
for that code then I will believe you. As it is, I don't.

And, I daresay, neither does anyone else.
 
K

Keith Thompson

Richard Heathfield said:
[RJH] Undefined behaviour is not /required/ to be bizarre
behaviour. Incidentally, I ran the program at home and got
the output: "a suffusion of yellow".

[RGR] No you didn't. You got 3 or 4.

[KB] Really? Could you please quote the part of the standard
that requires the code above to produce a result of either
3 or 4?

[RGR] Hardly likely to provide an "either or" is it?

The C Standard does actually require the implementation to choose
between two alternatives on occasion. This is not one of those
occasions.
Could you provide me any compiler where the output is NOT 3 or 4
please?

I have already shown output that is neither 3 nor 4, produced by the
program specified upthread when compiled using a conforming
implementation here at my home. Your inability to believe me is your
problem, not mine.

Indeed. I have a pretty good idea about the nature of the conforming
implementation you used; such an implementation would be reasonably
easy to obtain.

Figuring this out merely requires an understanding of what a
"conforming implementation" is.

To those who doubt this: I don't speak for Richard, but I suspect he
would have been willing to explain it to anyone who asked without
first accusing him of being a liar. (I'm curious about the details,
but I'm enjoying the mystery too much to ask.)
 
K

Kenny McCormack

You do not disclose what your "compile" command did. So you are being
typically supercilious and obstructive or are fibbing for whatever
reason. I suspect the reason is to be high and mighty about what
"undefined behaviour" means.

As I said - if you can show me one compiler that doesn't produce 3 or 4
for that code then I will believe you. As it is, I don't.

And, I daresay, neither does anyone else.[/QUOTE]

Surely, Richard, you could write a "compiler" (basically a shell/AWK
script - front end to a real compiler) that would do what Dicky is
claiming his does. It's not that hard.
 
R

Richard

You do not disclose what your "compile" command did. So you are being
typically supercilious and obstructive or are fibbing for whatever
reason. I suspect the reason is to be high and mighty about what
"undefined behaviour" means.

As I said - if you can show me one compiler that doesn't produce 3 or 4
for that code then I will believe you. As it is, I don't.

And, I daresay, neither does anyone else.

Surely, Richard, you could write a "compiler" (basically a shell/AWK
script - front end to a real compiler) that would do what Dicky is
claiming his does. It's not that hard.
[/QUOTE]

I already asked about that.

But is it fully standards compliant?
 
O

Old Wolf

Could you provide me any compiler where the output is NOT 3 or 4 please?

Someone posted almost the same code once and wanted
to know why the output was 7 on their Sun box. You
could try to find it in the archives if you like.
 
R

Robert Gamble

Richard Heathfield said:
[RJH] Undefined behaviour is not /required/ to be bizarre
behaviour. Incidentally, I ran the program at home and got
the output: "a suffusion of yellow".
[RGR] No you didn't. You got 3 or 4.
[KB] Really? Could you please quote the part of the standard
that requires the code above to produce a result of either
3 or 4?
[RGR] Hardly likely to provide an "either or" is it?
The C Standard does actually require the implementation to choose
between two alternatives on occasion. This is not one of those
occasions.
I have already shown output that is neither 3 nor 4, produced by the
program specified upthread when compiled using a conforming
implementation here at my home. Your inability to believe me is your
problem, not mine.

Indeed. I have a pretty good idea about the nature of the conforming
implementation you used; such an implementation would be reasonably
easy to obtain.

Figuring this out merely requires an understanding of what a
"conforming implementation" is.

To those who doubt this: I don't speak for Richard, but I suspect he
would have been willing to explain it to anyone who asked without
first accusing him of being a liar. (I'm curious about the details,
but I'm enjoying the mystery too much to ask.)

I don't know if this is the approach that Richard took but here is my
conforming (I think) implementation which produced similar results:

----
$ cat ub.c
#include <stdio.h>
int main(void)
{
int i = 3;
i = i++;
printf("%d\n", i);
return 0;
}

$ ./compile ub.c
ub.c: In function 'main':
ub.c:5: warning: operation on 'i' may be undefined

$ ./ub
a suffusion of yellow
----
$ cat nub.c
#include <stdio.h>
int main(void)
{
int i = 3;
i++;
printf("%d\n", i);
return 0;
}

$ ./compile nub.c

$ ./nub
4
----
$ cat compile
#!/bin/sh

outname=${1%\.*}

gcc -fsyntax-only -Wsequence-point "$1" 2>&1 | grep 'may be undefined'
/dev/null
if [ $? -eq 0 ]
then
gcc -Wall -ansi -pedantic -W -fsyntax-only "$1"
gcc -ansi -o "$outname" -x c - <<EOF
#include <stdio.h>
int main (void) { puts("a suffusion of yellow"); return 0; }
EOF
else
gcc -Wall -W -ansi -pedantic "$1" -o "$outname"
fi
----

There are obvious improvements that could be made such as passing
multiple arguments from "compile" to gcc but from what I can tell it
isn't any less conforming that gcc itself (unless of course gcc
incorrectly diagnoses possible undefined behavior).

Robert Gamble
 
R

Richard

Robert Gamble said:
Richard Heathfield said:
[RJH] Undefined behaviour is not /required/ to be bizarre
behaviour. Incidentally, I ran the program at home and got
the output: "a suffusion of yellow".
[RGR] No you didn't. You got 3 or 4.
[KB] Really? Could you please quote the part of the standard
that requires the code above to produce a result of either
3 or 4?
[RGR] Hardly likely to provide an "either or" is it?
The C Standard does actually require the implementation to choose
between two alternatives on occasion. This is not one of those
occasions.
Could you provide me any compiler where the output is NOT 3 or 4
please?
I have already shown output that is neither 3 nor 4, produced by the
program specified upthread when compiled using a conforming
implementation here at my home. Your inability to believe me is your
problem, not mine.

Indeed. I have a pretty good idea about the nature of the conforming
implementation you used; such an implementation would be reasonably
easy to obtain.

Figuring this out merely requires an understanding of what a
"conforming implementation" is.

To those who doubt this: I don't speak for Richard, but I suspect he
would have been willing to explain it to anyone who asked without
first accusing him of being a liar. (I'm curious about the details,
but I'm enjoying the mystery too much to ask.)

I don't know if this is the approach that Richard took but here is my
conforming (I think) implementation which produced similar results:

----
$ cat ub.c
#include <stdio.h>
int main(void)
{
int i = 3;
i = i++;
printf("%d\n", i);
return 0;
}

$ ./compile ub.c
ub.c: In function 'main':
ub.c:5: warning: operation on 'i' may be undefined

$ ./ub
a suffusion of yellow
----
$ cat nub.c
#include <stdio.h>
int main(void)
{
int i = 3;
i++;
printf("%d\n", i);
return 0;
}

$ ./compile nub.c

$ ./nub
4
----
$ cat compile
#!/bin/sh

outname=${1%\.*}

gcc -fsyntax-only -Wsequence-point "$1" 2>&1 | grep 'may be undefined'
/dev/null
if [ $? -eq 0 ]
then
gcc -Wall -ansi -pedantic -W -fsyntax-only "$1"
gcc -ansi -o "$outname" -x c - <<EOF
#include <stdio.h>
int main (void) { puts("a suffusion of yellow"); return 0; }
EOF
else
gcc -Wall -W -ansi -pedantic "$1" -o "$outname"
fi
----

There are obvious improvements that could be made such as passing
multiple arguments from "compile" to gcc but from what I can tell it
isn't any less conforming that gcc itself (unless of course gcc
incorrectly diagnoses possible undefined behavior).

Robert Gamble

I really have no idea what the two of your are talking about. None. I'm
sure it's very clever and stuff, but, as I said, the output of the i=i++
program did not produce

"a suffusion of yellow"

The output of a replacement program did that. That program being
selected based on your needs/wants.






--
 
K

Kenny McCormack

I really have no idea what the two of your are talking about. None. I'm
sure it's very clever and stuff,[/QUOTE]

Oh yes. We are positively aglow at their cleverness.
but, as I said, the output of the i=i++
program did not produce

"a suffusion of yellow"

The output of a replacement program did that. That program being
selected based on your needs/wants.

Clearly, from any objective perspective, they are lying (*). That much
is clear. But, as you know, objective reality has no place in this NG.
This is fantasy-land here.

(*) Alberto G.s in training, as I've pointed out many a time.

But, just to take their side for a second, the idea is that an
"implementation" (the Almighty Standard does not mention "compilers")
does not have to be a C compiler in the conventional sense in which you
and I (people who don't spend their lives in fantasy-land) understand
that term. It can be anything and it can produce any output it likes as
long as it does the "right" thing with strictly conforming programs.

So, the following is a perfectly acceptable "C implementation" for the
above program (the one containing: i = i++)

#!/bin/sh
echo 'echo "a suffusion of yellow"' > /tmp/yellow

The above is, of course, not a conforming C implementation, since it
doesn't do the right thing with strictly conforming programs. That
extension is left as an exercise for the reader.
 
R

Richard

I really have no idea what the two of your are talking about. None. I'm
sure it's very clever and stuff,

Oh yes. We are positively aglow at their cleverness.
but, as I said, the output of the i=i++
program did not produce

"a suffusion of yellow"

The output of a replacement program did that. That program being
selected based on your needs/wants.

Clearly, from any objective perspective, they are lying (*). That much
is clear. But, as you know, objective reality has no place in this NG.
This is fantasy-land here.

(*) Alberto G.s in training, as I've pointed out many a time.

But, just to take their side for a second, the idea is that an
"implementation" (the Almighty Standard does not mention "compilers")
does not have to be a C compiler in the conventional sense in which you
and I (people who don't spend their lives in fantasy-land) understand
that term. It can be anything and it can produce any output it likes as
long as it does the "right" thing with strictly conforming programs.

So, the following is a perfectly acceptable "C implementation" for the
above program (the one containing: i = i++)

#!/bin/sh
echo 'echo "a suffusion of yellow"' > /tmp/yellow

The above is, of course, not a conforming C implementation, since it
doesn't do the right thing with strictly conforming programs. That
extension is left as an exercise for the reader.
[/QUOTE]

Oh. I see. Why do they feel the need to be so clever all the time? I
wouldn't like to be on a team with smart asses like that.

You know, the old "Can I go to the toilet" Yes, but where are you going"
type humour.
 
K

Kenny McCormack

Oh. I see. Why do they feel the need to be so clever all the time? I
wouldn't like to be on a team with smart asses like that.[/QUOTE]

My read is that they are mostly unemployed (and unemployable) losers.
That's why they hang out here.

So, you probably have nothing to worry about on that score.
 
K

Kenneth Brody

Richard said:
Surely, Richard, you could write a "compiler" (basically a shell/AWK
script - front end to a real compiler) that would do what Dicky is
claiming his does. It's not that hard.

I already asked about that.

But is it fully standards compliant?[/QUOTE]

Take the source to any compiler. Change the parser such that when
it detects the tokens:

variable '=' variable '++' ';'

act as if

puts("a suffusion of yellow");

were seen instead.

Or, more simply, have whatever reads the source file search for the
specific "[whitespace]i = i++;" sequence and replace it with the
puts() call.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 

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
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top