The Static Keyword in C

preview_player
Показать описание
---

Students and static occasionally run into trouble, either because they use it when they shouldn't, don't use it when they should, or guess and end up right but don't understand why.

This video explains what "static" means in C (not C++ or Java) and how it changes a variable's scope and sometimes it's persistence.

Enjoy.



***

Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.

About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.

More about me and what I do:



***

Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.

About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.

More about me and what I do:

To Support the Channel:
+ like, subscribe, spread the word

Want me to review your code?

You can also find more info about code reviews here.
Рекомендации по теме
Комментарии
Автор

Here's a small mnemonic tip
You can think of "static" like "static electricity"... a static local variable "sticks" in memory across function calls kind of like a static-electrically charged piece of plastic might stick to your finger. And a static global variable or static function "sticks" to the translation unit it was put in, and it doesn't work in other places

unchayndspersonalaccount
Автор

Static int declared inside a function makes it a ram variable, as normally it's a scratch temporary register (R2 etc) As it's ram variable it will keep its value when function is called again.

tonysofla
Автор

this is fantastic! please continue making videos!!!

jti
Автор

Hello Jacob,
there are also other uses of static local variables, I have been using them to implement singletons in my C programs. They are very useful for this technique and allows you to not make use of global variables (that are often prohibited or are being very limited in their usage in a project for x reason). To show an example, you could use a function that returns an address to a static struct. Then you could assign and access this struct across your whole program in a very flexible way by calling the function, and dereferencing a pointer to the data you are interested in.
best regards,
Karim

karimdhrif
Автор

It should be pointed out that using static local variables makes that function non-reentrant and thread-unsafe, which is the same problem as global variables. In practice this doesn't mean much unless you intend to call the function from a signal handler or multiple threads. But you can easily make the counter function reentrant and thread-safe by using a static atomic counter.

sourestcake
Автор

I use static functions for private ones.

you're writing a function that does X, but it also needs to do Q, pop Q into a static function.

Primarily, static functions are best used for defining an API.

shekharmaela
Автор

"Making a local variable static" does not mean anything about its lexical visibility. All local variables are only visible (by name) by the function itself. Making a local variable static affects only its lifetime. It will live and retain its value across function calls.

sverkeren
Автор

OMG, finally a reasonable explanation!
Thank you!

mwein
Автор

Very Nice video and nice explanation. I have a doubt over here around: 2:40 -2:55. Please help me to clear it.(Disclaimer: I am not a C expert) Any variable declared in the scope of a function is local to that function be it static or not and any other function will not be aware of it anyways. Whereas static allows initialisation only once and no matter how many time the function is called it will not reinitialise its value and will retain the value from previous call. Though this functionality cannot be achieved by non-static variable. Please correct me if I am wrong.

sun
Автор

your videos are beyond amazing ! great quality

zawette
Автор

@JacobSorber Thank for the video. Will you please explain the key differences in 'global variable' and 'static global variable', yes global variable can be accessed outside the file using extern but are they same if extern is not used?

navalsaini
Автор

When you learn C storage classes, it makes total sense about static

k_gold
Автор

I am trying to understand this keyword. For now, the analogy that seems to work with static variables in functions is to compare them with files and users. The same way a user can have permission to read, write and execute a file, a function can have permission to see and modify a variable. Variables that are static are private to the function.
I haven't come across static functions yet, so I don't know if the analogy holds with them.

Thanks for the video!

pablo_brianese
Автор

Ya my first programming classes were in java was the static keyword in C was pretty weird to me.

seal
Автор

Things I got from this: 1. static in C isn't what I thought it was (I thought it was what it was in Java) 2. static appears to do nothing i.e. if you use it in a function on a variable, it has the scope of the function, if you use it outside of a function, it has global scope 3. If you use it on a function, something about translation units... I've never heard of translation units.

WistrelChianti
Автор

But aren't function variables always visible only inside the function?

RAMBVI
Автор

Hi jacob, can you please make a video on auto storage class..i am bit confused.

abubakarshaik
Автор

amazing could !! you pleas explain where the weak key word is used

brahimdjadir
Автор

and this is not the only C/C++ keyword that has multiple meanings depending of its context :D

simonhrabec
Автор

I'm thinking that is one key issue in learning programming (for me): encountering programming jargon that is not effectively revealing of its true purpose. One day I just sat down and thought to myself why the hell would anyone prefer a term like `private` in modelling a hierarchy of objects and then match it with another term like `method`. If the idea was to describe the «unique capabilities» of an object, why utilize the terms `private` and `methods` instead of just saying `capabilities` `unique` to the object? Ugh.

chevalierdeloccident