Scanf () function usage Summary

Source: Internet
Author: User
Tags control characters
Although some people say that scanf () is never used in large-scale programming, scanf () is often used by people who are used to C and who participate in ACM/ICPC, in addition, it is easy to sort out the knowledge that is commonly used and may be hard to understand. General knowledge:The common format of the scanf () function is: Scanf ("Format String", input first address table)The common format of scanf format control is : % [*] [Width] [f | n] [H | L] type character
  • The control characters in [] are optional.
  • "*" Indicates that the input value is skipped if no variable is assigned after it is read. This is useful in reducing memory spending and skipping unnecessary characters to avoid applying for useless variable space.
  • "Width" indicates the length of the input read characters. For an integer, the corresponding width number is truncated and assigned to the corresponding variable in the subsequent list; for the character type, the first character is assigned to the corresponding variable after the characters of the corresponding length are read, and the rest are automatically discarded. For example, scanf ("% 2D % 3d", & A, & B); If the input is 12345, 12 is assigned to a and 45 is assigned to B; scanf ("% 2C % 3C", & A, & B); If the input is 12345, '1' is assigned to a and '3' is assigned to B.
  • F, N, H, and l indicate the remote pointer, near pointer, short integer, and long integer respectively. For _ int64, the corresponding control character is ll or i64.
  • "Type character" is d -- enter a decimal integer, O -- enter an octal integer, X -- enter a hexadecimal integer, u -- enter an unsigned decimal integer f or e -- enter a real number (in decimal or exponential form), c -- enter a single character, s -- enter a string
Some tips for reading stringsThere are some useful controls for the input string. For example, you often need to read a line of string, which may contain spaces, tabs, and other blank characters, if you use % s directly, it is not acceptable, so some people think of using gets (). Of course, this is also an option, but people who know C basically know gets () it is a very dangerous function, and it is difficult to control, especially when it is used in turn with scanf (), the disadvantage of the former is even more obvious, so gets () is generally not recommended, in fact, % [^/n] can solve this problem very well. ^ indicates "not", that is, the character after it is read ends reading. In this way, you can directly use scanf ("% [^/n] % * C", STR); to read a line of strings. The role of % * C is to read/n, otherwise, it will always be/n. All controls that take effect on % s can use % []. For example, % [0-9] indicates read-only characters between '0' and '9, % [A-Za-Z] indicates read-only letters. '-' indicates a range connector. Of course, you can also directly list the characters you need to read, the reason why the range connector is used for reading letters above is that it is too troublesome to enter 52 characters, if you only need to read characters in "ABC", you can use % [ABC] (or % [cab], % [ACB], % [a-c], and % [C -A]...), if you want to read strings out of a certain range, add a '^' to the front, for example: % [^ A-Z] indicates reading characters other than lower-case letters. In fact, there are many promotion usages. For example, you need to process the following strings 23 44r f3088888, 3245; 34: 123. you can use the following code to output all the numbers: # include <stdio. h>
Bool SKIP (){
Scanf ("% * [^ 0-9]");
Return true;
}
Int main (){
Int N;
While (skip () & scanf ("% d", & N )! = EOF)
Printf ("% d/N", N );
Return 0;
}

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.