Home > Article > Backend Development > How to use scanf in c++
The scanf() function in C reads formatted data from standard input and stores it in a variable. The syntax is: int scanf(const char *format, ...), where format specifies the type and format of the read data, and ... is the address of the variable to store the read data. Usage: 1. Specify the format string and use the specifiers %d, %f, %c, %s to specify the data type; 2. Use the & operator to get the variable address; 3. Call the scanf() function and pass in format and variable address as parameters.
The scanf() function in C is used to read from the standard input Format and store data into the specified variable. The following is its syntax:
<code class="cpp">int scanf(const char *format, ...);</code>Where: format:
#Specify the format string:
%f | |
%c | |
%s | |
<code class="cpp">int main() { int i; float f; scanf("%d %f", &i, &f); // ... }</code>
This will read an integer i and a float f from standard input.
Note:scanf() function returns the number of successfully read variables. If no variables were read, 0 is returned.
The format string must specify a specifier that matches the type of data to be read. Otherwise, undefined behavior results.The above is the detailed content of How to use scanf in c++. For more information, please follow other related articles on the PHP Chinese website!