C Programming - Strings - Discussion

6. 

Which of the following function is more appropriate for reading in a multi-word string?

[A]. printf();
[B]. scanf();
[C]. gets();
[D]. puts();

Answer: Option C

Explanation:

gets(); collects a string of characters terminated by a new line from the standard input stream stdin

#include <stdio.h>

int main(void)
{
   char string[80];

   printf("Enter a string:");
   gets(string);
   printf("The string input was: %s\n", string);
   return 0;
}

Output:

Enter a string: IndiaBIX

The string input was: IndiaBIX


Kamali said: (Aug 27, 2013)  
What is the main difference between scanf and gets function?

Yogesh said: (Sep 28, 2013)  
What's the exact meaning of multi-word string?

Santhosh Pendyala484 said: (Feb 2, 2014)  
If you want to print some text including spaces use gets();
If not use scanf();

Because gets() accepts space as character whereas scanf will not accept.

In scanf function if you press space bar then the execution will terminate.

Santhoshpendyala484 said: (Feb 2, 2014)  
Try this you can get clarify.

#include <stdio.h>

int main(void)
{
char string[80];

printf("Enter a string:");
//gets(string);//scanf("%s",string);
printf("The string input was: %s\n", string);
return 0;
}

Santhoshpendyala484 said: (Feb 2, 2014)  
Multi-word string means including alphanumeric, and special characters.

Nandini said: (Oct 13, 2014)  
Anything which has to be use gets().

Anything which has to be write use puts().

Ozlem Sen said: (Jan 2, 2015)  
I have tried the code in e.g. There is a warning:

Warning: The 'gets' function is dangerous and should not be used.

In function \'main\': \'gets\' is deprecated.

gets(string);

So we must not use the function gets().

Avi said: (Dec 10, 2016)  
Right answer for this question is puts().

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.