1

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.

KelvinS
  • 2,870
  • 8
  • 34
  • 67
  • 6
    Because a leading zero indicates an octal number - see the [reference on integer literals](http://en.cppreference.com/w/cpp/language/integer_literal) for more information – UnholySheep Jan 20 '17 at 13:39

2 Answers2

5

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.

AMA
  • 4,114
  • 18
  • 32
1

03001 is an octal number in C++.

Silicomancer
  • 8,604
  • 10
  • 63
  • 130