Static Local Variables | C Programming Tutorial

  Рет қаралды 8,466

Portfolio Courses

Portfolio Courses

2 жыл бұрын

Examples of using static local variables in C. Source code: github.com/portfoliocourses/c.... Check out www.portfoliocourses.com to build a portfolio that will impress employers!

Пікірлер: 24
@flirtyemy042
@flirtyemy042 Жыл бұрын
Straight to the point and easy to understand videos. Earned a sub
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Thank you Chukwuemeka! :-D
@sphanie
@sphanie 2 жыл бұрын
really helpful, simple explanation!!
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
I'm glad it was helpful for you! 😀
@justcurious1940
@justcurious1940 Жыл бұрын
Great video Kevin, Thanks .
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome! :-)
@Anonymous-XY
@Anonymous-XY 7 ай бұрын
Thank you Kevin.
@stevehageman6785
@stevehageman6785 Жыл бұрын
Nice tutorials.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Thank you Steve! :-)
@walterzaq1
@walterzaq1 4 ай бұрын
Thanks!
@qneqne8440
@qneqne8440 Жыл бұрын
Why not declare calls as a global variable before int main (void) and calls++ later on in the code. Would that cause problems?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
We try to avoid global variables when possible because global variables can lead to more difficult to trace bugs. When a function is limited to only working with its own variables, and we have a bug that involves one of those variables, we know to look in that function for the bug. But if many functions work with a variable, and there is a bug involving that variable, then we need to look at many functions to find the bug (which takes more time, and is more confusing to figure out... which function caused the problem, when was it called, etc.). By making the variable a local static variable, only the one function can access it, which is better for debugging purposes than using a global. Hopefully this helps! :-)
@qneqne8440
@qneqne8440 Жыл бұрын
@@PortfolioCourses It does help. Thank you very much for such a thorough explaination
@PortfolioCourses
@PortfolioCourses Жыл бұрын
@@qneqne8440 You're welcome! 🙂
@bsykesbeats4013
@bsykesbeats4013 Жыл бұрын
Is there any way to retrieve the current value of the static variable? In Effective C by Robert Seacord, one of the exercises is to "Add a retrieve function to the counting example form Listing 2-6 to retrieve the current value of the counter". Listing 2-6 is just an increment function with a static variable btw: void increment(void) { static unsigned int counter = 0; counter++; printf("%d ", counter); } How could I add a function to retrieve the value if the scope is limited to the increment function? Wouldn't returning an int from increment() be the only way? (But if you did that, you wouldn't be able to retrieve the current value without incrementing it).
@PortfolioCourses
@PortfolioCourses Жыл бұрын
If the increment function were to return a pointer to the static int, then I suspect the other function could use the pointer to retrieve the value by dereferencing it. Though I've never tried that before. It's the only thing I can think of though. 🙂
@bsykesbeats4013
@bsykesbeats4013 Жыл бұрын
@@PortfolioCourses Gotcha. Any benefit of returning a pointer instead of an int? ... only other option I guess is to rewrite increment() to write to a log like you did and then write retrieve() to read from the log I guess...I appreciate the response.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
@@bsykesbeats4013 Not in particular that I can think of. 🙂 Maybe if other functions also wanted to modify that variable's value, then a pointer would be useful.
@fifaham
@fifaham Жыл бұрын
Very helpfull presentation, also a time stamp maybe added as per time frame in @5:12
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm glad you enjoyed it! :-) And agreed a timestamp would be a good idea when logging.
@user-xy1ji1dl6y
@user-xy1ji1dl6y Жыл бұрын
شرح عظيم
@PortfolioCourses
@PortfolioCourses Жыл бұрын
:-) على الرحب والسعة
@user-el3sj3kh2b
@user-el3sj3kh2b 3 ай бұрын
Thanks!
@PortfolioCourses
@PortfolioCourses 3 ай бұрын
Oh wow thank you so much for the super thanks!!! :-D
strcpy() and strncpy() functions | C Programming Tutorial
8:15
Portfolio Courses
Рет қаралды 36 М.
array vs &array Pointers Difference Explained | C Programming Tutorial
17:38
I Can't Believe We Did This...
00:38
Stokes Twins
Рет қаралды 123 МЛН
Scope of Variables - Local vs Global
11:12
Neso Academy
Рет қаралды 392 М.
struct Basics | C Programming Tutorial
24:44
Portfolio Courses
Рет қаралды 132 М.
Bitwise Operators | C Programming Tutorial
12:19
Portfolio Courses
Рет қаралды 22 М.
What is a static variable?
4:45
CodeVault
Рет қаралды 40 М.
Static for Classes and Structs in C++
9:12
The Cherno
Рет қаралды 345 М.
Introduction to Pointers | C Programming Tutorial
24:42
Portfolio Courses
Рет қаралды 106 М.
Dynamic Memory Allocation | C Programming Tutorial
31:51
Portfolio Courses
Рет қаралды 79 М.