当前位置:网站首页>printf不定长参数原理
printf不定长参数原理
2022-06-27 12:17:00 【xiongsiyu979】
一、printf()函数的参数不定长
printf()函数的功能是将字符串格式化,然后打印到屏幕上,而printf()函数的参数是不确定的(>=1)
printf()函数的声明:int printf(const char *format, …);
使用printf函数格式化类似于做完型填空,它分为两部分
const char *format是题目,...是选项,做对题目只有一个要求:根据题目的顺序,选项的变量是题目指定的变量类型
#include <stdio.h>
int main(int argc, char **argv)
{
printf("%c say %s %d \n", 'I', "hello", 6);
return 0;
}

二、printf()实现参数不定长原理
参数传递是依次存放在栈中传递的,不定参数存放在固定参数的后面
内存中不定参数,按照4或8字节对齐(32位机器上按照4字节对齐,64位机器上按照8字节对齐),对于指针变量,只存储指针变量所指向的地址
来写一个简易版的my_printf(),实现上面标准C语言库函数printf()的效果
#include <stdio.h>
#include <stdarg.h>
void my_printf(const char *format, ...)
{
va_list arg;//va_list是一个宏,是指向不定长参数列表的指针,也就是“选项”列表
va_start(arg, format);//初始化arg,将format后第一个地址赋值给arg
while (*format)//遍历"题目"
{
char ret = *format;//一个char一个char地遍历
if (ret == '%')
{
switch (*++format)//先++再取值
{
case 'c':
{
char ch = va_arg(arg, int);//va_arg(arg,int),获得arg指向参数的值,同时使arg指向下一个参数,int用来指名当前参数型,当前参数虽然为char类型,但是却用int,这是由于说明当前参数类型只是为了从内存取值,而va_list是按照4字节或8字节对齐的,指定为char会报错
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 在有些实现中可能会把arg改成无效值,这里,是把arg指针指向了 NULL,避免出现野指针
}
int main(int argc, char **argv)
{
my_printf("%c say %s %d \n", 'I', "hello", 6);
return 0;
}

边栏推荐
- Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing
- 推荐系统的下一步?阿里时空聚合GNN,效果吊打LightGCN!
- Write it down once Net analysis of a property management background service stuck
- Interview shock 60: what will cause MySQL index invalidation?
- log4j的详情配置
- yml的配置
- 关于枚举类的两种用法
- Topic38——56. 合并区间
- How to modify a node_ Files in modules
- [high frequency interview questions] difficulty 1.5/5, LCS template questions
猜你喜欢

What is the next step in the recommendation system? Alispacetime aggregates GNN, and the effect is to sling lightgcn!

nifi从入门到实战(保姆级教程)——身份认证

Topic37——64. 最小路径和

Dm8: Dameng database - lock timeout

Three traversal methods of binary tree

The world's fastest download tool XDM

想学好C语言,操作符也很重要

PyCharm汉化

JMeter connection DM8

Snipaste, the world's strongest screenshot software
随机推荐
A brief talk on cordola tree
Comment modifier Node Fichiers dans les modules
How histrix works
log4j. Detailed configuration of properties
Time management understood after being urged to work at home
解开C语言的秘密《关键字》(第六期)
如何下载带有超链接的图片
Tidb 6.0: making Tso more efficient tidb Book rush
uni-app 使用escook/request-miniprogram插件发请求说明
Ssh server configuration file sshd_ Config and operation
The world's fastest download tool XDM
MySQL high level statements (I)
微服务之配置管理中心
Raspberry pie 3b+ learning
AI for Science:科研范式、开源平台和产业形态
号称史上最难618,淘宝数据盘点你做对了吗?
Win10彻底永久关闭自动更新的步骤
application. Configuration information of properties
想学好C语言,操作符也很重要
Quanzhi A13 tossing memo