0

Ethernet starter kit(PIC32MX9795F512L)
language: C
MPLAB IDE 8.92
Compiler: XC32 v1.3

Hello i want to add leading zeros to my variables. At the end i want to use the in an array. For example: c=10*a+b. When c=5 it should be 05. I cant use any printf function or am I wrong?

Barmar
  • 741,623
  • 53
  • 500
  • 612
M. J.
  • 11
  • 1
    What are you using for "output" that isn't `printf`? Do you have `sprintf`? – Some programmer dude Apr 01 '16 at 19:28
  • It's your environment - why are you asking us? – Martin James Apr 01 '16 at 19:31
  • If you're saying that you don't think `printf()` can format decimal integers with leading zeroes, then you are mistaken. Just use the `0` flag in the relevant conversion specifier. – John Bollinger Apr 01 '16 at 19:32
  • 2
    You can write one leading zero with `printf("0%d", number);` except when `number == 0` you can weed that special case out, unless you *do* want `"00"` output: one leading zero. – Weather Vane Apr 01 '16 at 19:37
  • By the way, [a good `printf` (and family) reference](http://en.cppreference.com/w/c/io/fprintf) might be good to read. – Some programmer dude Apr 01 '16 at 19:38
  • 2
    Possible duplicate of [Printing leading 0's in C?](http://stackoverflow.com/questions/153890/printing-leading-0s-in-c) – Joel Apr 01 '16 at 19:45
  • There are various ways to *print* an integer with leading zeros. `printf` is the most obvious; if there's some reason you can't use `printf`, please explain why. But you can't add leading zeros to a *stored* integer. Integers are stored in binary, not as human-readable characters. – Keith Thompson Apr 01 '16 at 20:10
  • @Joel Almost all the answers there use a form of `printf`, which he says he can't use for some reason. – Barmar Apr 01 '16 at 20:15
  • @Barmar I don't think he said he is not allowed to use `printf`. He said "*I cant use any printf function or am I wrong?"* – Weather Vane Apr 01 '16 at 21:52

2 Answers2

1

You can use printf() to simply print a formatted number to standard output:

int c = 5;
fprintf(stdout, "c [%02d]\n", c);

If you can't use printf(), another option is to store the padded value in a char * or string. You can instead use sprintf() to write the formatted string to a char * buffer.

For example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char *argv[]) {
{
    char* c_str = NULL;
    int c_int = 5;
    int c_str_length = 3; /* two bytes for "0", "5", and one byte for the nul terminator */
    c_str = malloc(c_str_length); 
    if (!c_str) {
        fprintf(stderr, "Error: Could not allocate space for string!\n");
        return EXIT_FAILURE;
    }
    int n = sprintf(c_str, "%02d", c_int);
    if (n != c_str_length) { 
        fprintf(stderr, "Error: Something went wrong in writing the formatted string!\n");
        free(c_str);
        return EXIT_FAILURE;
    }
    fprintf(stdout, "c_str: [%s]\n", c_str);
    free(c_str);
    return EXIT_SUCCESS;
}

If you go this route, you can see how you could do some error checking along the way. You'll need to think about string length (hint: log10()), or use a static char [] array in place of a char * of sufficiently long length.

Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
0

It is quite easy to add a leading zero, provided you take care of negative values too. You said you want to write to an array, so I used sprintf but if you want to output directly, you can use printf in a similar way.

char cstr[24];
int c = 10 * a + b;
if (c > 0) {
    sprintf(cstr, "0%d", c);
} else if (c < 0) {
    sprintf(cstr, "-0%d", -c);
} else {
    //sprintf(cstr, "00");                
    sprintf(cstr, "0");                // depending on your needs
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56