Wednesday, July 17, 2013

gets() as Input Function in C

In C, along with scanf(), we have another function which is widely used for taking inputs. That is gets(). But the difference between scanf() and gets() is that gets() takes only the Strings or char arrays for the input. If you observe the prototype of the gets() function, you can understand it easily.
char   *_Cdecl gets(char *__s);
You can understand more clearly if you observe the following program.

Example:

  1. #include<stdio.h>
  2. main()
  3. {
  4. char str[50];
  5. printf("Enter some text");
  6. gets(str);
  7. printf(str);
  8. return 0;
Even if you enter the int values, it is considered as characters and is taken as a string. Thus gets() function is used to take string inputs.