大家好,又见面了,我是你们的朋友全栈君。
注意
字符串格式化时不要,输入和输出参数不要使用同一个字符数组。
springt函数处理流程分析:
-
输入和输出参数不同sprintf(str2, “ab%s”,str):
-
输入和输出参数相同sprintf(str, “ab%s”,str):
示例
#include <stdio.h>
int main()
{
char str[10] = "0123456789";
char str2[10] = "";
str[0] = 0;
str[1] = 0;
str[2] = 0;
str[3] = 0;
for(int i = 0;i<10;i++){
printf("1-str_c[%d] = %c\n",i,str[i]);
}
printf("1-str = %s\n",str);
//str格式化到str2
sprintf(str2, "ab%s",str);
for(int i = 0;i<10;i++){
printf("2-str2_c[%d] = %c\n",i,str2[i]);
}
printf("2-str2 = %s\n",str2);
for(int i = 0;i<10;i++){
printf("3-str_c[%d] = %c\n",i,str[i]);
}
printf("3-str = %s\n",str);
//str格式化到str(注意:这个地方输出结果)
sprintf(str, "ab%s",str);
for(int i = 0;i<10;i++){
printf("4-str_c[%d] = %c\n",i,str[i]);
}
printf("4-str = %s\n",str);
return(0);
}
下面是打印结果:
1-str_c[0] =
1-str_c[1] =
1-str_c[2] =
1-str_c[3] =
1-str_c[4] = 4
1-str_c[5] = 5
1-str_c[6] = 6
1-str_c[7] = 7
1-str_c[8] = 8
1-str_c[9] = 9
1-str =
2-str2_c[0] = a
2-str2_c[1] = b
2-str2_c[2] =
2-str2_c[3] =
2-str2_c[4] =
2-str2_c[5] =
2-str2_c[6] =
2-str2_c[7] =
2-str2_c[8] =
2-str2_c[9] =
2-str2 = ab
3-str_c[0] =
3-str_c[1] =
3-str_c[2] =
3-str_c[3] =
3-str_c[4] = 4
3-str_c[5] = 5
3-str_c[6] = 6
3-str_c[7] = 7
3-str_c[8] = 8
3-str_c[9] = 9
3-str =
4-str_c[0] = a
4-str_c[1] = b
4-str_c[2] = a
4-str_c[3] = b
4-str_c[4] =
4-str_c[5] = 5
4-str_c[6] = 6
4-str_c[7] = 7
4-str_c[8] = 8
4-str_c[9] = 9
4-str = abab
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/150225.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...