sprintf for Arduino not working?

I have declared 3 unsigned long constants, whereas the third is the subtraction of the first two as you can in the following 1 part.

//1
unsigned long CONST_A = 30000;
unsigned long CONST_B = 2000;
unsigned long CONST_C = CONST_A-CONST_B;

// 2
Serial.print("CONST_A ");Serial.println (CONST_A);
Serial.print("CONST_B ");Serial.println(CONST_B);
Serial.print("CONST_C ");Serial.println(CONST_C);

// 3
char debugText[30];
sprintf(debugText, "%d - %d = %d", CONST_A, CONST_B, CONST_C);
Serial.println(debugText);

The values in the 2. section prints everything properly. 30000, 2000, and 28000 as the result of the subtraction above.

But the debugText in the 3. section sprintf prints out :
30000 - 0 = 2000
the correct output should be
30000 - 2000 = 28000

Can anybody imagine where myerror is?

Thnx in advance

Kind regards Lespaul

%d is not what you should use for an unsigned long...

if you were to compile with warnings enabled, you should see something like

[color=red] warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=][/color]

just listen to the compiler...

  sprintf(debugText, "%lu - %lu = %lu", CONST_A, CONST_B, CONST_C);

J-M-L:
%d is not what you should use for an unsigned long...

  sprintf(debugText, "%lu - %lu = %lu", CONST_A, CONST_B, CONST_C);

Absolutely correct. Thank you JML

you're welcome. have fun (and turn on warnings in the IDE's preference window if not done yet :)) )

J-M-L:
(and turn on warnings in the IDE's preference window if not done yet :)) )

so you also can get bothered by all the mistakes in libraries like EEPROM.h and SD.h :smiley:

sterretje:
so you also can get bothered by all the mistakes in libraries like EEPROM.h and SD.h :smiley:

or fix them too... :slight_smile: