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
scanf()converts both01and1into the same bit pattern (a bunch of zero bits and a final one bit) — andsprintf()cannot tell how many leading zeros were in the text that was converted (if, indeed, any text was converted). Note that in source code,0777and777produce very different values; the first is an octal constant, the second a decimal constant. In decimal input withscanf("%d", …);, the leading zeros are ignored; withscanf("%i", …);, the leading zeros matter (it is octal again).01converted to an int a different number then1? What is theexact valueof01when converted to an int ?sprintf seems to ignore 0. You wanthow to use scanf to extract all numbers by their position in string.