Home
Username:
Password:
Advanced C Programming Tutorials

Special Keywords / static




Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.


Learn More

Subtitles of the Movie

The static keyword seems to be the most misunderstood keyword in the language. I think the reason for this is that it does different things depending on how it's used. And it's used in the C++ languages so very different from C. I'm just going to talk about C. By default, all variables declared outside of functions have external linkage. That is space that's set aside for them by the compiler and their location is reported to the linker, which makes it accessible from all source files in the same program. However, if it is declared as static as X is in this example, it's location is not reported to the linker so it's available only to the functions within this source file. In this example the static keyword has been moved to the function declaration and it works the same way. The name of the function is no longer reported to the linker so it's available only to other functions within this source file. This is the main thing that the static keyword is used for; re-scoping things that by default have external linkage to having only internal linkage. But there is another purpose. The variables declared inside a function don't have linkage of any sort. They can not be accessed from outside the function but the static keyword does have an affect on them. By default, all variables inside a function are automatic. They come into existence when the function is called. However, if you declare a variable inside a function as being static, the compiler makes a space for it and it exists for the life of the program. In this example the static value of A is declared and given an initial value of five. That's done by the compiler so it only happens once. Inside the function the value of A is displayed. Then it's doubled. An automatic variable does not retain the new value but a static one does. As you can see, the function is called twice and this is what happens. The first time through the function the initial value of the variable is displayed but also that first time through the value is doubled and on the second call the variable retained its value. That's what static does. It creates a place in memory for the variable so it does not vanish between function calls; it retains its value. Even though the execution of the function looks like it passes through the setting of the initial value, it really doesn't happen. That's because the initial value was set once by the compiler. Any subsequent setting of the value erases the compiler's value setting and replaces it with one of its own.

Tutorial Information

Course: Advanced C Programming
Author: Arthur Griffith
SKU: 33965
ISBN: 1-935320-24-6
Release Date: 2009-01-30
Duration: 5.5 hrs / 82 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available