Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use sprintf function in C language

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to use the C language sprintf function", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use the C language sprintf function.

I. brief introduction of sprintf function

The sprintf function is used to write formatted data to a string based on the following prototype:

# include / * * description: write formatted data to a string * * Parameter: * [out] str: output buffer to get the formatted string; * [in] format: formatted string, like the printf function; * * return value: if successful, the total number of characters written is returned, excluding the empty characters appended to the end of the string. If it fails, a negative number is returned. * / int sprintf (char * str, const char * format,...) 2. The sprintf function uses / * / / @ Author: ape programming / / @ Blog (personal blog address) : www.codersrc.com//@File:C language tutorial-C language sprintf function / / @ Time:2021/07/28 08:00//@Motto: it is impossible to reach thousands of miles without long steps. Do not accumulate a small stream can not become a river and sea, the program of wonderful life needs to unremittingly accumulate! / * / # include int main () {char buf [1024] = {0}; sprintf (buf, "www.codersrc.com\ n") Printf ("% s", buf); sprintf (buf, "www.codersrc.com age:%d\ n", 17); printf ("% s", buf); sprintf (buf, "www.codersrc.com age:%d name:%s\ n", 17, "zhangsan"); printf ("% s", buf); sprintf (buf, "www.codersrc.com age:%d name:%s height:%f\ n", 17, "zhangsan", 1.75) Printf ("% s", buf);}

Output:

Www.codersrc.com

Www.codersrc.com age:17

Www.codersrc.com age:17 name:zhangsan

Www.codersrc.com age:17 name:zhangsan height:1.750000

III. Differences between sprintf/fprintf/printf functions

Sprintf outputs a formatted string to a destination string

Printf outputs a formatted string to the screen

Fprintf is to write a formatted string to a file

At this point, I believe you have a deeper understanding of "how to use the C language sprintf function". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report