How to Implement a Finite State Machine in C

  Переглядів 27,793

Gary Explains

Gary Explains

День тому

Following my introduction to Finite State Machines, which used Python to implement the FSM, here is a very quick video about how to implement a FSM in C. Using the same example as in the previous video, this one verifies a floating point number to see if it is good.
Previous video: • Understanding Finite S...
Code in GitHub: github.com/garyexplains/examp...
Introduction to Android app development: www.dgitacademy.com
Let Me Explain T-shirt: teespring.com/gary-explains-l...
Twitter: / garyexplains
Instagram: / garyexplains
#garyexplains

КОМЕНТАРІ: 58
@cmdlp4178
@cmdlp4178 2 роки тому
Using Two underscrores __ at the beginning is reserved for the Library/Compiler, also _t at the end might be reserved for POSIX headers.
@benhetland576
@benhetland576 2 роки тому
Yes! Gary you are a very naughty naughty boy, or you are ignorant. Actually I believe the single initial underscore is reserved for any implementer of a standard library, while the double __ anywhere in a symbol is reserved for the standard itself and the compiler implementer. Symbols starting with _E are also reserved (errno.h?), and POSIX also reserves a lot of other (macro) prefixes, except it isn't required to comply with POSIX in order to comply with the C standard.
@GaryExplains
@GaryExplains 2 роки тому
Technically according to ISO9899 section 7.1.3 is it, "All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use." Naughty me! But then again, anything after the C Programming Language (2nd Edition) is apostasy! 😜
@CedLePingouin
@CedLePingouin 2 роки тому
Glad to see the C version of that FSM. Thanks Gary.
@helge000
@helge000 2 роки тому
Thanks Gary for making this video even though it might not be your most viewed video - really interesting and informative! On the off change this might get pretty redundant now, I would wish for a golang version
@greob
@greob 2 роки тому
Nice, interesting.
@osbaldotheVtenman
@osbaldotheVtenman 2 роки тому
THANK YOU
@1MarkKeller
@1MarkKeller 2 роки тому
*GARY!!!* Good evening Professor! Good Evening fellow classmates!* Stay safe out there everyone!
@GaryExplains
@GaryExplains 2 роки тому
MARK!
@venkateshhariharan4341
@venkateshhariharan4341 2 роки тому
Love your videos on programming 😍😍
@toddfisher8248
@toddfisher8248 2 роки тому
Thanks - great video on finite state machines! anyone else’s eyes catch the inconsistent formatting around 3:36 with the missing space if found != NULL
@GaryExplains
@GaryExplains 2 роки тому
Inconsistent formatting... LOL... I guess at least it means you were paying attention.
@hajenzoo29
@hajenzoo29 2 роки тому
There is a really nice programming language called Nim that I'd think you'd like. It has a similar look to Python but with type annotations, but it compiles to C so it is in the ballpark of C performance-wise. The reason I think you'd like it is because it is better fitted for tutorials like this because it is easier to read, especially for people not very experienced in C. It's also safer as it has good abstractions over pointers and such. It does for example pass function arguments by reference if the type is larger than 24 bytes but the compiler won't let you modify it unless you explicitly mark the argument as mutable.
@VivekYadav-ds8oz
@VivekYadav-ds8oz 2 роки тому
But since it's obscure, it would be hard to follow along and get help regarding the language-related problems that might arise.
@hajenzoo29
@hajenzoo29 2 роки тому
@@VivekYadav-ds8oz Not necessarily, if you're not very good at low-level programming and is used to high-level languages like Python then I think that reading Nim is significantly easier to read than C. But sure, if you already know C it will of course be easier to read than Nim. But that would hold true for any language you don't know :) Regarding getting help, Nim has quite an active community with a forum and a bridged Discord/IRC/Matrix/Gitter server. But I'll admit that it is sparse on some tutorials, that's why it would be great if someone as educative as Gary did some tutorials on it ;)
@davidklotzbach4273
@davidklotzbach4273 2 роки тому
Now you should give an example using the State pattern en.wikipedia.org/wiki/State_pattern
@yashjoshi
@yashjoshi 2 роки тому
aight
@maxwellsmart3156
@maxwellsmart3156 2 роки тому
Using a case statement would be easier for beginners to understand and debug.
@GaryExplains
@GaryExplains 2 роки тому
Yes, maybe, but it is completely the wrong way to implement a FSM.
@maxwellsmart3156
@maxwellsmart3156 2 роки тому
​@@GaryExplains I've used switch/case statements many times and in many languages to implement a FSM. They've all worked perfectly. Your statement is infinitely wrong and bizarre, and shows some finite state thinking.
@GaryExplains
@GaryExplains 2 роки тому
@@maxwellsmart3156 Switch case is great for a few states. But it is a nightmare when you have many states. Hard to read and terrible to maintain. I suggest you reconsider your thinking.
@maxwellsmart3156
@maxwellsmart3156 2 роки тому
@@GaryExplains You're the one who says a Switch/Case is "completely wrong". I made no representation that it was the only implementation for an FSM. Now you're making up "reasons" Any code can be hard to read and terrible to maintain. Code that is visual like a switch or a data driven approach using a CSV files are easy for those coming after you. I just didn't think you were going to be so rude about my original observation because it was a valid point. And I didn't know you were going to trip over your ego.
@GaryExplains
@GaryExplains 2 роки тому
@@maxwellsmart3156 OK. Then let's stop here.
@oraz.
@oraz. 2 роки тому
woo
@VictorMistral
@VictorMistral 2 роки тому
Bad styled C, please update methods from old old style C to a bit newer C. But honnestly C is a "dead" language. C++ is alive, I would recommand using C++, even for the same code... (C++ is not an object oriented language, so no I don't say : make it in objects...) For good pratice, try to const all vars and create them with the smallest scope possible, and only declare and init them at the same time. These points makes a world of difference for re-reading the code, and in perf.
@GaryExplains
@GaryExplains 2 роки тому
In your ever so humble opinion.
@VictorMistral
@VictorMistral 2 роки тому
Test things out with godbolt and read stuff out, like I don't know, cppcoreguideline. You see that it's not just my opinion... Or run cppcheck, and/or clang-tidy. Doesn't mention "can be const" but do mentions lots of things.
@VictorMistral
@VictorMistral 2 роки тому
Except for the fact that I say that C is a dead language... No real update since C11... And there is nothing C can do that C++ can't do at least as well. But it often can do it better/cleaner.
@GaryExplains
@GaryExplains 2 роки тому
Once you start talking about "style" then it is opinion. Most projects and companies have their own "style" and corresponding style guides. As for C is dead, hmmm, according to the TIOBE Index for May 2021, C is still king and C++ is less popular than Python and Java.
@lepidoptera9337
@lepidoptera9337 5 місяців тому
@@VictorMistral I am happily writing C code on microcontrollers, even though C++ is generally available and supported. It just doesn't have any advantages over C if you are doing real-time, bare metal stuff. If anything some of it gets in the way (like its constant nagging about harmless design choices).
@powerr46
@powerr46 2 роки тому
First
@yeppiidev
@yeppiidev 2 роки тому
doesnt matter if you're first lol
@Lexitivium
@Lexitivium 2 роки тому
Start-brackets down on their own lines, never spaces after 'if', 'while', etc. But otherwise an excellent video ;-)
@GaryExplains
@GaryExplains 2 роки тому
Start-brackets on the same line, always spaces after 'if', etc. But otherwise an excellent comment ;-)
@geekionizado
@geekionizado 2 роки тому
Please do Rust, C is extremely dangerous and leads to multibillionaire vulnerabilities every day. No one should code in C nowadays.
@osbaldotheVtenman
@osbaldotheVtenman 2 роки тому
I think you're right, but I also think for the sake of teaching, C is a great language. A lot of people know it, and there something beautiful (I think) about how simple the language syntax is, yet we can do soo much with it. Cheers!
@vapourmile
@vapourmile 2 роки тому
"No one should code in C nowadays." But tens of thousands of people do. I expect to this day C is likely to be still the most in-use language for systems-level and other low-level programming. If we were going to have an argument over language choice for state machine construction I'd place TLA+ as my #1 choice or Verilog/VHDL as joint second. Third would probably be SystemC.
@lepidoptera9337
@lepidoptera9337 5 місяців тому
Pointers for a state machine? You got to be kidding, man... that's some of the worst advice I have ever seen. The very point of state machines is that they are an ABSOLUTELY SAFE programming paradigm. Inserting a pointer makes this a nightmare to debug.
@GaryExplains
@GaryExplains 5 місяців тому
Interesting comment. Which particular use of pointers are you objecting to? I made this video two years ago, so just went back and looked at the code and there doesn't seem to be any problematic use of pointers. I use a point to the current state, that is about all. Nothing complex or difficult. Standard C really.
@lepidoptera9337
@lepidoptera9337 5 місяців тому
@@GaryExplains I am using C all the time, too. I rarely use pointers and I never use pointers for important stuff like program state. I do, of course, have an industrial product out there that has been running for the last 20 years with 100% uptime and zero known bugs. I guess I am doing it wrong, then. I should have used pointers to get frequent bug reports. ;-)
@GaryExplains
@GaryExplains 5 місяців тому
🤦‍♂️ So x.y is safer than x->y thanks for letting me know.
@lepidoptera9337
@lepidoptera9337 5 місяців тому
@@GaryExplains If you don't understand the difference, then nobody can help you. ;-)
@GaryExplains
@GaryExplains 5 місяців тому
What worries me is that you think that somehow the code that the compiler emits is somehow better or safer with . rather than -> if you think that, then I can't help you.
When Booleans Are Not Enough... State Machines?
21:50
Next Day Video
Переглядів 22 тис.
ТАМАЕВ УГНАЛ МАШИНУ ВЕНГАЛБИ! Разбил CLS!
30:18
Асхаб Тамаев
Переглядів 3 млн
Computers Without Memory - Computerphile
8:52
Computerphile
Переглядів 333 тис.
Understanding Finite State Machines (or  Finite-State Automaton)
16:46
Gary Explains
Переглядів 22 тис.
Comparing C to machine language
10:02
Ben Eater
Переглядів 4,9 млн
Do This Instead Of Representing State With Booleans
12:23
Joy of Code
Переглядів 113 тис.
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Переглядів 249 тис.
Automata & Python - Computerphile
9:27
Computerphile
Переглядів 97 тис.
Floating Point Numbers - This is Where Things Get Weird!
15:11
Gary Explains
Переглядів 1,9 тис.
Arduino Tutorial on Finite State Machine Implementation
22:00