How to Implement a Finite State Machine in C

  Переглядів 7,981

Gary Explains

4 місяці тому

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: www.youtube.com/watch?v=2OiWs...
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

КОМЕНТАРІ
Mark Keller
Mark Keller 3 місяці тому
*GARY!!!* Good evening Professor! Good Evening fellow classmates!* Stay safe out there everyone!
Gary Explains
Gary Explains 3 місяці тому
MARK!
Todd Fisher
Todd Fisher 4 місяці тому
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
Gary Explains
Gary Explains 4 місяці тому
Inconsistent formatting... LOL... I guess at least it means you were paying attention.
VictorMistral
VictorMistral 4 місяці тому
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.
Gary Explains
Gary Explains 4 місяці тому
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.
VictorMistral
VictorMistral 4 місяці тому
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.
VictorMistral
VictorMistral 4 місяці тому
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.
Gary Explains
Gary Explains 4 місяці тому
In your ever so humble opinion.
oraz
oraz 4 місяці тому
woo
david klotzbach
david klotzbach 4 місяці тому
Now you should give an example using the State pattern en.wikipedia.org/wiki/State_pattern
Maxwell Smart
Maxwell Smart 4 місяці тому
Using a case statement would be easier for beginners to understand and debug.
Gary Explains
Gary Explains 4 місяці тому
@Crescendo I did say why and I was told I was just making up reasons. So, best not to go further with this.
Crescendo
Crescendo 4 місяці тому
@Gary Explains Some people just like to argue on the internet. Perhaps it would help if you were to state your case for why a switch/case/default construct is the wrong way to implement an FSM?
Gary Explains
Gary Explains 4 місяці тому
@Maxwell Smart OK. Then let's stop here.
Maxwell Smart
Maxwell Smart 4 місяці тому
@Gary Explains 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.
Gary Explains
Gary Explains 4 місяці тому
@Maxwell Smart 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.
Njul
Njul 4 місяці тому
Nice, interesting.
Cédric Floquet
Cédric Floquet 4 місяці тому
Glad to see the C version of that FSM. Thanks Gary.
Lexitivium
Lexitivium 4 місяці тому
Start-brackets down on their own lines, never spaces after 'if', 'while', etc. But otherwise an excellent video ;-)
Gary Explains
Gary Explains 4 місяці тому
Start-brackets on the same line, always spaces after 'if', etc. But otherwise an excellent comment ;-)
Daniel Helgenberger
Daniel Helgenberger 4 місяці тому
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
SpaceFace102
SpaceFace102 4 місяці тому
THANK YOU
Lucas Zanella
Lucas Zanella 4 місяці тому
Please do Rust, C is extremely dangerous and leads to multibillionaire vulnerabilities every day. No one should code in C nowadays.
Crescendo
Crescendo 4 місяці тому
"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.
SpaceFace102
SpaceFace102 4 місяці тому
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!
The PhilomathsApprentice
The PhilomathsApprentice 4 місяці тому
aight
Venkatesh Hariharan
Venkatesh Hariharan 4 місяці тому
Love your videos on programming 😍😍
cmdLP
cmdLP 4 місяці тому
Using Two underscrores __ at the beginning is reserved for the Library/Compiler, also _t at the end might be reserved for POSIX headers.
Gary Explains
Gary Explains 4 місяці тому
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! 😜
Ben Hetland
Ben Hetland 4 місяці тому
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.
hajenzoo29
hajenzoo29 4 місяці тому
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.
hajenzoo29
hajenzoo29 4 місяці тому
@Vivek Yadav 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 ;)
Vivek Yadav
Vivek Yadav 4 місяці тому
But since it's obscure, it would be hard to follow along and get help regarding the language-related problems that might arise.
marsch
marsch 4 місяці тому
Check XState on github, it's awesome! FSM on the web.
powerr46
powerr46 4 місяці тому
First
Yeppii
Yeppii 4 місяці тому
doesnt matter if you're first lol
Understanding Finite State Machines (or  Finite-State Automaton)
16:46
Gary Explains
Переглядів 8 тис.
Pointers in C / C++ [Full Course]
3:47:23
freeCodeCamp.org
Переглядів 794 тис.
Зоопарк
3:01
10AGE - Topic
Переглядів 201 тис.
Мой кот точно пытается меня убить
19:27
TheBrianMaps
Переглядів 2,9 млн
Understanding Mesh Networking (feat. MikroTik Audience)
17:12
Gary Explains
Переглядів 12 тис.
Microsoft is Unrepentant about Windows 11 - Removes Checker Tool
9:20
Gary Explains
Переглядів 287 тис.
Piccolo OS: Write Your Own Multitasking OS
28:29
Gary Explains
Переглядів 12 тис.
How Computers Synchronize Their Clocks - NTP and PTP Explained
14:13
Gary Explains
Переглядів 20 тис.
32-bits is DEAD!
19:50
Gary Explains
Переглядів 47 тис.
RISC-V vs x86 - History and Key Differences Explained
23:36
Gary Explains
Переглядів 35 тис.
Build Your Own Supercomputer Using a Jetson Mate
11:35
Gary Explains
Переглядів 64 тис.
Wi-Fi Glossary and Terminology - Jargon Buster
28:06
Gary Explains
Переглядів 10 тис.
Зоопарк
3:01
10AGE - Topic
Переглядів 201 тис.
Мой кот точно пытается меня убить
19:27
TheBrianMaps
Переглядів 2,9 млн
Я ПЕРЕЕЗЖАЮ!!! room tour, взрослая жизнь
13:32
AdushkinaVlogs
Переглядів 302 тис.
Смотрим "Пацанки" 6 сезон 1 серия
2:15:49
Дарья Каплан
Переглядів 266 тис.
МастерШеф 11 сезон. Выпуск 3 от 11.09.2021
2:32:59
МастерШеф
Переглядів 487 тис.