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.

Dani AI

Generated

For — practical options and pitfalls when reading a fixed 25‑character ASCII sequence.

If the input contains no whitespace (your example ABC...Y), a width‑limited scanf is simple and safe: it prevents overflow and NUL‑terminates the buffer. If you need to discard the rest of the line afterwards, consume characters until '\n' (do not use fflush(stdin)). For inputs that must include spaces or you need exactly 25 bytes (including whitespace), read raw bytes and NUL‑terminate.

Example (non‑whitespace token, safe):

char buf[26]; /* 25 chars + NUL */
if (scanf("%25s", buf) == 1) {
    int c;
    while ((c = getchar()) != '\n' && c != EOF) { } /* clear remainder of line */
}

Example (exactly 25 bytes, including spaces):

char buf[26];
size_t n = fread(buf, 1, 25, stdin);
buf[n] = '\0'; /* safe NUL-termination */

For substring search in C, use strstr for NUL-terminated strings. If you are in C++ prefer std::string and std::string::find — they’re safer and more convenient. For very small strings (25 chars) algorithmic complexity doesn’t matter; a simple strstr or std::string::find is fine. Only consider KMP/Boyer‑Moore when working with much larger texts or many repeated searches.

Notes tied to the thread:

  • ’s per-character loop works and is useful when you must validate or filter each character as you read it.
  • is correct: fflush on stdin is undefined behavior; don’t rely on it. Use a getchar loop or read exactly the number of bytes you want.
  • Always allocate one extra byte for the terminating NUL and check return values from I/O functions.

Choose safety and clarity over micro-optimizations. For 25 characters, readable, well-checked code is effectively “the most efficient” in practice.

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.