Topic: c++ expressions


Author: nafabbio@tiscali.it ("Fabio Napoli")
Date: Mon, 1 Dec 2003 01:11:56 +0000 (UTC)
Raw View
What prints this program?

#include <iostream>

using namespace std;

int main(int argc, char ** argv)
{
    int a = 4;
    int b = --a + --a;

    cout << b << endl;

}

my compiler says  4, is it a standard behaviour?




---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: jackklein@spamcop.net (Jack Klein)
Date: Mon, 1 Dec 2003 06:45:40 +0000 (UTC)
Raw View
On Mon, 1 Dec 2003 01:11:56 +0000 (UTC), nafabbio@tiscali.it ("Fabio
Napoli") wrote in comp.std.c++:

> What prints this program?
>
> #include <iostream>
>
> using namespace std;
>
> int main(int argc, char ** argv)
> {
>     int a = 4;
>     int b = --a + --a;
>
>     cout << b << endl;
>
> }
>
> my compiler says  4, is it a standard behaviour?

There is no standard behavior, your program generates undefined
behavior.  You have modified the value twice without a sequence point
in between.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: francis@robinton.demon.co.uk (Francis Glassborow)
Date: Mon, 1 Dec 2003 09:17:06 +0000 (UTC)
Raw View
In article <bq3fn0$ukn$1@lacerta.tiscalinet.it>, Fabio Napoli
<nafabbio@tiscali.it> writes
>What prints this program?
>
>#include <iostream>
>
>using namespace std;
>
>int main(int argc, char ** argv)
>{
>    int a = 4;
>    int b = --a + --a;
>
>    cout << b << endl;
>
>}
>
>my compiler says  4, is it a standard behaviour?

Yes, but so is outputting 27, 289, 3.14 or absolutely anything else
including sending an email to your employer informing him that you write
programs that exhibit undefined behaviour.


--
Francis Glassborow      ACCU
If you are not using up-to-date virus protection you should not be reading
this. Viruses do not just hurt the infected but the whole community.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]