cpp skills - data input and output

Brush oj, input and output to master, otherwise it will be a waste of time.

Entry

  • scanf (): various types of data can be entered.
  • getchar (), getche (), getch (): These three functions are used to enter a single character.
  • gets (): Get the data line, and as a string. (In '\ n' end)

cin.getline function as cpp, c read characteristics can be used.

scanf

  • In scanf, all data entered from the keyboard, regardless of numbers, letters, or space, Enter, Tab and other characters, will be treated as data stored in the buffer. It is stored sequentially to the input of the front row, the next row sequentially input. Press Return when taken scanf entered data buffer from front to back sequentially taken.

  • scanf% d only recognize the "decimal integer." In terms of% d, space, carriage return, Tab key data and delimiters are case data. Scanf when taken into the data buffer, and if encountered% d spaces, carriage returns, Tab key, then it is not accessible, but continue to the next skip taking the subsequent data, until get to the "decimal integer" until. For skipped and extracted data, which the system will be freed from the buffer. Skipped or not the data fetched, that the system will have been placed in the buffer until the next scanf acquired.

  • If you encounter% d letters, then it does not skip will not be accessible, but jumped directly from the buffer.

  • However, if the replaced% d% c, then any data will be treated as a character, whether it is digital or spaces, carriage returns, Tab key will retrieve it.

  • Input from the keyboard 123, this number is not 123, but the character '1', the character '2' and the character '3', are arranged sequentially in a buffer. Because each character can only put a char variable character. Therefore, after the input of "123" and press Enter, scanf entered the buffer, in the order, first take the character '1', if you have to take and then take character '2', and so on.

#include<cstdio>
scanf("%d",&var);

Format control characters

Here Insert Picture Description
Stressed:% s is after the end of white space characters, blank character is still in the buffer. After using gets () '\ n' will not be the buffer

Export

  • puts (): Only output string, and the rear end of the output will automatically wrap.
  • putchar (): can only output a single character.
  • printf (): various types of data can be output.
//cpp printf() 标准格式
%[flag][width][.precision]type

//eg:
printf("%-3d",x);
//左对齐,宽度为3,输出类型为int

Format control character

Format control character Explanation
%c The output of a single character
%hd、%d、%ld In decimal, integer output symbol has the form of short, int, long type
Hu%,% u,% lu In decimal, unsigned integer output in the form of short, int, long type
% I,% o,% do In octal, without the prefix, unsigned integer output in the form short, int, long type
#% I,% #, or% # it Octal, prefix, unsigned integer output in the form short, int, long type
%hx、%x、%lx %hX、%X、%lX In hexadecimal, without the prefix, unsigned integer output in the form short, int, long type. If x is lowercase hexadecimal digits then the output is also lower case; if X capitalized, then the output hexadecimal numbers are also capitalized.
%#hx、%#x、%#lx %#hX、%#X、%#lX Hexadecimal, prefixed integers, unsigned form output short, int, long type. If x is lowercase, then the output hexadecimal numbers and prefixes are lowercase; if X capitalized, then the hexadecimal number prefix and outputs are capitalized.
%f、%lf Output float, double type in decimal decimal
% E,% and% Yes% lE Output float, double type in the form of fractional exponent. If e is lowercase, then the output is also lower case e; if uppercase E, then the output of the E is also capitalized.
% G,% lg% G,% lG Output float, double, and decimal fractional number to index a shorter form, and does not add the fractional part of the last extra 0. If lowercase g, e or lower case then exponentially when the output; if G uppercase, then exponentially when the output E is also capitalized.
%s Output a string
Flag character meaning
- - for left-aligned. If not, follow the default alignment, the default is usually right-aligned.
+ For an integer or a decimal output indicating sign (sign). If not, then the output will be only negative symbol. Space for an integer or a decimal, the output timing is preceded by a space, known as a negative number is negative.
# For octal (% o) and hexadecimal (% x /% X) integer, # indicates a prefix is ​​added when the output; prefix 0 octal, hexadecimal prefix 0x / 0X. For fractional (% f /% e /% g), # indicates force in decimal output. If there is no fractional part, the default is not output decimal point, after the #, even if there is no fractional part will be a decimal point.

Conversion char * / char [] / string of

https://blog.csdn.net/CGD_ProgramLife/article/details/80410663

references

The main source of this article: http://c.biancheng.net/view/1793.html

Guess you like

Origin blog.csdn.net/baidu_41560343/article/details/90719093
Recommended