*[注]:本文对itoa(),ultoa()两种函数不做多加介绍,因与ANSI标准不兼容
同时本文只粗略的介绍两种函数的参数概念。
C语言提供两种函数将整数格式化转换为字符串
int sprintf(char *str, const char *format, ...); int snprintf(char *str, size_t size, const char *format, ...);sprintf
sprintf函数提供多个参数,除了前两个参数类型固定外,后面可以接任意多个参数。前两个参数分别是
字符串/字符数组
类型
int a=1111;
char str[10];
sprintf(str,"%d",a); // 把整数a转换为字符串,存放在str中
printf("str=%sn",str); // 输出str=1111
snprintf
函数说明:最多从源串中拷贝 n-1 个字符到目标串中,然后再在后面加一个 ‘ ’。所以如果目标串的大小为 n 的话,将不会溢出。
snprint函数同样提供多个参数,分别是
字符串、字符数组
需要保留的n-1个字符
类型
int a=1234567890;
char str[20];
snprintf(str,5,"%d",a); // 把整数a转换为字符串,存放在str中
printf("str=%sn",str); // 输出str=1234
暑期编程PK赛
得CSDN机械键盘等精美礼品!


