当前位置:网站首页>Principle of printf indefinite length parameter
Principle of printf indefinite length parameter
2022-06-27 12:43:00 【xiongsiyu979】
One 、printf() The parameters of the function are indefinite
printf() The function formats a string , Then print to the screen , and printf() The parameters of a function are uncertain (>=1)
printf() Declaration of functions :int printf(const char *format, …);
Use printf Function formatting is similar to filling in the blanks , It's divided into two parts
const char *formatyes subject ,...yes Options , There is only one requirement for doing the right job : According to the order of the questions , The variable of the option is the variable type specified by the topic
#include <stdio.h>
int main(int argc, char **argv)
{
printf("%c say %s %d \n", 'I', "hello", 6);
return 0;
}

Two 、printf() Realize the principle of variable length of parameters
Parameter transfer is stored in Stack Passed in , Indefinite parameters are stored behind fixed parameters
Indefinite parameters in memory , according to 4 or 8 Byte alignment (32 On the bit machine according to 4 Byte alignment ,64 On the bit machine according to 8 Byte alignment ), For pointer variables , Store only the address pointed to by the pointer variable
To write a simple version of my_printf(), Implement the above standards C Library functions printf() The effect of
#include <stdio.h>
#include <stdarg.h>
void my_printf(const char *format, ...)
{
va_list arg;//va_list Is a macro , Is a pointer to a variable length parameter list , That is to say “ Options ” list
va_start(arg, format);// initialization arg, take format The last first address is assigned to arg
while (*format)// Traverse " subject "
{
char ret = *format;// One char One char Ground traversal
if (ret == '%')
{
switch (*++format)// First ++ Then take
{
case 'c':
{
char ch = va_arg(arg, int);//va_arg(arg,int), get arg Points to the value of the parameter , At the same time make arg Point to the next parameter ,int Used to name the current parameter type , Although the current parameter is char type , But with int, This is because the current parameter type is only used to get values from memory , and va_list Is in accordance with the 4 Byte or 8 Byte Alignment , Designated as char Will report a mistake
putchar(ch);
break;
}
case 's':
{
char *pc = va_arg(arg, char *);
while (*pc)
{
putchar(*pc);
pc++;
}
break;
}
case 'd':
{
int in = va_arg(arg, int);
putchar(in+'0');
break;
}
default:
break;
}
}
else
{
putchar(*format);
}
format++;
}
va_end(arg);//va_end In some implementations arg Change to invalid value , here , It's a arg The pointer points to NULL, Avoid wild pointers
}
int main(int argc, char **argv)
{
my_printf("%c say %s %d \n", 'I', "hello", 6);
return 0;
}

边栏推荐
- 和动态规划的第一次相遇
- Configuration management center of microservices
- Topic38——56. 合并区间
- Private dry goods sharing: how to implement platform in Enterprise Architecture
- Pycharm in Chinese
- 关闭windows defender安全中心的方法
- Interview shock 60: what will cause MySQL index invalidation?
- Topic37——64. Minimum path sum
- mybaitis生成器详解
- 浏览器cookie转selenium cookie登录
猜你喜欢

亚马逊测评掉评、留不上评是怎么回事呢?要如何应对?

阿里一个面试题:使用两个线程,交替输出字母和数字

gcc编译动态库和静态库

esp32s3 IPERF例程测试 esp32s3吞吐量测试

全球最快下载工具 XDM

浏览器cookie转selenium cookie登录

JMeter connection DM8

ACL 2022 | TAMT proposed by Chinese Academy of Sciences: TAMT: search for a portable Bert subnet through downstream task independent mask training

PyCharm汉化

Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing
随机推荐
全志A13折腾备忘
Win10彻底永久关闭自动更新的步骤
想学好C语言,操作符也很重要
[on Nacos] get started quickly
亚马逊测评掉评、留不上评是怎么回事呢?要如何应对?
[fans' welfare] today, I'd like to introduce a method to collect money for nothing - convertible bonds. I personally verified that each person can earn 1500 yuan a year
全球最快下载工具 XDM
解开C语言的秘密《关键字》(第六期)
MySQL high level statements (I)
Picocli getting started
对象序列化
. Net6 access skywalking link tracking complete process
script defer async模式
Dynamic programming [4] (counting class DP) example: integer partition
TiDB 6.0:让 TSO 更高效丨TiDB Book Rush
application.properties 的配置信息
Detailed configuration of log4j
Operators are also important if you want to learn the C language well
uni-app开发微信小程序动态渲染页面,动态改变页面组件模块顺序
Hands on API development