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.

Recommended Answers

All 7 Replies

For me I'd use a loop to store the character in an array

Thanks.

Later on I used the following solution,

fgets(inputRow, sizeof(inputRow), stdin);
fflush(stdin);
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.

commented: Woo! +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.