-1

I am trying to find a way to count digits of an integer (for example for 01 there is 2 digits)I thought turning integer to string then using strlen() would help but it did not. How can I achieve my goal?

int main()
{
    int someInt = 01;
    char str[12];
    sprintf(str, "%d", someInt);
    printf("%d",strlen(str));
}

Jbb
  • 217
  • 1
  • 7
  • 2
    `for example for 01 there is 2 digits` No, its not. `01` is the integer `1` which is 1 digit. `How can I achieve my goal?` You can't. Integers don't store preceding zero's. – tkausl Oct 26 '21 at 00:50
  • 01 is a literal equal to 1, and unless you change formatting it's one character – Swift - Friday Pie Oct 26 '21 at 00:51
  • You don't print `01` to `str`, you print `1`. Check the `printf()` documentation for how to print leading zeroes. – torstenvl Oct 26 '21 at 00:52
  • 1
    Also, as a side note, integer literals with a leading zero are interpreted as octal. https://stackoverflow.com/questions/1661369/how-does-c-handle-integer-literals-with-leading-zeros-and-what-about-atoi That doesn't change anything if your integer is below "8", but you'll get unexpected results if you try to write "19" as "019". – skrrgwasme Oct 26 '21 at 01:05
  • `const char *someInt = "01";`: this is a human-readable string of two characters which represent a number and its lengt is '2'. `int someInt = 01;` in 'c' represents a machine-operatable number `1` and sprintf just converts it into a human-readable string as `1`. The lengty of the resulting string is '1`' – Serge Oct 26 '21 at 01:10
  • " but it did not. " --> Jbb, what was your output? What did you expect? I'd expect the `int` 1 to be 1 digit long. – chux - Reinstate Monica Oct 26 '21 at 02:26
  • Given some integer constant like 123, you can count the number of digits at compile-time with a macro: `#define COUNT_DIGITS(n) (sizeof (#n) - 1)`. This is essentially the same thing as converting to a string and calling strlen, just far more efficient. – Lundin Oct 26 '21 at 06:59

2 Answers2

2

Let's look at your code.

int main()
{
    int someInt = 01;
    char str[12];
    sprintf(str, "%d", someInt);
    printf("%d",strlen(str));
}

As noted in the comments, 01 is an integer literal and you've written... 1. Let's also initialize every character in your string to '\0' to avoid potential null terminator issues, and print a nice newline at the end of the program.

int main()
{
    int someInt = 01;
    char str[12] = {0};
    sprintf(str, "%d", someInt);
    printf("%d\n", strlen(str));
}

It still prints 1, because that's how long the string is, unless we use modifiers on the %d specifier. Let's give that field a width of 2 with %2d.

As suggested, in comments on this answer, the correct format specifier for printing the length of a string is %zu as the result of strlen is of type size_t rather than int.

int main()
{
    int someInt = 01;
    char str[12] = {0};
    sprintf(str, "%2d", someInt);
    printf("%zu\n", strlen(str));
}

Now it prints 2.

If you want to store "01" in str, you could modify it to print leading zeroes to pad the int with %02d.

Chris
  • 26,361
  • 5
  • 21
  • 42
1

When C compiler compiles your program, a lot of information present in your C source code is lost. Forever.

All information about how you ident your code, how you split it into multiple lines and other formatting information is lost. The names of your variables are lost. (To be precise, some of this information goes into a separate file that debuggers use when you step through your program, but that's a different topic).

Compiler produces exact same code for

int someInt = 1;

and

int someInt = 01;

and

int someInt = 001;

By the time your program executes, it can no longer tell whether the original C file was in any of the 3 forms above (or any of the other equivalent forms).

What you could do, is define your number as a string to begin with. Then convert it to an integer if you need it as an integer.

int main()
{
    char[] numericString= "01";
    int someInt;
    someInt = (int)strtol(numericString, NULL, 10);
    printf("The number is %d", someInt);
    printf("Number of digits is %zu", strlen(numericString));
}
George
  • 2,436
  • 4
  • 15
  • 30
  • `printf("%d\n", strlen(str));` uses wrong format. It should be `printf("%zu\n", strlen(str));` – 0___________ Oct 26 '21 at 01:30
  • 2
    `Compiler produces exact same code for ...` - while it is correct in this particular case, generally it is not. Imagine using `10` and `010` values. – qrdl Oct 26 '21 at 05:45
  • @qrdl. If your program contains **different representations** of the same number (like in my example), the compiler will always produce the same code. If your program contains **different numbers** (like 10 and 8 in your example), the compiler will produce different output. – George Oct 26 '21 at 13:39
  • @0___________. I don't have a `printf("%d\n", strlen(str));`. Did you mean to comment on a different answer? – George Oct 26 '21 at 13:42
  • @George don't you use `%d` to printf `size_t`? It is wrong. – 0___________ Oct 26 '21 at 15:50
  • @0___________. You are right. Updated. – George Oct 26 '21 at 16:22