The method of sprintf used in C + + and the difference analysis of printf _c language

Source: Internet
Author: User
Tags sprintf

This article illustrates the difference between the methods used by sprintf in C + + and printf. Share to everyone for your reference. The specific analysis is as follows:

First, we'll look at the MSDN prototype and the sprintf prototype

Copy Code code as follows:
int printf (const char *format [, argument] ...);

And
Copy Code code as follows:
int sprintf (char *buffer, const char *format [, argument] ...);

By definition, the functions of the two are very similar.

If you are in touch with more console programs, see printf More, printf functions print results to the screen, and the sprintf function can complete other data types converted to strings.
Let me explain from the following points

(1) The first two parameters of the function are fixed, the optional parameter is any, buffer is to hold the string pointer or array name, Fromat is a formatted string, as long as the formatted string used by printf, can be used in sprintf, formatted string is the essence of the function.
(2) You can first format an integer data into a string. Like what:

Copy Code code as follows:
Char str[20];
int i_arg = 12345;
sprintf (buf, "%-6d", I_arg);

(3) Look at an example of a floating-point type. Like what:
Copy Code code as follows:
Char str[20];
Double d_arg = 3.1415926;
sprintf (str, "%6.2f", D_arg);

can control the accuracy
(4) to connect two strings, or to concatenate multiple 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 represents width, and n represents the number of decimal digits. Like what:
Copy Code code as follows:
Char dest[256];
Char src1[] = {' A ', ' B ', ' C ', ' d ', ' e '};
Char src2[] ={' 1 ', ' 2 ', ' 3 ', ' 4 '};
sprintf (dest, "%.5s%.4s", SRC1,SRC2);

You can also dynamically intercept some characters of a string:
Copy Code code as follows:
Char dest[256];
Char src1[] = {' A ', ' B ', ' C ', ' d ', ' e '};
Char src2[] ={' 1 ', ' 2 ', ' 3 ', ' 4 '};
sprintf (dest, "%.*s%.*s", 2,SRC1,3,SRC2);

You can also steal a valid bit of a floating-point type
Copy Code code as follows:
sprintf (str, "%*.*", 10,4,d_arg);

To add, the sprintf return value is the number of characters in the string, which is the result of strlen (str),
You can also print the address of a parameter
Copy Code code as follows:
int i=2;
sprintf (str, "%0*x", sizeof (void *), &i);

or use:
Copy Code code as follows:
sprintf (str, "%p", &i);

In addition, these are all multibyte-type (ANSI) functions, and similar functions should be used for Unicode types:

Copy Code code as follows:
int wprintf (const wchar_t *format [, argument] ...);

int swprintf (wchar_t *buffer, const wchar_t *format [, argument] ...);

The usage is very similar to the above, that is, the type is different.
For printf and sprintf This function is included in the <stdio.h> header file
The sprintf and wprintf functions are included in the <stdio.h> or <wchar.h> header file.

I hope this article will help you with the C + + program design.

Related Article

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.