Home > Article > Backend Development > How to use gets function in c++
The gets() function in C is used to read a string from standard input and store it in a character array. It reads the string until a newline character is encountered or the end of file is encountered. Its usage includes: declaring a character array to store strings. Use the gets() function to read the string. Verify the return value to ensure the read was successful.
Usage of gets() function in C
gets() function is used to read characters from standard input string and store it in the specified character array. It is similar to the scanf() function, but has no format specifier and reads until a newline character is encountered or the end of file is encountered.
Syntax:
<code class="cpp">char *gets(char *str);</code>
Parameters:
Return value:
Usage:
To use the gets() function, follow these steps:
Example:
<code class="cpp">#include <iostream> using namespace std; int main() { char str[100]; cout << "Enter a string: "; gets(str); if (str != NULL) { cout << "The string you entered is: " << str << endl; } else { cout << "Error reading the string." << endl; } return 0; }</code>
Note:
getline()
or fgets()
. The above is the detailed content of How to use gets function in c++. For more information, please follow other related articles on the PHP Chinese website!