I know this thread is "a little bit" old, but i would like if you can help me.

Im using sprintf(). This is my code:

int countDigits(int inteiro,int max){

  char *string;

  string = (char *) calloc(max+1,sizeof(char));
  sprintf(string,"%d",inteiro);

  return strlen(string);
}

However if the first digit of "inteiro" is a 0, this function doesnt count it. I wonder why?

(sorry about my eventual bad english)

Salem commented: But you decided to bump it anyway :rolleyes: -2

Dani AI

Generated

Short answer: integers do not carry "leading zeros" — printing an int with the plain %d conversion produces the minimal necessary digits, so any zeros that were part of the original textual representation are already gone. As suggested, the printf family can add zero padding when formatting (the correct specifier is %05d, not %05D), but that only affects output, not the stored numeric value.

There are a few problems in the original function that make it fragile: it allocates a buffer but never frees it (memory leak), it uses sprintf without bounds checking (buffer overflow risk), and it assumes max is large enough for the textual form of the integer. Two robust approaches are common — count digits arithmetically, or use snprintf safely.

A safe numeric-only way (fast and does not allocate) — this counts decimal digits and optionally the minus sign:

int countDigitsNumeric(int n) {
    if (n == 0) return 1;
    unsigned int u = (n < 0) ? -(unsigned int)n : (unsigned int)n;
    int digits = 0;
    while (u) { u /= 10; ++digits; }
    return digits + (n < 0 ? 1 : 0); /* include sign if desired */
}

If you prefer the formatted-string approach, use snprintf into a fixed buffer and use its return value (it yields the number of characters that would have been written):

int countDigitsWithSnprintf(int n) {
    char buf[32];
    int len = snprintf(buf, sizeof buf, "%d", n);
    return (len < 0) ? 0 : len;
}

Notes: if preserving leading zeros matters (postal codes, product IDs), keep the value as a string — converting to int loses those zeros. Also beware integer literals that start with 0 are octal in C (e.g. 0123). For safety prefer snprintf or the numeric method and always check buffer sizes or free any heap memory you allocate.

I know this thread is "a little bit" old, but i would like if you can help me.

Im using sprintf(). This is my code:

int countDigits(int inteiro,int max){

  char *string;

  string = (char *) calloc(max+1,sizeof(char));
  sprintf(string,"%d",inteiro);

  return strlen(string);
}

However if the first digit of "inteiro" is a 0, this function doesnt count it. I wonder why?

(sorry about my eventual bad english)

sprintf() returns the length of the string it creates. So you use an int variable to capture that and then return it. However that is merely an efficiency.

The printf/sprintf/sscanf functions use format specifers that indicate the how the data should be displayed. In your case you a simple value of integer which will be the word size of the machine. At the same time since there is no "precision" the string will have zeroes supressed. You can definitely tell the printf() family of functions on how wide the string should be, and weather or not you want leading zeroes. For example, %5d would get you a five digit wide string that is zero supressed whereas %05D gives you the zeroes. You can also justify but beyond this, I highly recommend a good C text like K&R. These days you can probably download it as PDF.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.