How to use sprintf function in C language
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!