Connection string-sprintf

Source: Internet
Author: User

In writing a program, you sometimes need to connect two strings. Here we will introduce two functions.
Function 1: sprintf

Reference: http://oss.lzu.edu.cn/blog/article.php? Tid_877.html

Definition:
Int sprintf (char * buffer, const char * Format [, argument]...);

Description:
Sprintf is a variable parameter function. Apart from the first two parameter types, sprintf can take over multiple parameters.
Its essence is obviously on the second parameter: Format String.

Return Value: Some people pay less attention to the return value of the printf/sprintf function, but sometimes it is useful. spritnf returns the number of characters that the function call finally prints to the character buffer.
That is to say, after a sprinf call ends, you do not need to call strlen again to know the length of the result string.

The usage is as follows:

1. Print the integer to the string:
One of the most common applications of sprintf is to print Integers to strings. Therefore, spritnf can replace ITOA in most cases.
For example:
// Print the integer 123 into a string and save it in S.

Sprintf (S, "% d", 123); // generate "123"

2. connection string
Since the sprintf format control string can insert a variety of things and finally "connect them into a string", it can naturally connect strings,
This can replace strcat in many cases, but sprintf can connect multiple strings at a time (it can also insert other content in them at the same time, in short, it is very flexible ).
For example:

Char * Who = "I ";

Char * Whom = "csdn ";

Sprintf (S, "% s love % S.", who, whom); // generate: "I love csdn ."

Here are two sprintf examples:
Example 1:
Main ()
{
Int I = 0;
Char * B = "Link ";
// Char s [10];
Char * s;
S = (char *) malloc (10 );
For (I = 0; I <5; I ++ ){
Sprintf (S, "% S % d", B, I );

Printf ("% s/n", S );
}
}

Output result:
Link0
Link1
Link2
Link3
Link4

Example 2:
# Include <stdio. h>
# Include <time. h>
# Include <stdlib. h>
Int main (){
Srand (time (0 ));
Char s [64];
Int offset = 0;
Int I = 0;
For (I = 0; I <10; I ++ ){
Offset + = sprintf (S + offset, "% d,", Rand () % 100); // the return value of the sprintf function is the length of the array, which is equivalent to the value of strlen.
}
S [offset-1] = '/N'; // Replace the last comma with a line break.
Printf ("the result is: % s/n", S );
Return 0;
}

Output result:
The result is :,

Function 2:

Function Name: strcat
Function: String concatenation Function
Usage: char * strcat (char * Destin, char * Source );
Program example:
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
Main ()
{Char str1 [30] = "ABCDE", str2 [10] = "123 ";
Strcat (str1, str2 );
Printf ("the merge result is: % s/n", str1 );
}

Output result:
The merge result is: abcde123

Differences:
Since the sprintf format control string can insert a variety of things and finally "connect them into a string", it can naturally connect strings,
This can replace strcat in many scenarios, but sprintf can connect multiple strings at a time (it can also insert other content in them at the same time, in short, it is very flexible)

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.