Home  >  Article  >  Backend Development  >  Difference between scanf() and gets() in C (code example)

Difference between scanf() and gets() in C (code example)

藏色散人
藏色散人Original
2019-03-25 15:00:332887browse

Difference between scanf() and gets() in C (code example)

scanf() function

It is used to read input (character, character) from standard input (keyboard) string, numeric data).

It is used to read input until a space, newline character or end of file (EOF) is encountered.

For example, see the following code:

#include <stdio.h> 
int main() 
{ 
    char str[20]; 
    printf("enter something\n"); 
    scanf("%s", str); 
    printf("you entered: %s\n", str); 
  
    return 0; 
}

The input here will be provided by the user and the output will be as follows:

输入: Geeks for Geeks
输出: Geeks

输入: Computer science
输出: Computer

gets() function

It is used to read input from standard input (keyboard). It is used to read input until a newline character or end of file (EOF) is encountered.

#include <stdio.h> 
int main() 
{ 
    char str[20]; 
    printf("enter something\n"); 
    gets(str); 
    printf("you entered : %s\n", str); 
    return 0; 
}

Here the input will be provided by the user as follows

输入: Geeks for Geeks
输出: Geeks for Geeks

输入: Computer science
输出: Computer science

The main difference between them is:

scanf() reads the input until it is encountered whitespace, newline, or end-of-file (EOF), whereas gets() reads input until a newline or end-of-file (EOF) is encountered, gets() does not stop reading input when a space is encountered but treats the space as String. scanf can read multiple values ​​of different data types, while gets() can only get string data.

The difference can be displayed in tabular form as follows:

SCANF() GETS()
When scanf() is used to read string input, it stops reading when it encounters a space, newline character, or end of file When gets() is used to read When taking input, it stops reading input when it encounters a newline character or End Of File.
While it treats whitespace as a string, it does not stop reading input when it encounters a whitespace.
It is used to read input of any data type It is only used for string input.

How to use scanf() to read the complete sentence of the user

Actually we can use scanf() to read the entire String. For example, we can use %[^\n]s in scanf() to read the entire string.

#include <stdio.h> 
  
int main() 
{ 
  
    char str[20]; 
    printf("Enter something\n"); 
  
    // Here \n indicates that take the input 
    // until newline is encountered 
    scanf("%[^\n]s", str);  
    printf("%s", str); 
    return 0; 
}

The above code reads the string until it encounters a newline character.

Example:

输入: Geeks for Geeks
输出: Geeks for Geeks

输入: Computer science
输出: Computer science

Related recommendations: "C Tutorial"

This article is about the relationship between scanf() and gets() in C The differences are introduced, I hope it will be helpful to friends in need!

The above is the detailed content of Difference between scanf() and gets() in C (code example). 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