Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Shouldn't storage classes be taught early in a C class or book?

I notice that a lot of books, even some of the better ones, covert it toward and end of the book and some books just add it as an appendix. I would teach it together with variables. This is so foundational and I think unfortunately many do not make it that far in a book. Now that auto has a different meaning (vs being optional) it may confuse people that didn't realize it has always been there.

for example: C Programming: A Modern Approach

18.2 Storage Classes 401

Properties of Variables 401 The auto Storage Class 402 The static Storage Class 403 The extern Storage Class 404 The register Storage Class 405 The Storage Class of a Function 406 Summary 407

@DeadMG, I fear I didn't state my question properly. Everyone always uses storage classes. Every single variable in a C program has a storage class. The thing is that some of the keywords were made optional because, for example, int i; = auto int i; The reason I ask is because I've seen code so bad where variables are declared at the top of a program to be made available to a function and not lose its value when it goes out of scope but only one function needed it. Then, by default the variable's storage class is "extern" int i; but they may not know since extern is assumed by the compiler. What they should have done is to declare it in the function that needed it as static, static int varToKeepValue; that way you achieve the desired goal of preserving the variable's value when the function goes out of scope throughout the execution of the program, while also making the code more readable.

btw: I'm interested in this, please email me your thoughts if this question gets closed.

On another note: you have a very odd approach... keep learners ignorant? I think is because they don't have that information that the language is not used properly. It's not like not letting kids run with knives. My approach is, learn the language inside and out that will let you use it properly.

share|improve this question

closed as not constructive by MichaelT, gnat, Martijn Pieters, thorsten müller, Mark Booth Mar 7 '13 at 18:09

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. If this question can be reworded to fit the rules in the help center, please edit the question.

About the "letting kids run with knives" metaphor - while I agree it should be taught - it actually kinda is the same thing though: we teach kids not to run with knives and are a bit worried when we see doing it, but we know we shouldn't mind so much when it's an adult (but are still slightly worried, for other reasons). Well, same thing. Beginners may not have the same maturity required to assess when it's a crappy and possibly self-damaging idea, and with more experienced users - though you are more enclined to let it slip - it can be a red-flag in some situations. Not that bad an analogy... –  haylem Jul 9 '12 at 22:44
Also, regarding the example of the variable being made available for the program as extern... I don't think it's a lack of of knowledge about storage classes that is the real problem, it's the lack of knowledge about scoping and about good programming practices for modularity. (and when I see static vars in a function, that also raises an equality bright red-flag, in my opinion. If there's no a pretty serious comment next to it as to why it's needed, I'm going to think something's probably badly designed (in the general case, that is; there can be very fair uses of this). –  haylem Jul 9 '12 at 22:47

2 Answers 2

Not really. Actually using these classes so that they don't make your program worse, not better, is decidedly non-trivial and the only thing they achieve for beginners is to make them write terrible programs.

share|improve this answer
[citation needed] I'd like to see evidence that beginners can "only" write terrible programs if they know what storage classes do (beyond whatever you think of the quality of a beginner's C program otherwise). –  user4051 Jul 8 '12 at 16:04
@GrahamLee: Consider that there are approximately zero ways that professional C coders use global or static variables in a positive fashion ... and then imagine what a beginner will do. –  DeadMG Jul 8 '12 at 19:24
Zero is a very approximate answer. Imagination is not a citation. –  user4051 Jul 8 '12 at 19:27
-1 : Answer and subsequent responses argumentative, opioniated, with no fountaion and referenced facts. –  mattnz Jul 8 '12 at 21:42
@DeadMG Do you mean static local variables? Because using static variables at the global scope in a file helps encapsulation. –  Steven Burnap Jul 8 '12 at 21:43

They Should be Taught

In fact, they usually are.

I'm a bit surprised your class wouldn't cover them.

Auto exists for a Reason

You generally don't need to worry about them though:

  • auto is good enough (in most cases),
  • your compiler is better than you (in most cases).

It's good to try to give hints to said compiler, though. Maybe you'll indeed get something better. Maybe not. But you'll have spent time experimenting it, which means you may or may not have wasted time.

But be sure to learn these important lessons:

  • optimization without benchmark is not an optimization,
  • pre-mature optimization is the root of all evil.

It's often the case at first that junior programmers feel like they need to use every single bit of knowledge and use the best possible code. What they don't get is that the best code is not necessarily the most perfect one. It's the one that does the job by minimizing the result of a complex equation involving time, effort, and outcomes for users (and in the case of C, portability).

share|improve this answer
Thanks. It's not a class I'm taking. I'm teaching someone C and I wanted to make some videos. I just noticed that storage classes are not covered early, and sometimes even skipped, while I've been reviewing several C books. Students often look google for code and I've noticed some poorly written snippets of code online. –  Adam Mendoza Jul 9 '12 at 8:49
@AdamMendoza: I wouldn't call a snippet NOT using storage class as necessarily "poor" though. In fact, I'm wary of the ones that do without good justification. –  haylem Jul 9 '12 at 8:54
True, you don't have to specify the storage class. But lack of understanding them leads to poor code. I attempted to clarify my question above. –  Adam Mendoza Jul 9 '12 at 18:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.