How to format an integer as 3 digit string? If I give 3 formated string should be 003
printf("%03d\n", 3); // output: 003
This doesn't work in Arduino.
How to format an integer as 3 digit string? If I give 3 formated string should be 003
printf("%03d\n", 3); // output: 003
This doesn't work in Arduino.
The standard C function printf print to stdout. Arduino doesn't has a stdout. You can print to Serial, but must prepare/format the string first.
Instead of printf, use snprintf. It prints to a char array, which you later can use with Serial.print, or whatever other function that take a string.
void setup() {
Serial.begin(9600);
while(!Serial);
char buffer[4];
snprintf(buffer,sizeof(buffer), "%03d", 3);
Serial.println(buffer);
}
void loop() {
}
Note: all the comments apply to this answers.