Home >Backend Development >C#.Net Tutorial >What does scanf mean in C language?

What does scanf mean in C language?

下次还敢
下次还敢Original
2024-05-02 17:09:16998browse

The scanf function is used to read data from standard input. It specifies the data type and format through a format string and returns the number of items read.

What does scanf mean in C language?

The meaning of scanf

scanf is used in C language to read from standard input (usually the keyboard) function of the data. The "scan" in its name means scan input and "f" means format.

Function prototype

<code class="c">int scanf(const char *format_string, ...);</code>

Parameters

  • format_string: a format string, Specifies how the input is interpreted.
  • ...: An optional list of variables to receive the read data.

How it works

scanf uses format_string as a template to guide input interpretation. format_string Contains format specifiers, each format specifier corresponding to a data type to be read (for example, %d represents an integer, %f represents a floating point number).

scanf scans the input characters one by one and converts them to the corresponding data type according to the format specifier. scanf reports an error if the input does not match the format specifier.

Return value

scanf returns an integer indicating the number of successfully read data items. If an error occurs or End of Input (EOF) is reached, EOF is returned.

Example

The following code uses scanf to read an integer and a floating point number from standard input:

<code class="c">int num;
float real;
scanf("%d %f", &num, &real);</code>

Note

  • scanf does not automatically skip whitespace characters (e.g. spaces, tabs). If you need to skip whitespace characters in format_string, you can use %*[whitespace]. (where * means read but discard input)
  • scanf does not verify that the input value is valid. If an invalid value is entered, scanf reports an error or gives unpredictable results.

The above is the detailed content of What does scanf mean in C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn