Use and difference of printf (), sprintf (), scanf () and sscanf () in C Language

Source: Internet
Author: User

The following is a detailed analysis of the usage and differences of printf (), sprintf (), scanf (), and sscanf () in C language. For more information, see

Printf
Syntax:
# Include <stdio. h>
Int printf (const char * format ,...);

The printf () function prints the output to STDOUT (standard output) and other parameters according to the format given by format. The return value is the number of output characters.
Sprintf
Syntax:
# Include <stdio. h>
Int sprintf (char * buffer, const char * format ,...);
The sprintf () function is similar to printf (), and the format control is the same. As long as it is a formatted string used by printf, it can be used in sprintf, but the output is sent to buffer (buffer. The return value is the number of characters written.

Function 1: format a numeric string
Sprintf (s, "%-8X", 12345); // Changes to "12345"
The uppercase "X" indicates that the hexadecimal format is uppercase. The width occupies 8 positions, and the "-" indicates the left alignment.

Function 2: control the floating point printing format
Floating Point Numbers are controlled using the format character "% f". By default, the 6-digit % m. nf after the decimal point is retained. m indicates the width of the print, and n indicates the number of digits after the decimal point.
Sprintf (s, "% 10.3f", 3.1415626); // s: "3.142"

Function 3: connect two strings
Direct Connection:
Char dest [256];
Char src1 [] = {'A', 'B', 'C', 'D', 'E '};
Char src2 [] = {'1', '2', '3', '4'}; s
Printf (dest, "%. 5 s %. 4 s", src1, src2); // output: "abcde1234"

Intercept some characters of a string for connection,
Char dest [256];
Char src1 [] = {'A', 'B', 'C', 'D', 'E '};
Char src2 [] = {'1', '2', '3', '4 '};
Sprintf (dest, "%. * s %. * s", 2, src1, 3, src2); // output: "ab123"

Function 4: character/Ascii code comparison
We know that printing a character using "% d" or "% x" can produce its 10-or 16-digit ASCII code. In turn, print an integer using "% c" to see its ASCII characters. The following section prints the ASCII code table of all visible characters to the screen (printf is used here, note that "#" and "% X" are automatically prefixed with "0X" in hexadecimal notation ):
For (int I = 32; I <127; I ++ ){
Printf ("[% c]: % 3d 0x % # 04Xn", I );
}

Function 5: print the address information
Sometimes, when debugging a program, we may want to view the addresses of some variables or members. Because the addresses or pointers are only 32-bit numbers, you can print the unsigned integers "% u:
Sprintf (s, "% u", & I );
However, people usually prefer to use hexadecimal instead of hexadecimal to display an address:
Sprintf (s, "% 08X", & I );
However, these are indirect methods. For address printing, sprintf provides a special "% p ":
Sprintf (s, "% p", & I );
I think it is actually equivalent:
Sprintf (s, "% 0 * x", 2 * sizeof (void *), & I );

Function 6: return values
The returned values of printf and sprintf are the number of written characters.
That is to say, after each sprinf call ends, you do not need to call strlen again to know the length of the result string. For example:
Int len = sprintf (s, "% d", I );
Scanf
Syntax:
# Include <stdio. h>
Int scanf (const char * format ,...);

The scanf () function reads data from stdin (standard input) based on the format specified by format and saves data to other parameters.
Sscanf
Syntax:
# Include <stdio. h>
Int sscanf (const char * buffer, const char * format ,...);

The sscanf () function is similar to scanf (), but the input is read from the buffer.
Similar to scanf, sscanf is used for input, but the latter uses the screen (stdin) as the input source, and the former uses a fixed string as the input source.
Usage:
% [] Indicates that a character set is to be read. If [followed by the first character is "^", it indicates the inverse meaning. The string in [] can be 1 or more characters. The null character set (% []) violates regulations and can lead to unpredictable results. % [^] Is also in violation of regulations.

% [A-z] reads strings between a-z. If it is not earlier than this, it is stopped, as shown in figure
Char s [] = "hello, my friend"; // note: the comma is between non-a-z.
Sscanf (s, "% [a-z]", string); // string = hello

% [^ A-z] reads strings not between a-z. If a character between a-z is encountered, it is stopped, as shown in figure
Char s [] = "HELLOkitty"; // note: the comma is not between a and z.
Sscanf (s, "% [^ a-z]", string); // string = HELLO

% * [^ =] The variable is not saved with the * sign. Skips a qualified string.
Char s [] = "notepad = 1.0.0.1001 ";
Char szfilename [32] = "";
Int I = sscanf (s, "% * [^ =]", szfilename); // szfilename = NULL because it is not saved
Intj = sscanf (s, "% * [^ =] = % s", szfilename); // szfilename = 1.0.0.1001

% 40c reads 40 characters
% [^ =] Reads the string until '=' is reached. '^' can contain more characters, such:
Char s [] = "notepad = 1.0.0.1001 ";
Char szfilename [32] = "";
Int I = sscanf (s, "% [^ =]", szfilename); // szfilename = notepad
If the parameter format is: % [^ =:], you can also read notepad from notepad: 1.0.0.1001.

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.