Home > Backend Development > C++ > What is the difference between scanf() and gets() in C language?

What is the difference between scanf() and gets() in C language?

WBOY
Release: 2023-08-26 10:33:06
forward
2809 people have browsed it

What is the difference between scanf() and gets() in C language?

In C language, both scanf() and gets() functions are defined to get input from external sources and pass it as input to the system. Now there are some characteristic differences between these two functions.

The following are the important differences between scanf() and gets() in C language -

Sr. No. key scanf() function gets() function
1 Definition The scanf() function can read input from the keyboard and store them according to the given format specifier. It reads input until a space, newline, or EOF is encountered. The gets() function, on the other hand, is used to receive input from the keyboard until a newline character or EOF is encountered. Spaces are considered part of the input.
2 WhiteSpace In the scanf() function, spaces are not considered input characters. If any spaces are encountered in the middle, it Also stops reading input from external sources. On the other hand, in the get() function, space is treated as an input character, and it is also treated as an input character. If any spaces are encountered in between, stop reading input from the external source and continue reading from the input source.
3 Syntax scanf() function takes a format string and a list of variable addresses. For example, scanf(“%d”, &number); On the other hand, the get() function takes the name of the variable to store the received value. For example, gets(name);
4 DataType scanf() function can read multiple values ​​​​of different data types. td> But on the other hand, the get() function will only get string data.

The above is the detailed content of What is the difference between scanf() and gets() in C language?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template