Add leading Zeros to integer doesn't work (arduino, c)

14,218

Solution 1

Integers cannot and do not recognize formatting like leading zeros.

The only way to store this type of information would be to use a string (like a char[] instead of an integer, however it looks like you are simply writing out the value so you shouldn't need a variable for it (as printf() will handle that for you).

// %04d% will pad your number to 4 digits
printf("%04d", number);

If you do actually need to store your value, you can use sprintf() and declare your result as seen below :

int number;
char *result = malloc(5);

and then set it within your setup() function as such :

sprintf(result, "%04d", number);

Using both of these changes, your updated code might look like :

int number;
char *result = malloc(5);

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

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

Consider formatting your values when you are outputting them, otherwise you can use the integer values to perform any arithmetic and calculations with.

Solution 2

What's wrong with the code is that you are printing the return value of the 'printf' call. It returns the number of characters printed. Since it looks like stdio hasn't been setup yet, it returns -1 as an error flag. Read the manual page again, carefully.

On this line:

result = printf("%03d", number);

we want to use 'sprintf' and not 'printf'.

'sprintf' needs to put its output string somewhere. Maybe pre-allocate it, making sure it is larger than your longest output string plus one (to hold the terminating nul character). Do not allocate it inside of 'setup', but put it outside of any routine (since you initialize it in one routine, and use it in another).

char buf[6];

r = sprintf(buf, "%04d", number);

// now print the string inside of 'buf'
Serial.println(buf);
Share:
14,218
manIneedHelp
Author by

manIneedHelp

Hey there im a not so cool guy that is a noob in programming xD

Updated on June 05, 2022

Comments

  • manIneedHelp
    manIneedHelp almost 2 years

    I want to convert, for example, 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 instead of 0012. What is wrong with this code?

  • Rion Williams
    Rion Williams about 8 years
    The question was originally tagged in C#. The basic idea still remains the same though. result would need to be a string as opposed to an integer.
  • manIneedHelp
    manIneedHelp about 8 years
    Ah ok. thanks for that! is there any possibility to do that with an integer?
  • Rion Williams
    Rion Williams about 8 years
    As I mentioned, integers cannot hold any type formatting information, which leading zeroes would qualify as. The real question is do you need a variable to store result at all or are you just outputting it? If that is the case, then you shouldn't need to define result or set it equal to your printf() call.
  • David Ranieri
    David Ranieri about 8 years
    You want malloc(5) for the trailing NUL
  • Rion Williams
    Rion Williams about 8 years
    Thanks Alter, my C is quite rusty and I completely forgot about that. I've updated the code accordingly.