当前位置:网站首页>1310_ Implementation analysis of a printf
1310_ Implementation analysis of a printf
2022-07-25 05:11:00 【grey_ csdn】
All learning summary : GitHub - GreyZhang/c_basic: little bits of c.

From this place we can see , Although we're calling printf, But the functions implemented in this module are actually printf_, It's just a clever transformation through the skill of macro definition . In this way, we can maintain the original habit of writing code , At the same time, it avoids possible printf Conflict .

About va_list、va_start、va_end Use , Let's take a look at... Through an example .

The example above , You can see the simple use of several macros . meanwhile , There is also a new one va_arg Use . First compile and run this code .

This is the code compilation above 、 The effect of operation . Actually , Here is a key place . Although these va_xxx The usage of function variable parameter realizes the call of function variable parameter , But in fact, the number of this parameter cannot be recognized automatically , It is controlled by the code itself . What does this mean ? We can see such a conclusion by modifying the code .

Like the code above , I put count The parameter of is changed to 3, The following parameters are still 4 individual . that , What is the effect of such compilation ?

This is the effect of compiling and running the above code . From this result , Actually, I did 5 Finally, the program only recognizes 4 individual . this 4 individual , It could be interpreted as one 1 + 3 Result , And the first parameter in front count It determines how many parameters should be taken in the end . That is to say , The number identification of this parameter cannot be automated or intelligent , You can only make program level judgments by yourself . such , Continue with the previous analysis .

such , Let's look at the implementation of this function . In fact, the key point will shift to _vsnprintf The use of functions ._vsnprintf function , Called a character output function , A temporary one in front buffer, After the printf_ The passed in parameters of are completely inherited .


Take another look at the basic parameters of this function : The first parameter is actually a function type , This definition is attached to the above ; The second parameter is a pointer to a character , From the name, it should be a buffer of characters , The third is the maximum length , The fourth and fifth parameters are from printf_ function .
The next judgment is to look at this character buffer Is it a valid character , If not, then you don't need to output , The output function called is also an output free function . above printf_ Defines a valid buffer, In fact, it also ensures that this place will not be effective out It's covered up .

Keep going , If the input parameter is not % At the beginning , That is to say, characters can be output directly . Otherwise , The processing of parameters moves back .



So much above , In fact, I just made a judgment on the format . If , To give this pritnf If you continue to lose weight , There are some parameters I don't usually use, such as ,j,z wait , I can remove it directly .


The code interception of this part is not very complete , But it is easy to understand . What we deal with here is all digital information , First of all, a module is judged , Know exactly how many decimal . Here, we can probably know his next handling method , I won't analyze the scattered small functions . Another clever place is the processing of positive and negative values , Directly through this, the processing logic is divided into two . After that , The data display format conversion and output are packaged together .

Here is an implementation support of floating point and scientific counting .

Here is the printing of characters and strings , In fact, format conversion is considered in every processing , This may result in output content ratio buffer More , The main function is filling and alignment . If only for debugging embedded software , In fact, such functions can also be accommodated , A part of the design can be eliminated .

There is not much to explain in the following output , Here is an output %, In fact, there is only % But when there is no data format , Then you can directly output one % end .
Look at it this way , In fact the whole printf The implementation of is very simple , The key point is actually this vsnprintf The implementation of the .

In the code I used , There are also several such functions . Actually , These functions are almost useless for my usual use , therefore , It can also be optimized directly . I directly try to do a code shielding , Then compile and test the whole project .

The result is the same as I expected , There are no compilation or link problems . look , The gadget I use can continue to be tailored in terms of resources .
边栏推荐
- Thesis reading | which is the best multilingual pre training technology for machine translation? See the latest progress!
- Interviewer: explain the core principle of ThreadLocal
- Matter's Unified Smart Home connection standard enables local automatic interaction between smart devices
- 【微信小程序】拍卖商品详情页设计与交互实现(包含倒计时、实时更新出价)
- When image component in wechat applet is used as background picture
- 300. Longest increasing subsequence
- Performance Optimization: how to solve the slow loading speed of the first screen of spa single page application?
- Execution process of NPM run XX
- Unity LOD
- 38 lines of PHP code free import database analysis Linux access log
猜你喜欢

小红书携手HMS Core,畅玩高清视界,种草美好生活

Summary and Prospect of aut, the transport layer protocol of sound network -- dev for dev column

QT download installation tutorial

Browser cache HTTP cache CDN cache localstorage / sessionstorage / cookies

Li Kou 731. My schedule II

Now the operator wants to check the answer details of all user questions from Zhejiang University. Please take out the corresponding data

I have seven schemes to realize web real-time message push, seven!

Unity LOD

ES6 -- Methods and extensions of array objects, traversal of arrays, and extension methods of strings

Interviewer: explain the core principle of ThreadLocal
随机推荐
H5 new feature domcontentloaded event - custom attribute -async attribute -history interface method - local storage -postmessage
four hundred and forty-four thousand one hundred and forty-one
教你如何定位不合理的SQL?并优化之
[untitled]
自己实现is_convertible
深圳随到随考,科目四随到随考,科三理论第二理论随到随考说明
Render Optimization: repaint and reflow
js 页面增加过渡层
Androd releases jitpack open source project (gradle7.2)
Why does the official not recommend using UUID as MySQL primary key
Json.tojsonstring cannot pass Boolean
Redis集群搭建(Windows)
Unity LOD
1310_一个printf的实现分析
Unity LOD
微信小程序相关操作示例
绕过 Web 应用程序中的 XSS 过滤器
Preliminary understanding of Panda3D particle system
DOM在Ahooks中的处理过程
Performance Optimization: lazy loading of pictures