Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Pre-incrementation -> strange thing
Forum Updated to NodeBB v4.3 + New Features

Pre-incrementation -> strange thing

Scheduled Pinned Locked Moved Solved C++ Gurus
3 Posts 2 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • feniks_fireF Offline
    feniks_fireF Offline
    feniks_fire
    wrote on last edited by feniks_fire
    #1

    Hey,
    I have this code

    #include <QCoreApplication>
    #include <QDebug>
    int main(int argc, char *argv[])
    {
    	QCoreApplication a(argc, argv);
    	int b = 1;
    	int c = ++b + ++b;
    	qDebug()<<c;
    	b = 1;
    	c = ++b + ++b + ++b;
    	qDebug()<<c;
    	b = 1;
    	c = ++b + ++b + ++b + ++b;
    	qDebug()<<c;
    	b = 1;
    	c = ++b + ++b;
    	c += ++b + ++b;
    	qDebug()<<c;
    	return a.exec();
    }
    

    and I get this value

    6
    10
    15
    16
    

    I cannot imagine why is such and no other result.
    Someone can explain me?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Take a look at the here and its associated note about order of evaluation.

      AFAIK, you're in the undefined behaviour territory.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • feniks_fireF Offline
        feniks_fireF Offline
        feniks_fire
        wrote on last edited by
        #3

        Thanks, for reply.
        I don't expect that this can be UB, even in my dreams :|.

        Anyway topic solved.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved