Usage of sprintf () function in C language

Source: Internet
Author: User
Tags sprintf

Turn from: http://nnssll.blog.51cto.com/902724/198237/

The use of sprintf functions
1, the function is contained in the stdio.h header file.
2, sprintf and usually we often use the printf function is very similar. The sprintf function prints to the string, and the printf function prints the output to the screen. The sprintf function is widely used in our operations for converting other data types to string types.
3, the format of the sprintf function:
int sprintf (char *buffer, const char *format [, Argument,...]);
The optional argument can be any other than the first two parameters are fixed. Buffer is a character array name; format is a formatted string (like: "%3d%6.2f% #x%o", and% is combined with # to automatically precede the hexadecimal number with 0x). As long as the format string that can be used in printf can be used in sprintf. The formatted string is the essence of this function.
4, Char str[20];
Double f=14.309948;
sprintf (str, "%6.2f", f);
can control the accuracy
5, Char str[20];
int a=20984,b=48090;
sprintf (str, "%3d%6d", a,b);
str[]= "20984 48090"
You can connect multiple numeric data.
6, Char str[20];
Char s1[5]={' A ', ' B ', ' C '};
Char s2[5]={' T ', ' Y ', ' X '};
sprintf (str, "%.3s%.3s", S1,S2);
Multiple strings can be concatenated into strings
%M.N in the output of the string, m represents the width, the number of columns in the string, and n represents the actual number of characters. %M.N in floating-point numbers, m also denotes width; n represents the number of digits in a decimal.
7, can dynamically specify, need to intercept the number of characters
Char s1={' A ', ' B ', ' C '};
Char s2={' T ', ' Y ', ' X '};
sprintf (str, "%.*s%.*s", 2,S1,3,S2);
sprintf (S, "%*.*f", 10, 2, 3.1415926);
8, sprintf (S, "%p", &i);
Can print out the address of I
The above statement is equivalent to
sprintf (S, "%0*x", 2 * sizeof (void *), &i);
9, the return value of the sprintf is the number of characters in the array of characters, that is, the length of the string, without calling strlen (s) to find the length of the string.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.