[英]Using sprintf to convert integer to string seems to ignore 0
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. 当我在输入为'01'的情况下运行以下代码时,value的值为1,忽略了0。但是,如果我输入0不在第一个位置的情况下是'301',则该代码有效。
int input;
printf("Enter an number: \n");
scanf(" %d", &input);
char array[10];
int value = sprintf(array, "%d", input);
printf("%d", value);
A way to discern leading '0'
digits is to record the scan offset of input. 识别前导'0'
数字的一种方法是记录输入的扫描偏移量。
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"
将记录扫描文本的偏移位置。 "%n"
does not contribute to the scanf()
return value. "%n"
不影响scanf()
返回值。
Limitations: input like "-123"
and "+123"
will report 4. 限制:输入"-123"
和"+123"
将报告4。
The manual page of sprintf()
says sprintf()
的手册页说
int sprintf(char *str, const char *format, ...);
sprintf()
, write to the character stringstr
.sprintf()
,写入字符串str
。
Here 这里
int value = sprintf(array, "%d", input);
sprintf()
converts the int
input into char array
. sprintf()
将int
输入转换为char array
。
For eg if user entered input
as integer 123
it conver that into char array 123
. 例如,如果用户输入的input
为整数123
它将转换为char数组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). sprintf()
返回返回打印的字符数(不包括用于结束输出到字符串的空字节)。 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. ? 当我使用输入'01'运行以下代码时,value的值为1,忽略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 input
被声明为整数,并且当用户给出01
scanf()
仅将1
视为前导0
被忽略,并且只有1
被存储到array
,该array
看起来像
--------------
| 1 | \0 |
--------------
array
However if i input '301' with the 0 not in the first position the code works. 但是,如果我输入的不是第一个位置的0时输入“ 301”,则代码有效。 If user entered 301
then scanf()
stored 301
into input
and sprintf()
converts that int
into char array & stores into array
as 301
like 如果用户输入301
然后scanf()
存储301
到input
和sprintf()
变换该int
成字符数组&存储到array
为301
像
-------------------------
| 3 | 0 | 1 | \0 |
-------------------------
array
When you read a string as a number using the %d
format specifier, any leading zeros are basically lost. 当您使用%d
格式说明符将字符串读取为数字时,基本上所有前导零都将丢失。
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. 如果还需要执行数字运算,则可以使用strtol
获取数字表示形式。
char array[10];
scanf("%s", array);
int input = strtol(array, NULL, 10);
printf("input as number %d, input as string: %s\n", input, array);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.