[英]Converting from integer to string in C
I want to convert an integer to a string in C. I have tried the following code but the program is constantly outputting a 9-digit number. 我想在C中将整数转换为字符串。我尝试了以下代码,但是程序不断输出9位数字。 Does someone knows what is the error please and how can I fix it? 有人知道错误是什么吗,我该如何解决?
int num = 158;
char str[5];
sprintf(str, "%d" ,num);
printf("The result is: %d" , sprintf);
Thanks! 谢谢!
Either you print the integer with %d 您可以使用%d打印整数
printf("The result is: %d\n", num);
or the string representation with %s 或%s的字符串表示形式
printf("The result is: %s\n" , str);
By doing 通过做
printf("The result is: %d" , sprintf);
You are printing the decimal representation of the address of the function sprintf. 您正在打印函数sprintf地址的十进制表示形式。 Example: 例:
#include <stdio.h>
int main() {
int num = 158;
char str[5];
sprintf(str, "%d" ,num);
printf("The result is: %d\n", sprintf);
printf("The result is: %8x\n", sprintf);
}
Compile statically in order to make it easier to locate the address of sprintf. 静态编译以便更轻松地找到sprintf的地址。
➜ ~ [4] [Thu 13] $ gcc file.c -o bin -static
In the code, I also print the hexadecimal representation, which is easier to locate in the binary file. 在代码中,我还打印了十六进制表示形式,在二进制文件中更易于查找。 Output: 输出:
The result is: 4200768
The result is: 401940
You can actually check the linear address of sprintf in the ELF executable: 您实际上可以在ELF可执行文件中检查sprintf的线性地址:
➜ ~ [4] [Thu 13] $ nm bin | grep sprintf
0000000000480830 W asprintf
0000000000480830 T __asprintf
0000000000480830 T ___asprintf
0000000000401940 T _IO_sprintf
0000000000480a40 T _IO_vasprintf
00000000004019d0 T __IO_vsprintf
00000000004019d0 T _IO_vsprintf
0000000000401940 T sprintf
0000000000401940 T __sprintf
0000000000480a40 W vasprintf
00000000004019d0 W vsprintf
As expected, 0x0000000000401940. 如预期的那样,为0x0000000000401940。
Change your printf to printf("The result is: %s" , str);
将您的printf更改为printf("The result is: %s" , str);
. 。
%s
is the specifier for strings and your string name is str
. %s
是字符串的说明符,您的字符串名称是str
。 Printing with incorrect % specifiers invoke undefined behavior. 使用不正确的%指定符进行打印会调用未定义的行为。
printf("The result is: %d" , sprintf);
This code attempts to print sprintf
which is a function. 此代码尝试打印sprintf
这是一个功能。 So its address is passed to printf
. 因此,其地址将传递给printf
。 That's just not what you intended to do. 那不是你打算做的。 Not to mention the fact that %d
with an address leads to undefined behavior. 更不用说具有地址的%d
导致未定义行为的事实。
To print the string you made, you do this: 要打印您创建的字符串,请执行以下操作:
printf("The result is: %s", str);
Note that you must use the %s
format string because the argument you supply is a string. 请注意,您必须使用%s
格式字符串,因为您提供的参数是字符串。
If all you want to do is to print the value, then you can remove str
, remove the call to sprintf
, and get printf
to perform the formatting: 如果您只想打印值,则可以删除str
,删除对sprintf
的调用,并获取printf
来执行格式化:
printf("The result is: %d", num);
One advantage of this is that it avoids you having to decide how large a buffer to allocate. 这样的一个优点是,它避免了您必须决定要分配多大的缓冲区。 You allocated a buffer with length 5 which can accept numbers up to 4 digits, or 3 digits if negative. 您分配了一个长度为5的缓冲区,该缓冲区最多可以接受4位数字,如果为负,则可以接受3位数字。 For values with more digits, then your code will overrun that buffer. 对于具有更多数字的值,则您的代码将超出该缓冲区。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.