Electronic – XC8’s Microchip compiler long type doesn’t go up 32bits value

xc8

I need a big number in my firmware, So i choosed long for the job.
In the microchip's documents the long type provides 4Bytes for use:
enter image description here

So i created this variable (timeCounter):
enter image description here

In a interrupt of 100us of timer2 this variable is incremented:
enter image description here

Problem:
The variable is unsigned long but when i plot his value the max value achieved is aproximately 32725! and the variable is SIGNED!!!!
image:
enter image description here

The code below shows a sizeof(unsigned long) equals to 4bytes…
enter image description here
enter image description here

I've already disabled the compiler optimizations.
Using PIC18f26k80.

=========================================================

code:

#include "main.h"

void main( void ){

char string[100];
unsigned long timeCounter;

setupHw();
initHw();
initSw();

while( true ){

    clearWDT();   


    /*============================================================================*\
     * Task3 (60s)
     * Processo...
     * DependĂȘncias:                  
    \* ===========================================================================*/
    if( f60s ){
        f60s = false;

        sprintf(string, "TimeCounter (60s): %d\n", timeCounter);
        txStringUart1(string);
       } // end da task2       
    }
}

Best Answer

The problem is not with your type but with the way you print it. In C %d format specifier is for int, not for long. For signed long you should use %ld instead, if your compiler supports it. For unsigned use %lu.