[Notes] Usage of cin, scanf, gets (fgets), getchar

Notes~~~

Introduction to the usage of cin, scanf, gets (fgets) and getchar

#include<bits/stdc++.h>  

using namespace std;

//char *str[100]; //开一个字符串数组
 char str[100][1000]; 
 
int main()
{
	for(int i=0;i<3;i++)
	//cin>>str[i];
	//scanf("%s",str[i]);
	//fgets(str[i],10,stdin); //可以接收空格,遇到回车认为输入结束

}

1. Scanf 
Scanf can generally be used to read numbers, characters, and strings; 
conclusion: 
(1) When scanf reads a single character (%c) from the buffer, if the first character in the buffer is a space, tab or When these separators are wrapped, scanf will not ignore them, and will read and clear them. 
(2) When scanf reads a number or string (not a single character) from the buffer, if the first character in the buffer is a space, tab or newline separator, scanf will ignore it and clear it, and continue Read the next character, if the buffer is empty, continue to wait. But if the reading is successful, the delimiter after the character is left in the buffer, and scanf will not process it. 
(3) When scanf reads a character string, when it encounters a space, a carriage return, and a Tab key, it will be considered as the end of input. When a carriage return is encountered, the space and tab keys will automatically add'\0' after the string, but the carriage return, space and tab keys will still remain in the input buffer. (So ​​it cannot receive strings containing spaces, carriage returns, or Tabs) 
(4) scanf() When reading a string, the 
syntax: scanf("format control string", variable address list); 
when accepting a string: scanf("%s", character array name or pointer);

2. Gets (there is a buffer overflow vulnerability, it is recommended to use fgets)
gets is generally only used to read strings; 
conclusions: 
(1) Gets can receive spaces, and enters a carriage return as the end of the input; 
(2) gets can accept carriage return All the characters entered before the key, and replace'\0' with'\n'. The Enter key will not stay in the input buffer. 
(3) gets() When reading a string, the
syntax: gets (character array name or pointer); 
(4) The variable s in the gets(s) function is a string pointer. If it is a single character pointer, there will be no error in compiling and linking, but memory overflow error after running. 
(5) The gets function can be read infinitely, and the upper limit is not judged. The reading is ended with a carriage return, so the buffer space should be large enough so that no overflow occurs when the read operation is performed.

3. getchar 
getchar() reads a character (including space, carriage return, and Tab) in the input buffer sequence; note that the getchar function can only accept a single character, and the input number is also processed as a character. When more than one character is input, Only the first character is received. 

Fourth, the cin 
operator reads data according to the type of the variable behind. 
Input end conditions: Enter, Space, and Tab keys are encountered. 
Processing of terminator: discard the terminator (Enter, Space, Tab) that makes the input end in the buffer 

Guess you like

Origin blog.csdn.net/melon_sama/article/details/107928578