串联一个int为字符串或c转换 [英] Concatenating an int to a string or converting in C
问题描述
林寻找关于foo-的最后6位随机数,我一直在想,现在的几个小时,但没有成功。唯一的错误信息
注:预期'的char *,但参数的类型为INT
香港专业教育学院试图为int到char转换,但它只是不喜欢它,我的code是低于,
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&time.h中GT;字符* CONCAT(字符* S1,字符* S2)
{
字符*结果=的malloc(strlen的(S1)+ strlen的(S2)+1); // + 1零终止
//在实际code,你会在malloc的检查错误在这里
的strcpy(结果是,S1);
的strcat(结果,S2);
返回结果;
}诠释的main()
{
函数srand(时间(NULL));
INT R = RAND(); 的printf(CONCAT(foo-,R)); 返回0;
}
例如:结果
函数srand(时间(NULL));
INT R = RAND();
字符*海峡=(字符*)malloc的(的sizeof(char)的* 20);
的snprintf(海峡,7,%D,R);如果(strlen的(STR)6;){/ *当r有少于6个数字* /
sprintf的(STR,%06D,R);
}字符* S = CONCAT(foo-,STR);
的printf(%S,S);
免费(STR);
免费(S);返回0;
结果
从 http://joequery.me/$c$c/snprintf-c/:结果
INT的snprintf(的char * str中,为size_t的大小,为const char *格式,...);
搜索结果
STR
是printf的输出将被重定向到缓冲区。尺寸
是将要写入的字节(字符)的最大数量
缓冲区,包括snprintf的终止空字符
为你自动放置。在格式
和可选的...
参数只是字符串格式,如%D,敏如在看到
printf的。
块引用>因此,为了得到转换一个6位数字,你在
指定的snprintf()
的尺寸
参数到7
(您包括空字符)。结果
的sprintf()
函数发送格式化输出到一个字符串的第一个参数指出,(在我们的情况下,这是STR
)。06D%
格式提供给STR
至少6位将被发送。结果
结果结论
要6位数字转换为字符数组。如果数字至少有6位,与
的snprintf()
你会得到被转换号码的前6位。如果有低于6位数,的sprintf()
将在开头添加零,直到有6位数字。所以,如果R = 101;
的结果将是000101
。
结果请注意,以你的
CONCAT
函数的用法:的printf(CONCAT(富 - ,R));
编译器发出警告(在我的情况GCC):
警告:格式不是字符串文字,没有格式参数
[-Wformat安全]
块引用>
的printf()
函数需要知道传递字符串参数的格式。有关于它的一个不错的答案就在这里:<一href=\"http://stackoverflow.com/questions/4419293/warning-format-not-a-string-literal-and-no-format-arguments\">warning:格式不是一个字符串文字,没有格式参数Im looking for a 6 digit random number on the end of foo-, I have been trying for a few hours now with no success. only error messages
note: expected 'char *' but argument is of type 'int'
Ive tried to convert the int to char but it just doesn't like it, My code is below,
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> char* concat(char *s1, char *s2) { char *result = malloc(strlen(s1)+strlen(s2)+1);//+1 for the zero-terminator //in real code you would check for errors in malloc here strcpy(result, s1); strcat(result, s2); return result; } int main () { srand(time(NULL)); int r = rand(); printf(concat("foo-", r)); return 0; }
解决方案for example:
srand(time(NULL)); int r = rand(); char* str = (char*)malloc(sizeof(char)*20); snprintf(str, 7, "%d", r); if (strlen(str) < 6) { /* if r had less than 6 digits */ sprintf(str, "%06d", r); } char* s = concat("foo-", str); printf("%s",s); free(str); free(s); return 0;
from http://joequery.me/code/snprintf-c/ :
int snprintf(char *str, size_t size, const char *format, ...);
str
is the buffer where printf output will be redirected to.size
is the maximum number of bytes(characters) that will be written to the buffer, including the terminating null character that snprintf automatically places for you. Theformat
and the optional...
arguments are just the string formats like "%d", myint as seen in printf.so, in order to get a 6 digit number converted, you specify in
snprintf()
thesize
argument to7
(you include a null character).
sprintf()
function sends formatted output to a string pointed to by the first argument (in our case this isstr
).%06d
format provides that tostr
at least 6 digits will be sent.
CONCLUSION
you want to convert a 6 digit number to a char array. if the number had at least 6 digits, with
snprintf()
you will get the first 6 digits of the number that was converted. if it has less than 6 digits, thesprintf()
will add zeroes at the beginning until there are 6 digits. so, ifr = 101;
the result would be000101
.
note that with your usage of
concat
function:printf(concat("foo-",r));
compiler warns about (gcc in my case):
warning: format not a string literal and no format arguments [-Wformat-security]
printf()
function needs to know the format of the string argument that is passed. there's a nice answer about it right here : warning: format not a string literal and no format arguments这篇关于串联一个int为字符串或c转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!