各种字符所占字节
wchar_t wch = L'我'; //占4个字节char ch;//占1个字节printf("%d\n", sizeof("A")); //占两个字节,因为字符串末尾有'/0'printf("%d\n", sizeof("我")); //占三个字节,中文占两个字节
sprintf函数
1 #define _CRT_SECURE_NO_WARNINGS
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <time.h>
6 #include <Windows.h>
7
8
9 void main()
10 {
11 srand(time(
0));
12 char str[
100] = {
0 };
13 int num;
14 while (
1)
15 {
16 num = rand() %
10;
17 sprintf(str,
"color %d%c", num,
'e');
18 Sleep(
1000);
19 system(str);
20 }
21
22
23 system(
"pause");
24 }
View Code
//_itoa(100, res, 2); //第一个参数是十进制的数,第二个参数是char型数组,存放结果,最后一个是要转成的进制
sprintf函数也可以实现字符串加法的功能
1 #define _CRT_SECURE_NO_WARNINGS
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 void main()
6 {
7 char str[
10] =
"task";
8 char newstr[
10] =
"list123";
9 char strall[
100] = {
0 };
10
11 sprintf(strall,
"%s%.4s", str, newstr);
//字符串加法
12
13 system(strall);
14 system(
"pause");
15 }
View Code
转载于:https://www.cnblogs.com/xiaochi/p/8135167.html