r/C_Programming • u/Rogaev • Oct 19 '26
Question Text input doesn't make any sense to me
Disclaimer: I have basically no idea what most programming terms mean.
For a while now (a couple months as of now), I've been trying to create an open source calculator. Throughout the development process, there has been a very large problem. I don't know how exactly to scan text and look for different commands. The scanf function (among other options) seems to just look for a single "phrase" throughout the entirety of whatever input it receives. I just don't understand how people can do this as easily as they do. Many tutorials seem to not really cover this.
I've tried to come up with some sort of system to account for this. For example, a function that looks for all number values and determines the value type (like a float, integer, or double), and then matches character combinations to known commands (I haven't fully committed to such a method yet).
The way that ffmpeg handles input is an example of this. I don't understand how I can put as many spaces as I want between "ffmpeg" and some other command and still get the same result.
Apologies for such a long post. I'm just not really sure how people can do this. I'm almost certain that I'm missing something.
Thank you in advance
27
u/zhivago Oct 19 '26
Start with fgets() to read a line.
Then break it down and handle it.
17
u/ReallyEvilRob Oct 19 '26
What you are trying to do is tokenize your input. The scanf() function is not the best tool for this. You need to read input into a buffer and then use strtok() to split the input into tokens. Once you've identified the tokens, you'll need to parse them into something like a binary tree to parse your operators and operands.
7
u/goilabat Oct 19 '26
ffmpeg you mean while doing ffmpeg --file something --option something ?
That's not ffmpeg doing that's the shell ffmpeg receive a array of char[] in this case {"--file", "something", "--option", "something"} all null terminated and also received 4 as a parameter 5 in truth but the first one is the current path
That why generally you have a main function that take (int ac, char **av)
But that wasn't the question so how do we do these thing other comment said fgets that get line from file read generally for bigger file that get a number of byte from a file
And after you parse in a loop doing a state machine kinda design like
4
u/Classic-Rate-5104 Oct 19 '26
You can try to tokenize the input using strtok, but even better, this kind of problems require a lexical analysis. So have a look for tools like flex, which generates code based on syntax description
4
u/ArtOfBBQ Oct 19 '26
It seems like every course out there starts with scanf() because the teacher wants to have a way to make your program interactive. I suspect so because a lot of questions here end up being about scanf
IMHO this is just bad teaching. Scanf is an insanely complex library (code written by other people) function. The teacher I guess is hoping that you will be interested in scanf() enough to memorize how to use it, but not interested enough
to wonder how it does what it does, which is actually filtering the students for a horrible property. The students who ask "how is this happening, though?" are actually the ones with the most potential, but you screw them over by putting this impossible mystery box that distracts from the simple stuff they should be learning
IMHO,, in the beginning you should hardcode "variables" in your program and just compile (turn code into program) multiple times with different values. Don't even use scanf
int hardcoded_flag = 1
if (hardcoded_flag == 1) {
// do stuff
} else {
// do other stuff
}
now you compile and run your program and observe that it "does stuff". Change hardcoded_flag to 0, compile again, and observe that it does "other stuff". You don't need scanf, just experiment in this way
3
u/ChickenSpaceProgram Oct 19 '26
other folks have recommended fgets(), which is a good idea. for handling operator precedence (the fact you do * before +, for example), look into the shunting yard algorithm or Pratt parsing.
3
u/enlightment_shadow Oct 23 '26
Simple answer: use libraries Hard answer (and honestly what I think you need): learn about formal languages and automata, parsers and system design
Of course you can do both
2
u/codeguru42 Oct 19 '26
This is called "string parsing". Hopefully that gives you a phrase to google
3
u/gm310509 Oct 19 '26
What you are talking about is tokenisation and parsing.
tokenisation is basically breaking an input (which could be part of a line of text, a whole line of text or multiple lines of text - it depends upon your rules) into tokens and then parsing them for valid sequences of tokens.
For example consider this statement (for a language I just made up):
this might be broken down into a list of tokens as follows:
In my made up language whitespace isn't important and is thus ignored by the tokeniser - unless it is embedded in a string.
You can then analyse the tokens to see if they are in an order that makes sense. For example, if you see a PRINTLN_TOKEN, then it must be followed by a OPEN_PAREN_TOKEN. After the OPEN_PAREN_TOKEN on the "println" parse path, there may be a few options. For example it might accept a String (as shown), a variable, or a constant - but if the parser identified, for example, a MULTIPLY_TOKEN, then that would be invalid and cause an error to be generated.
You might find this guide to be helpful: https://craftinginterpreters.com/
You can do something much simpler though. For example, if you made a rule that every token must be seperated by whitespace, you could use the C
strtokfunction to return the individual words separated by spaces.Here is some code from one of my How To videos (for Arduino), where I do this simple approach:
In the above code:
Serial printlnwhich you probably won't be familiar with, but it just prints the supplied message or value of the variable.inBufis a custom class I created to maintain a C String.tokensarray.processTokensfunction looks at the individual words and causes the input to be actioned.All the best with it.
1
u/YakubReddit Oct 23 '26
Welcome to the great world of language programming Here you have to handle with the things such as:
1
u/Ok-Lifeguard-9612 Oct 20 '26
"I have basically no idea what most programming terms mean"