add leading Zeros to integer doesn't work :(

Hey there,

im very frustrating on this :(( I just want to convert for exampe the number 12 to 0012. So that the number is always 4 characters long. I tried it with this code:

int number;
int result;

void setup() {
number = 12;
Serial.begin(9600);
result = printf("%03d", number);
}

void loop() {
Serial.println(result);
}

However this code outputs only -1 istead of 0012. What is wrong with this code? I tried to solve this since a whole 1 hour :,(

Please help me!

Thank you!

Where does the output from printf() go to ?

Try

int number;

char result[10];

void setup()
{
  number = 12;
  Serial.begin(9600);
  sprintf(result, "%04d", number);
}

void loop()
{
  Serial.println(result);
}