当前位置:网站首页>C language printf family
C language printf family
2022-06-29 10:39:00 【Summer insects can't talk with ice】
Indefinite parameter series printf
printf
This is the most commonly used , The function is to print the formatted string on the screen
int printf (const char *__format, ...)
The first parameter is the string , The second parameter is …, Is an indefinite parameter , The return value is the formatted string length , among \n\t This escape character is a , Chinese is two lengths
#include <stdio.h>
int main(void)
{
char* a=" Hello ";
int num;
num = printf(a); // If it is a string, you can directly put the header pointer on the first parameter
printf(" Last one printf The length is :%d\t",num);
return 0;
}
sprintf
This is used to format the string , The formatted string is returned to the first pointer
int sprintf (char *__stream, const char *__format, ...)
The first parameter is the pointer that returns the string , The second parameter is formatted , The third is an indefinite parameter for formatting , The return value is the length of the string
#include <stdio.h>
int main(void)
{
char a[20] = {
0}; // Give too much space to hold strings
int num = 10;
num = sprintf(a, "num Size : %d", num); // If it is a string, you can directly put the header pointer on the first parameter
printf(" Last one printf The length is :%d\n The formatted string is :%s",num,a);
return 0;
}
fprintf
This is used to format the string output to a file
int fprintf (FILE *__stream, const char *__format, ...)
The first parameter is the pointer to the return file , The second parameter is formatted , The third is an indefinite parameter for formatting , The return value is the length of the formatted string
#include <stdio.h>
int main()
{
FILE * fp;
fp = fopen ("file.txt", "w+");
fprintf(fp, " It's today %d", 20200510);
fclose(fp);
return(0);
}
snprintf
This is also used to format strings , contrast sprintf Just one more length parameter , It's safer to use
int snprintf (char *__buf, size_t __len, const char *__format, ...)
The first parameter is the pointer that returns the string , The second is the length , The third parameter is formatted , The fourth is an indefinite parameter for formatting , The return value is the length of the string
#include <stdio.h>
int main()
{
char name1[6] = {
'\0'};
char name2[6] = {
'\0'};
int size1 = sprintf(name1, "%s", "Arthur");
printf("name1:%s, size1:%d\n", name1, size1); // result :name1:Arthur, size1:6
int size2 = snprintf(name2, sizeof(name2)/sizeof(char), "%s", "Arthur");
printf("name2:%s, size2:%d\n", name2, size2); // result :name2:Arthu, size2:6
return 0;
}
Why is it safe , Here we will say , In theory, many college students or college teachers tend to have a misunderstanding when teaching . Many people think that setting a character array to store “Arthur” It should be set to num[6], In fact, this is a wrong idea . As we all know, there is usually a at the end of a string ‘\0’ As the end , When you set it up 6 An array store actually holds seven , So after the continuous memory, the memory has been modified , If your position happens to be a parameter somewhere else , Then the whole program will lead to uncontrollable results at one time . So when we want to save a string, we must prepare an array that is one larger than the length of the string to store it , Will not lead to cross-border .
The above example illustrates why snprintf Than sprintf Security .
va_list series printf
vprintf
This series of functions are generally parameters for functions with indefinite parameters , Maybe it's not very practical, but it can be used for all purposes printf It is necessary to take it out and say
int vprintf(const char *format, va_list arg)
This series of functions should be used with #include <stdarg.h> This library file , This library file defines va_list Structure ,
There are also a series of macro definition functions
#include <stdio.h>
#include <stdarg.h>
void WriteFrmtd(char *format, ...)
{
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}
int main ()
{
WriteFrmtd("%d variable argument\n", 1);
WriteFrmtd("%d variable %s\n", 2, "arguments");
return(0);
}
vsprintf
and sprintf By comparison, it is also the difference of the last parameter
int vsprintf(char *str, const char *format, va_list arg)
#include <stdio.h>
#include <stdarg.h>
char buffer[80];
int vspfunc(char *format, ...)
{
va_list aptr;
int ret;
va_start(aptr, format);
ret = vsprintf(buffer, format, aptr);
va_end(aptr);
return(ret);
}
int main()
{
int i = 5;
float f = 27.0;
char str[50] = "hello world";
vspfunc("%d %f %s", i, f, str);
printf("%s\n", buffer);
return(0);
}
vfprintf
int vfprintf(FILE *stream, const char *format, va_list arg)
#include <stdio.h>
#include <stdarg.h>
void WriteFrmtd(FILE *stream, char *format, ...)
{
va_list args;
va_start(args, format);
vfprintf(stream, format, args);
va_end(args);
}
int main ()
{
FILE *fp;
fp = fopen("file.txt","w");
WriteFrmtd(fp, "This is just one argument %d \n", 10);
fclose(fp);
return(0);
}
vsnprintf
int vsnprintf(char *str, size_t size, const char *format, va_list ap);
#include <stdio.h>
#include <stdarg.h>
#define MAXLEN 10
int mon_log(char* format, ...)
{
char str_tmp[MAXLEN];
int i=0,j=0;
va_list vArgList;
va_start (vArgList, format);
i=vsnprintf(str_tmp, MAXLEN, format, vArgList);
va_end(vArgList);
printf("%s", str_tmp);
for(j=0;j<MAXLEN;j++)
{
printf("%d ", str_tmp[j]);
}
printf("\n");
return i;
}
void main()
{
int i=mon_log("%s,%d,%d,%c","abc",2,3,'\n');
printf("%d\n",i);
}
边栏推荐
- Win32Exception (0x80004005): 组策略阻止了这个程序。要获取详细信息,请与系统管理员联系。
- AQS之Atomic详解
- Alibaba cloud server is installed and configured with redis. Remote access is unavailable
- IIS服务器相关错误
- SQL Server 数据库的统计查询
- Print prime numbers between 100 and 200 (C language)
- The difference between & & and &
- Downloading and installing VMware (basic idea + detailed process)
- Fully understand the volatile keyword
- 拼图小游戏中学到的Graphics.h
猜你喜欢
随机推荐
打印1000~2000年之间的闰年(C语言)
Text of the basic component of the shutter
30岁,女,普通软件测试媛,对职业的迷茫和焦虑
2019.11.17 training summary
Agctfb partial solution
SQL Server 数据库的连接查询
查看CSDN的博客排名
2020-09-21 visual studio header file and Library Directory configuration
攻防世界-Re-insfsay
How to quickly complete disk partitioning
September 21, 2020 referer string segmentation boost gateway code organization level
HDU 4578 transformation (segment tree + skillful lazy tag placement)
Comment terminer rapidement une partition de disque
Dev使用过程中的基本操作
产品力不输比亚迪,吉利帝豪L雷神Hi·X首月交付1万台
BUUCTF--新年快乐
如何快速完成磁盘分区
Add/modify/drop column of alter table operation in MySQL
I would like to know how to open an account for free online stock registration? In addition, is it safe to open a mobile account?
Alibaba cloud server is installed and configured with redis. Remote access is unavailable








