Printf and scanf (to be filled later), printfscanf

Source: Internet
Author: User

Printf and scanf (to be filled later), printfscanf
Scanf and printf header files: <stdio. h> 1.% d, % 3d, % 03d, %-3d

% D: returns an integer (int) in decimal format)

% 3d: Specify the width to 3, and fill in spaces on the left of the insufficiency.

% 03d: an equal-width format with 0 filled on the left. For example, if the number is 12 and % 03d is displayed as follows: 012.

%-3d: Left alignment, with spaces on the Right of less than three digits

 1 #include<stdio.h> 2 int main() 3 { 4  5     int a = 23, b = 123, c = 1234; 6     printf("%d %d %d\n", a, b, c); 7     printf("%3d\n%3d\n%3d\n", a, b, c); 8     printf("%03d\n%03d\n%03d\n", a, b, c); 9     printf("%-3d\n%-3d\n%-3d\n", a, b, c);10     return 0;11 }
2. Differences between scanf and scanf_s

① In general, the two can replace each other: There is an int type variable a, so scanf ("% d", & a) is equivalent to scanf_s ("% d ", &)

② Scanf_s is not a standard library function, but is used in VS to ensure that data does not overflow.

For example, a char B [10] character array usually uses scanf ("% s", B), but what if the length of the input string exceeds 9? (A 10-character array can store a string of up to 9 characters because null characters are stored at the last position.) This may cause overflow or affect the input after the program.

      

 

(Figure dev-C ++ compilation)

It can be seen that scanf may cause memory leakage, although it may be correctly executed in this case (it is strictly related to the compiler's code check ), however, there is indeed a logic error (the maximum subscript of Element B is 9, and here B [10] can be set up), which is not allowed by programmers.

If it is compiled in VS 2015 IDE (the above program is not executed in VS because the scanf function reports an error in, unless the compiling environment is modified (this has not been tried yet ~)), The scanf_s function is used as follows:

 1 #include<stdio.h> 2 int main() 3 { 4     char b[10] = { '\0' }; 5     scanf_s("%s", b,10); 6     int n; 7     scanf_s("%d",&n); 8     printf("b=%s\nn=%d\n", b, n); 9     printf("b[9]=%c\n", b[9]);10     return 0;11 }

Execution status:

Figure 1: An error occurred while reading array B because the input string length exceeds 9 and is greater than 10. After debugging, it is found that the first empty character in string B is ASCII code 0, the next nine are actually-2 ASCII values '? '. After array B is input, because there is data in the input stream at this time, the buffer is not empty, so that when the integer n is assigned a value, the remaining number is directly read from the input stream (note, n is saved as a number at this time, rather than characters ).

Figure 2: When a string of 10 characters is entered, an error still occurs because array B can only store a string of 9 characters. However, the input stream buffer is empty, so you can continue to input data and assign it to B.

Figure 3: when the length of the input string is less than or equal to 9, the input is normal. Because B [9] is a null character, and the null character cannot display the output, the result of Figure 3 is displayed.

③ Scanf_s usage: The format is the same as that of scanf when it is not the input string. When it is the input string, the maximum number of characters to be read must be pointed out at the end (note that it is not the string length, the reader can try to change 10 in the previous code to 9, and then use the input data in figure 3, and the B array will have an error ).

 

 

 

 

Related Article

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.