11.字符,字符常见开发,_itoa函数

时间:2023-03-09 18:08:51
11.字符,字符常见开发,_itoa函数
  • 各种字符所占字节
  1. wchar_t wch = L'我'; //占4个字节
  2. char ch;//占1个字节
  3. printf("%d\n", sizeof("A"));  //占两个字节,因为字符串末尾有'/0'
  4. printf("%d\n", sizeof("我"));  //占三个字节,中文占两个字节
  • sprintf函数
     #define _CRT_SECURE_NO_WARNINGS
    
     #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <Windows.h> void main()
    {
    srand(time());
    char str[] = { };
    int num;
    while ()
    {
    num = rand() % ;
    sprintf(str, "color %d%c", num, 'e');
    Sleep();
    system(str);
    } system("pause");
    }

    //_itoa(100, res, 2); //第一个参数是十进制的数,第二个参数是char型数组,存放结果,最后一个是要转成的进制

  sprintf函数也可以实现字符串加法的功能

  #define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h> void main()
{
char str[] = "task";
char newstr[] = "list123";
char strall[] = { }; sprintf(strall, "%s%.4s", str, newstr);//字符串加法 system(strall);
system("pause");
}