Scanf and printf functions are used to format input and output)

Source: Internet
Author: User

Scanf and printf functions are used to format input and output, which are powerful but error-prone. In particular, scanf has more serious consequences.
In C, the default Integer type is int, and the floating point type is double by default. Therefore, % HD and % LD must be used for reading short and long with scanf, while floating point numbers are somewhat special. % F corresponds to float instead of % HF, while double corresponds to % lf, long Double corresponds to % lf. In fact, I think it is more reasonable to use % HF -- float, % f -- double, % lf -- long double, which is more consistent with integer processing, I really don't know how scanf's design and implementation are considered.
Since the compiler does not scan the strings in the scanf and printf formats, it is unknown whether the matching is correct. Scanf should not process any parameters. The short pointer is the short pointer and the float pointer is the float pointer. Printf is different. Parameters with lower precision than int, such as short, will be upgraded to int, and types with lower precision than double, such as float, will be upgraded to double.
Scanf treats a specific parameter as a Type pointer regardless of the type of the input pointer. For example, % d corresponds to a 4-byte integer pointer, if the input is a short pointer, scanf will still use it as an integer pointer, and the result is modified by two more bytes. In addition, % d and % HD correspond to char *, and % F, % lf, or % lf correspond to int *, which may cause serious consequences. % N corresponds to an integer pointer and returns the number of scanned characters. % HN corresponds to short * and % LD corresponds to long *, which is consistent with % d. % N does not scan the input stream, which is the biggest difference between other format codes.
Printf is relatively safer, because in most cases it only reads the parameter, and % N is an exception. However, a corresponding error may cause incorrect output results. For example, the classic ones are:
Printf ("% d \ n", 3.14 );
The output result is not 3 but a strange 1374389535. The reason is actually very simple. 3.14 is an 8-byte double value, but printf treats its 4-Byte Low as an int, so I got such a strange result. Printf does not know how to deal with 3.14, but only knows that it is a double value, so it is treated as a double. If you put a character variable, the additional three bytes on the stack will be read, and the memory access may be incorrect.

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.