what was the syntax to input strings with more than one word i.e with space in between through scanf() not gets()
char name[50]; printf("Enter your full name: "); scanf("%[^\n]",name); The %[^\n] conversion asks scanf( ) to keep receiving characters into name[ ] until a \n is encountered. It's a character-set, and starting with ^ means accept anything but that.
Inputting Multiple ValuesIf you have multiple format specifiers within the string argument of scanf, you can input multiple values. All you need to do is to separate each format specifier with a DELIMITER - a string that separates variables.
Input a multi-word string using scanf() To overcome this, we need to use the format specifier, “%[^\n]s” . This means that the program will receive all the words in the string, from the beginning up to a \n . scanf("%[^\n]s", stringName); main.
The function gets() is used for collecting a string of characters terminated by new line from the standard input stream stdin. Therefore gets() is more appropriate for reading a multi-word string.
Is it
scanf("%[^\t\n]",string);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With