2

When I run the following code with an input of '01', value has the value of 1, ignoring the 0. However if i input '301' with the 0 not in the first position the code works.

int input;
printf("Enter an number: \n");
scanf(" %d", &input);

char array[10];
int value = sprintf(array, "%d", input);
printf("%d", value);
10
  • It's an int, not a string. Commented Oct 30, 2018 at 16:55
  • 1
    This is to be expected. The scanf() converts both 01 and 1 into the same bit pattern (a bunch of zero bits and a final one bit) — and sprintf() cannot tell how many leading zeros were in the text that was converted (if, indeed, any text was converted). Note that in source code, 0777 and 777 produce very different values; the first is an octal constant, the second a decimal constant. In decimal input with scanf("%d", …);, the leading zeros are ignored; with scanf("%i", …);, the leading zeros matter (it is octal again). Commented Oct 30, 2018 at 16:56
  • 1
    @Ben Is 01 converted to an int a different number then 1? What is the exact value of 01 when converted to an int ? Commented Oct 30, 2018 at 17:00
  • 2
    If you want to read in leading zeros, your best option is to read in the number as a string Commented Oct 30, 2018 at 17:09
  • 2
    Then save the input to a char array, not to int. You are not interested in the integral value of the numbers, but in each value of the position. This is another question. You posted sprintf seems to ignore 0. You want how to use scanf to extract all numbers by their position in string. Commented Oct 30, 2018 at 17:09

3 Answers 3

2

A way to discern leading '0' digits is to record the scan offset of input.

int n1, n2;
int input;
if (scanf(" %n%d%n", &n1, &input, &n2) == 1) {
  char array[40];
  int width = n2 - n1;
  int text_length = sprintf(array, "%0*d", width, input); // "0" --> Pad with zeros
                                                       // "*" --> Min width in argument list
  printf("%d <%s>\n", text_length, array);  
}

" " will consume leading white-space. "%n" will record the scan text offset position. "%n" does not contribute to the scanf() return value.

Limitations: input like "-123" and "+123" will report 4.

Sign up to request clarification or add additional context in comments.

1 Comment

Note width and text_length are expected to be the same in this case unless there is a rare print error.
1

The manual page of sprintf() says

int sprintf(char *str, const char *format, ...);

sprintf(), write to the character string str.

Here

int value = sprintf(array, "%d", input);

sprintf() converts the int input into char array.

For e.g if user entered input as integer 123 it conver that into char array 123. Now it looks like

-------------------------
|  1  |  2  |  3  |  \0  |
-------------------------
array

and sprintf() returns return the number of characters printed (excluding the null byte used to end output to strings). That means

int value = sprintf(array, "%d", input); /* if input = 123(integer) */
printf("%s: ,%d: \n", array,value);/* array: 123(string), value: 3 */

When I run the following code with an input of '01', value has the value of 1, ignoring the 0. ? input is declared as an integer and when user gives 01 then scanf() considers only 1 as leading 0 ignored and only 1 gets stored into array, the array looks like

--------------
|  1  |  \0  |
--------------
array

However if i input '301' with the 0 not in the first position the code works. If user entered 301 then scanf() stored 301 into input and sprintf() converts that int into char array & stores into array as 301 like

     -------------------------
    |  3  |  0  |  1  |  \0  |
    -------------------------
    array

Comments

1

When you read a string as a number using the %d format specifier, any leading zeros are basically lost.

If you want to keep the leading zeros, you need to read the input as a string. That way you get exactly what the user entered. If you additionally want to perform numerical operations, you can then use strtol to get the numerical representation.

char array[10];
scanf("%s", array);

int input = strtol(array, NULL, 10);
printf("input as number %d, input as string: %s\n", input, array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.