Notes for printf and sprintf

Source: Internet
Author: User
■ Problem: After the user is required to input the file storage path, if the source contains a sprintf (buff, Incoming Force quota ), Once Incoming Force quota Contains "%", so the expected values in the buff may be different from the actual values. For example, if you enter the following path: C: \ Temp % sfile, then sprintf (path, "C: \ Temp % sfile"); Path Expected Value: C: \ Temp % sfile path actual value: C: \ Temp (null) file if you go to a path like fopen, obviously Program Problems may occur. Solution: Filter % before printf with the Escape Character "%" ■ problem: After requiring the user to input the path to save the file, if the source contains a sprintf (buff, Incoming Force quota ), Once Incoming Force quota Contains "%", so the expected values in the buff may be different from the actual values. For example, if you enter the following path: C: \ Temp % sfile, then sprintf (path, "C: \ Temp % sfile"); Path Expected Value: C: \ Temp % sfile path actual value: C: \ Temp (null) file if you go to a path like fopen, the program will obviously have a problem. Solution: Filter % before printf with the Escape Character "%" ■ question about % N void main () {int num1, num2; printf ("12345" % N 6789 % N \ N ", & num1, & num2) printf (" first count % d Second Count % d \ n ", num1, num2);} an asserted error is thrown after compiling and running vs2008: expression: ("'n' 'format specifier disabled", 0) ■ % A, % B, % C ...... % Z will not see this error, but % N is the original, In the printf family function, % N is used to obtain the number of printed strings. Printf ("12345" % N 6789 % N \ N ", & num1, & num2) when the above Code The result is num1 = 5, num2 = 9. However, due to security issues in the use of the printf function % N, it was often used by hackers, therefore, vs2008 is disabled by default. If % N is required, you can add the following code before using it: _ set_printf_count_output (INT enable); If the enable value is not 0, % N in the printf family function will be lifted.

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.