r/C_Programming • u/Viable-public-key • Mar 29 '26
Question Taking arbitrary length input from the keyboard
Hello fellow C programmers and enthusiasts. I picked up C recently, and can't overcome the problem in the title.
Scanf() needs an already declared array, with fixed size. If user enters more than that, it could lead to buffer overflow and is undefined behaviour. It might work on my machine despite the undefined behaviour, but I'd rather avoid that.
There is a %ms format specifier, but it's only for MacOS and Linux, doesn't work on windows (source : Gemini). So I also want to avoid that as well.
I want to enter the input in one go, so dynamically allocating and reallocating memory is out of option. Is there a way to get arbitrary length of input data from keyboard, or declaring an Array of arbitrary length in C?
I thought since scanf() stores the input in stdin buffer, if I could get the stdin buffer size somehow then I can work around it, but couldn't find a way to interact with it.
Is there a way to achieve this?
Edit: getchar() is the way to go, thanks for the replies everyone!
Edit: Dynamically allocating is in fact not out of option, as I can read the whole string at once into the stdin buffer, and interact with that while dynamically allocating my buffer array.
32
u/Initial-Elk-952 Mar 29 '26 edited Mar 29 '26
Scanf is generally frowned upon. You might try sscanf aftering reading into a known size buffer.
The posix getline() is kind of nice, because it will realloc the buffer as needed.
Also, normal terminals have limits on line length because the underlying PTYs do.
If the program dynamically allocates, this doesn't mean the user will have to retype. You can't take an unbound amount of memory without ever dynamically allocating. The two things are impossible. You can statically allocate fix upper bound, or dynamically allocate as you need (up to as much as the machine will let you allocate).
Realistically, the PTY line max will is good upper bound.
9
u/glasket_ Mar 29 '26
Check out the Beginner's guide away from scanf first.
Second, you can use a fixed buffer with a dynamic allocation for the actual storage. You create a
char arr[SIZE], use that forfgets(arr, SIZE, stdin), and then insert into a pointer to amallocblock. You grow the allocation when necessary usingreallocby tracking the size and how many bytes have been inserted.8
u/questron64 Mar 29 '26
Don't use scanf for this, there are other input functions. If the input is terminated by a newline then use fgets. Read up to the maximum size of your buffer, and if the last character read wasn't a newline then realloc the buffer and call fgets again. Be sure to check for fgets returning an error or this will loop forever on EOF.
6
u/RetroGameMaker Mar 29 '26
The way I used to do it is to read every key press with a function like getch(). With a single character read, you can reallocate your buffers easily
1
u/Educational-Paper-75 Mar 29 '26
You could switch to character input mode because afaik the default is line input mode, and use getchar() in a loop to read each character and dynamically increase dynamic character array when need be.
1
u/KineticAlpaca362 Mar 30 '26
c is very cool
3
u/realhumanuser16234 Mar 29 '26
you can use
%ms, even on windows, its just not msvc compatible1
u/GoblinToHobgoblin Mar 29 '26
What is the use case here?
2
u/SmokeMuch7356 Mar 30 '26
Unknown input length pretty much requires using a dynamically-allocated buffer. A common approach is something like the following:
which you would use as
You could elect to not fail on a
reallocfailure and return what's been read so far, but you'll want an extra ouput parameter to indicate that.1
u/runningOverA Mar 30 '26
getc() is what you need. Read one char by one char and grow as you need.
And don't worry about performance. Other line entry functions use getc() internally.
1
u/kabekew Mar 29 '26
On windows just process the WM_CHAR message and add it to your array one key at a time. Computers don't have infinite memory so there's no way to allow true arbitrary length. You'll either have to reallocate the array or not add further input if it reaches the max size.
0
u/vowelqueue Mar 29 '26
Use fgetc or fgets. There’s a cap to how much input they’ll give you at a time, so you can allocate more space to fit the input as needed.
-6
u/grok-bot Mar 29 '26
On top of what the others have said, if you are okay with not just using the stdlib you can use either the
readlineoreditlinelibrary, which provide a bash/nano/emacs-like experience.25
u/[deleted] Mar 29 '26
[removed] — view removed comment