scanf() and gets() in C

When and Where to use scanf() and gets() in C

when_and_where_to_use_scanf()_and_gets()_in_c

scanf()

scanf() allows us to enter data from keyboard that will be formatted in a certain way.

The genaral form of scanf() statement is as follows :

scanf("format string",list of addresses of variables);

For example:

scanf("%d %f %c",&c &a &ch);

Note that we are sending addresses of variables (addresses are obtained by using '&' the 'address of' operator) to scanf() function. This is necessary because the values received from keyboard must be dropped into variables corresponding to these addresses. The values that are supplied through the keyboard must be separated by either blank(s), tab(s), or newline(s). Do not include these escape sequences in the format string.

gets()

gets() receives a string from the keyboard. scanf() has some limitations while a receiving a string of characters.There is no way to enter a multi-word string into a single variable. gets() function gets a string from the keyboard and it is terminated when an Enter Key is hit.Thus, spaces and tabs are perfectly acceptable as part of the input string. gets() gets a newline (\n) terminated string of characters from the keyboard and replaces the \n with a \0.

main()
{
    char player[40];

    puts("Enter name");
    gets(player); /*sends base address of array */
    puts("Happy plying !");
    puts(player);
}

Following is the sample output:

Enter name
Kapil Dev
Happy Playing !
Kapil Dev

Difference between scanf()and gets()

The main difference between scanf() and gets() is:

  1. scanf() reads input until it encounters whitespace, newline or End Of File(EOF) whereas gets() reads input until it encounters newline or End Of File(EOF), gets() does not stop reading input when it encounters whitespace instead it takes whitespace as a string.
  2. scanf()can read multiple values of different data types whereas gets() will only get character string data. gets() can be used to read one string at a time.


If you like dEexams.com and would like to contribute, you can also write your article here or mail your article to admin@deexams.com . See your article appearing on the dEexams.com main page and help others to learn.


Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Python if , elif and else

Python Conditions and If statements

  • 0
Python for beginners

Learning Python Part 1

  • 3
Struct Alignment and Padding

Struct Alignment and Padding in C++ And C

  • 0
Friend function

Friend function C++

  • 0
Pointers

C++ Pointers

  • 0
Structures

C++ Structures

  • 0
Types of Inheritance in C++

Inheritance and access specifiers C++

  • 0
Java date pattern

Java Date Pattern Syntax

  • 0
Java Date and Calendar

Java Date formats

  • 0
JAVA Data Type

Data types in Java

  • 0
Java unreachable code

Unreachable Code Error in Java

  • 0

Post Comment

Comments(0)

WEB TECHNOLOGY

Articles

×

Forgot Password

Please enter your email address below and we will send you information to change your password.