Search code examples
cinputscanfc89

Require certain words to appear using scanf


I want scanf to accept only certain word, for example, if I want to accept either foo or bar from the user, I will write something like:

scanf(/*more input specifiers before and after*/ "%s[foo/bar]", demo);

This will return 1 and assign demo with user input, only if the user have entered either foo or bar.

How can I achieve such functionality from scanf?

Note: should be compatible with C89 and should work when using fscanf.


Solution

  • The scanf() function reads the data from the stdin and stores it according to the parameter format into the locations pointed by the additional arguments. The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string.

    Another work of scanf() is that it does returns the number of successfully read items. It has no any working ability to compare the given string like foo /OR/ bar.