c++qtqstringqdebug

qDebug prints a wrong number


I'm just trying to print a number using qDebug as follows:

qDebug() << QString::number(03001);

But the result is:

"1537"

If I try to print without the first zero:

qDebug() << QString::number(3001);

The result is correct:

"3001"

Why does it happen?

I'm using Qt 5.3.


Solution

  • Leading zero will make the number to be interpreted as an octal literal.

    octal-literal is the digit zero (0) followed by zero or more octal digits (0, 1, 2, 3, 4, 5, 6, 7)

    So this is not by any means related to qDebug, but to the way C++ interprets integer constants.