C/C++ common input and output usage details summary (graphic analysis)

One.scanf() and printf()

  •     Be sure to include #include<stdio.h> when calling printf and scanf
  •     The usage of printf is: printf("format control string", output parameter one, output parameter two);
  •     The usage of scanf is: scanf("format control string", input parameter one, input parameter two);
#include<stdio.h>
int main(){
	int a=996;
	printf("%d\n",a);//scanf()同理
	return 0;
}

AC expansion:

1. %*, which means receiving and reading but does not save the corresponding specified type data

                Usage: scanf("%d%*c%d",&a,&b) For example, when receiving 1, 2 or 1/2 of three data, you can assign 1 to a and 2 to b. Although the characters in the middle are received but do not need to be stored, it perfectly solves the problem of separation when the user outputs two numbers again.

2. The  return value of the scanf function    (to put it bluntly, it means how many parameters are correctly received, and what is the return value)         

  • Normal input of two integers

scanfä¸é¢ï¼ä½ å¯è½ä¸ç¥éçscanfçç¨æ³

  • Abnormal input of two integers

scanfä¸é¢ï¼ä½ å¯è½ä¸ç¥éçscanfçç¨æ³

  • Two integers with %* separate normal input

scanfä¸é¢ï¼ä½ å¯è½ä¸ç¥éçscanfçç¨æ³

      3. Data output width         

  • The complete format of printf format control:
    %-0 mn l or h format characters

       ①%: indicates the start symbol of the format description, which is indispensable.
       ②-: Yes- means left-justified output, if omitted, it means right-justified output.
       ③0: 0 means the designated space is filled with 0, if omitted, the designated space is not filled.
       ④m.n: m refers to the domain width, that is , the number of characters occupied by the corresponding output item on the output device . N refers to accuracy. Used to describe the number of decimal places of the output real number. When n is specified, the implicit precision is n=6 bits.
       ⑤L or h:l refers to long type for integer type, and double type for real type. h is used to modify the format characters of integer type to short type.

        Detailed description ④:

  • %ms: The output string occupies m columns. If the length of the string itself is greater than m, the limit of m will be exceeded and all the strings will be output. If the string length is less than m, a space is added to the left.

#include<stdio.h>
int main(){
	char s[100];
	scanf("%s",s);
	printf("%10s\n",s);
	return 0;
}

  • %-ms: If the string length is less than m, within the range of column m, the string will lean to the left, with spaces on the right.

  • %m.ns: The output occupies m columns, but only the n characters from the left end of the string are taken. The n characters are output on the right side of column m, with spaces on the left

#include<stdio.h>
int main(){
	char s[100];
	scanf("%s",s);
	printf("%10.4s\n",s);
	return 0;
}

  • %-m.ns: where m and n have the same meaning as above, and n characters are output on the left side of the m column range, with spaces on the right. If n>m, the value of n is automatically taken , that is , the normal output of n characters is guaranteed.

  • %m.nf: The output occupies a total of m columns, which have n decimal places. If the value width is less than m, add a space at the left end.

#include<stdio.h>
int main(){
	float f;
	scanf("%f",&f);
	printf("%10f\n",f);
	return 0;
}

Supplement: The decimal part of floating-point numbers occupies 6 digits, the decimal point occupies 1 digit, and the integer part occupies 2 digits. In addition, the precision range of floating-point numbers is limited (with certain errors). For details, please refer to   https://blog.csdn.net/imJaron/ article/details/78392264

  • %-m.nf: The output occupies a total of n columns, which have n decimal places. If the value width is less than m, add a space at the right end.

  • %e: The number part (also known as the mantissa) outputs 6 decimal places, and the exponent part occupies 5 or 4 digits.

  • %m.ne and %-m.ne: The meanings of m, n, and "-" characters are the same as before. Here n refers to the number of decimal places in the digital part of the data, and m represents the width of the entire output data.

For other formats, readers should verify the distinction by themselves.

二.cin.get()   getchar()     cin.getine()   getline()   gets()    scanf()

  • cin.get() reads a single character, including any leading white space characters. For details, please refer to: http://c.biancheng.net/view/1346.html
  • getchar() Get a character, end an input with a carriage return, and there will be carriage return in the buffer
  • cin.getline() Get a line of string

          cin.getline(s,100); Read a whole line at a time and discard the newline generated by the Enter key, s must be a character array type, that is, char s[100];

  • getline() is C, get a line of string

          getline(cin,s) //Accept a string, can accept spaces and output, must include the header file #include<cstring>, s must be a string type, namely string s; you need to press the Enter key twice to display when outputting , The first time you press Enter to indicate the end of the string, the second time you press Enter to start output.

  • gets() accepts a string, can accept spaces and output it, and end an input with a carriage return, the buffer does not leave carriage returns
  • scanf(); End an input with a leading blank such as tab space, carriage return, and carriage return in the buffer

Article source reference link:

1. https://www.cnblogs.com/michaeljunlove/p/3883717.html

2.https://www.dotcpp.com/wp/786.html

3.https://wenwen.sogou.com/z/q654652327.htm

Guess you like

Origin blog.csdn.net/qq_41048982/article/details/90709309