赞
踩
int sprintf(char *string, char *format [,argument,...]);
int sprintf_s(char *buffer,size_t sizeOfBuffer,const char *format, [argument] ... );
int _sprintf_s_l(char *buffer,size_t sizeOfBuffer,const char *format,locale_t locale ,[argument] ... );
int swprintf_s(wchar_t *buffer,size_t sizeOfBuffer,const wchar_t *format ,[argument]...);
int _swprintf_s_l(wchar_t *buffer,size_t sizeOfBuffer,const wchar_t *format,locale_t locale ,[argument]…);
template <size_t size>
int sprintf_s(char (&buffer)[size],const char *format, [argument] ... ); // 仅存在于C++
template <size_t size>
int swprintf_s(wchar_t (&buffer)[size],const wchar_t *format ,[argument]...); // 仅存在于C++
char buf[1024] = { 0 };
sprintf(buf, "Hello %s", "world");
printf("buf:%s\n", buf);
memset(buf, 0, 1024);
sprintf(buf, "今年是%d年", 2021);
printf("buf:%s\n", buf);
// 执行结果
buf:Hello World
buf:今年是2021年
char buf[1024] = { 0 };
memset(buf, 0, 1024);
char str1[] = "Hello";
char str2[] = "world!";
int len = sprintf(buf,"%s %s", str1, str2);
printf("buf:%s len:%d\n", buf, len);
// 执行结果
buf:Hello world! len:12
char buf[1024] = { 0 }; memset(buf, 0, 1024); int num = 100; sprintf(buf, "%d", num); printf("buf:%s\n", buf); // 设置宽度 右对齐 memset(buf, 0, 1024); sprintf(buf, "%8d", num); printf("buf:%s\n", buf); // 设置宽度 左对齐 memset(buf, 0, 1024); sprintf(buf, "%-8d", num); printf("buf:%s\n", buf); // 执行结果 buf:100 buf: 100 buf:100
char buf[1024] = { 0 };
memset(buf, 0, 1024);
sprintf(buf, "0x%x", num);
printf("buf:%s\n", buf);
// 执行结果
buf:0x64
char buf[1024] = { 0 };
memset(buf, 0, 1024);
sprintf(buf, "0%o", num);
printf("buf:%s\n", buf);
// 执行结果
buf:0144
char *strcpy(char *dest, const char *src)
char src[40];
char dest[100];
memset(dest, '\0', sizeof(dest));
strcpy(src, "hello world!");
strcpy(dest, src);
printf("最终的目标字符串:%s\n", dest);
// 执行结果
最终的目标字符串:hello world!
void *memcpy(void *destin, void *source, unsigned n)
char src[] = "******";
char dest[] = "ABCDEFGHIJK";
printf("destinationbefore memcpy: %s\n", dest);
memcpy(dest, src, strlen(src));
printf("destinationafter memcpy: %s\n", dest);
// 执行结果
destinationbefore memcpy: ABCDEFGHIJK
destinationafter memcpy: ******GHIJK
char *s = "Golden Global View";
char d[20];
// 从第14个字符(V)开始复制,连续复制4个字符(View)
memcpy(d,s+14,4);
d[4] = '\0';
printf("%s",d);
// 执行结果
View
char *s = "Golden Global View";
char d[20];
memcpy(d,s,strlen(s));
// 因为从d[0]开始复制,总长度为strlen(s),d[strlen(s)]置为结束符
d[strlen(s)] = '\0';
printf("%s",d);
// 执行结果
Golden Global View
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。