What is the most efficient way to take input of a character array or string of 25 ASCII characters using scanf?

They are not formatted, just a sequence of character. i.e. ABCDEFGHIJKLMNOPQRSTUVWXY

One more point is that, after input I have to find a sequence of character within this string, so it there any benefit in using 'string class' or should I just 'scanf and char array', cause I know my constrains.

Suggestion both considering following operation and not, is appreciated.

Edited 4 Years Ago by iamcreasy: n/a

Thanks.

Later on I used the following solution,

fgets(inputRow, sizeof(inputRow), stdin);
fflush(stdin);

Edited 4 Years Ago by iamcreasy: n/a

fflush(stdin);

Please don't do this anymore. fflush() is only defined on output streams, which means your code is subtly broken.

fflush() is only defined on output streams, which means your code is subtly broken.

What means, "only defined on output streams"?

What means, "only defined on output streams"?

Here's what the standard says:

#include <stdio.h>
int fflush(FILE *stream);

Description
If stream points to an output stream or an update stream in which the most recent
operation was not input, the fflush function causes any unwritten data for that stream
to be delivered to the host environment to be written to the file; otherwise, the behavior is
undefined.

Note that input streams and update streams where the most recent operation was input fall into the "otherwise" part of the definition. stdin is an input stream, so fflush(stdin) invokes undefined behavior.

... undefined behavior.

Got it.

Is it possible to know what is the undefined behavior? or dose it depend on the context?

Is it possible to know what is the undefined behavior?

Undefined behavior is by definition undefined. Anything can happen. I've heard cases of undefined behavior frying hardware, or corrupting system files, so it's far from harmless. And I suspect that's why people always look for descriptions of what might happen, they want to weigh the risks.

Comments
Woo!
This question has already been answered. Start a new discussion instead.