Anarelle - 8 days ago 8

C++ Question

I have a singly linked list of type Node that is defined like this:

`struct Item {`

long date;

bool isPaid;

};

struct Node{

Item item;

Node* next;

};

Where "date" is a field that will take the form "ddmmaaaa" (that is, first two digits for the day, second two digits for the month, last four digits for the year).

However, when I try to add a few nodes, I get a different number inserted:

`newItem=new Node;`

newItem->item.date=01062016;

newItem->item.isPaid=true;

newItem->next=nullptr;

list=insertItem(list, newItem);

(insertItem is my function that takes a pointer to a list head and a pointer to a new node and inserts the node in the list).

I get the right number if I try to assign something like 13042016 to the long field:

However, when I try to assign 01062016, I get 287758 instead:

I'm not trying to use a different approach (I know the date thing is weird, I just need it this way). Only to understand why I get strange numbers when I add a date that perfectly fits in a long variable.

I'm using Qt Creator 2.7.2 based on Qt 5.1.0.

Answer

Because `01062016`

starts with `0`

but not `0x`

hence it is an octal number, it's equivalence with `287758`

in decimal.

See Integral Literal:

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