Home > Backend Development > C#.Net Tutorial > C language ~ what does scanf mean?

C language ~ what does scanf mean?

下次还敢
Release: 2024-04-13 18:39:27
Original
438 people have browsed it

scanf function reads data from standard input into variables in the specified format. Format specifiers specify data types, such as %d (integer), %c (character), %f (floating point), %s (string). The function returns the number of variables read, EOF indicates end of file or error.

C language ~ what does scanf mean?

Introduction to scanf function

The scanf function is used in C language to read from standard input (usually the keyboard) Get formatted data. It reads data from the input stream in the given format and stores it in the specified variable.

Function prototype

<code class="c">int scanf(const char *format, ...);</code>
Copy after login

Parameters

  • format: a format string , describes the data type and format to be read.
  • ...: A variable number of pointers pointing to variables to store read data.

Return value

The scanf function returns the number of variables successfully read. If an EOF (end of file) or error is encountered, it returns EOF.

Format specifiers

The format string consists of the following format specifiers:

##%cCharacters%c%dDecimal integer%d%fFloating point number%f##%s%x
Specifiers Data type Example
String %s
Hex integer %x
Use case

<code class="c">int age;
char name[20];

scanf("%d %s", &age, name);</code>
Copy after login
This will read from standard input An integer (for age) and a string (for name) and store them in the

age

and name variables.

Note

The format specifier must match the data type to be read.
  • The input data must conform to the specified format.
  • The scanf function does not automatically discard extra characters in the input stream.
  • If there is not enough data in the input stream, the scanf function will return EOF.

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

Related labels:
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template