Issue while trying to print long values and ulong values

 

Hey all, 

I'm experimenting with accessing the deal history, however, I can't seem to get the correct format specifier to properly display long values.

Here's my code:

   HistorySelect(0, TimeCurrent());
   
   int deals = HistoryDealsTotal();
   
   ulong ticket;
   long pos_id; 
   
   for (int i = 0; i < deals; i++)
   {
      ticket = HistoryDealGetTicket(i);
      pos_id = HistoryDealGetInteger(ticket, DEAL_POSITION_ID);
      
      string message = StringFormat("Index: %i, Deal ticket: %d, Position ID: %d", i, ticket, pos_id);
      Print(message);
      
      // Index: 1, Deal ticket: -1221050699, Position ID: -1220637801

      Print(ticket, " ", pos_id);  
      
      // 50318556853 50318969751
    
   }

I tried both StringFormat() and PrintFormat(), but both seem to be experiencing integer overflow instead of printing the correct value.

UPDATE:

It seems that using `%I64d`instead of `%d` works, I'm guessing it's indicating the data used will be 64 bits, however, the HistoryDealGetTicket() documentation uses `%d` and for some reason seems to work just fine... Any help to understand this is highly appreciated.

 
Fernando Jose Velasco Borea: Hey all,  I'm experimenting with accessing the deal history, however, I can't seem to get the correct format specifier to properly display long values. Here's my code:

I tried both StringFormat() and PrintFormat(), but both seem to be experiencing integer overflow instead of printing the correct value. UPDATE: It seems that using `%I64d`instead of `%d` works, I'm guessing it's indicating the data used will be 64 bits, however, the HistoryDealGetTicket() documentation uses `%d` and for some reason seems to work just fine... Any help to understand this is highly appreciated.

MQL5 PrintFormat

h | l | ll | I32 | I64

Specification of data sizes, passed as a parameter.

Parameter Type

Used Prefix

Joint Specifier of Type

long

ll (two lower case L)

d, i, o, x, or X

long

I64

d, i, o, x, or X

ulong

I64

o, u, x, or X

type

Type specifier is the only obligatory field for formatted output.

Symbol

Type

Output Format

d

int

Signed decimal integer

i

int

Signed decimal integer

u

int

Unsigned decimal integer

 
Fernando Carreiro #:

MQL5 PrintFormat

h | l | ll | I32 | I64

Specification of data sizes, passed as a parameter.

Parameter Type

Used Prefix

Joint Specifier of Type

long

ll (two lower case L)

d, i, o, x, or X

long

I64

d, i, o, x, or X

ulong

I64

o, u, x, or X

type

Type specifier is the only obligatory field for formatted output.

Symbol

Type

Output Format

d

int

Signed decimal integer

i

int

Signed decimal integer

u

int

Unsigned decimal integer

Thanks! 

So in a nutshell, I can use "%i64d" or "%u" to display the information. The part I still don't understand is the example in the documentation, as it uses the format specifier "%d" and it seems to work fine, but when I try to use it, it overflows the integer value, or at least I think that's what's happening.

 
Fernando Jose Velasco Borea #: So in a nutshell,

So in a nutshell, use the proper specifier for the variable's type: %d for int, %u for uint, %I64d for long, and %I64u for ulong.