Why learn C?
radar.oreilly.com
radar.oreilly.com
But lots of people do need to learn C. They might not be working on hip new social web services, but they're out there.
In my work, I have observed recent computer science graduates filter in and get assigned to program embedded systems using C. Their knowledge of C was minimal. Their code tended to be suboptimal, or even bizarre to the point of, I wasn't sure why it compiled. I recommended they get a copy of K&R. They did. I don't think they got much out of it.
I don't blame them personally. They simply had not been taught C in school, and had never had a reason to learn it before. Trying to learn it on the job with antiquated books didn't seem to work very well. (Though some did eventually come to learn C well. Others migrated to different assignments not involving C.)
Whatever your reason for wanting to learn C, be it purely academic, or if you actually need to program with it, fresh, modern educational resources are a good thing.
C is the lowest abstraction layer that compiles on the most hardware. "Where the falling angel meets the rising ape", as it were.
Because if you don't know C, you've only seen the map, not the territory.
Because if you don't know C, you're looking at the finger, not the moon.
Because if you don't know C, everything you think about computers is a leaky abstraction.
The counterpoint is that flawed models are often good enough. Newton's theory of gravitation comes to mind.
But if you like knowing what's going on, either for personal satisfaction, or because what you're working on is not fully encapsulated by a different language (inevitably, originally, written in C), then C is both interesting and useful.
To understand how a computer works when it executes a program, accesses some data, writes to a disk, spawns a process and forks it, then kills the child after a certain interrupt can pretty much only be learned by learning assembly and some hardware and OS-related literature. Of course, for most programmers(let alone people in general) this is nearly useless information today. It's obsolete. And that's good.
But what C teaches you about how "things are under the hood", is just a leaky abstraction. C doesn't care about such low-level things because they are implementation details. Standard C could be ran by pen and paper just as well as on a computer. It's a "black box". Takes input and produces output according to the standard.
Frankly, the only "low-level" thing C has over e.g. Java is that most domains in which C is used are working close to the hardware and it runs on "the real hard machine". With C, in it's modern domains, you are exposed to these "low-level" details, not because of C, but because of the domain. With many other languages the user doesn't have to deal with such problems because of the problem domain is different.
Why have a type system that includes implicit coercions between types, sometimes with different internal representations? (Just imagine an int promoting to a float.) Doesn't that obscure the "real meaning" of the program, or is it merely a detail you find uninteresting? C is not the only language that makes these low-level concepts accessible, and C does not represent the "floor" with respect to making the behavior of hardware explicit.
If what you're actually getting at is that C is the most popular language today that exposes all of those things, it sounds less convincing- Systems languages cannot advance if we take C's position as given.
This isn't less convincing to me, I would never argue that someone learn C instead of all other languages. But I do believe that knowing C makes you a better programmer in all (current popular production) languages.
Re: your point about systems programming not advancing...I didn't mean to imply that C was "perfect" (in the Latin sense, meaning "done, finished"), just that it's the best we have for many things.
I do really like Go, and wish I could use it more, for non-personal projects.
You've never written C.
getchar(); //C STDIN.getc //Ruby getChar //Haskell System.in.read(); //java How do any of those show me how software works under the hood? In none of them do I have any clue how a character makes it from my terminal to my program.
int* a; Teaches me nothing about caches, memory latency, NUMA etc. Hell, dereferences are even guarantied to read the same physical location in memory(just the same logical location.)
struct stuff{ int a; int b; };
Doesn't teach me anything about memory layout. C assumes you are running on some hardware from the 70s, it doesn't know about virtual memory, address spaces, memory pages, NUMA, ram with multiple channels, the no execute bit, GPGPU programming. The only thing C has going for it is simplicity.
But it's an interesting /kind/ of simplicity.
FORTRAN is simple, for instance. Yet we don't use it much any more.
C is simple enough to not make you go bananas trying to learn the language [C++], but rich enough that you don't go bananas solving large, interesting problems [assembly]. It pretty much nailed the uncanny valley of just complex enough.
It's a bit creaky. It desperately needs namespaces. I'm on the fence about memory models (this is a platform thing, in my mind), and definitely Do Not Want threads jammed into the language. I would love a decent macro language, but that's probably a decade-long debate (if what happened in the Scheme community is any indication). I would love a compilation system that didn't suck [yes, macros and a Go-like build system probably don't mix well].
I've been writing C for over 30 years. I plan to keep writing it for another 20. The unscientific, neat thing about C is that it's /fun/. I know this doesn't go over well with standards types and cow-orkers who feel the urge to override operator = and take dependencies on Koenig lookup all the time, but C has a charm that other, similar languages have been unable to capture.
Computing is like an iceberg, with the web being the bit above the surface.
rich enough that you don't go bananas solving large, interesting problems [assembly]
Two words: macro assembler. It may surprise you to know that not that long ago, sophisticated GUI apps were written in assembly language (in fact the IDE I use on my ST, Devpac, was written in assembly, and it's everything you would expect of a modern IDE - editor, compiler, debugger, etc - running under GEM). Many games were written in pure ASM.
This doesn't get you away from the ooky stuff that C does for you, like register allocation and code optimization (link time code generation is a wonderful thing, even for C). Very few assmeblers are bright enough -- or should be trusted enough -- to do code motion, strength reduction, common subexpression analysis. And, oh bog, just writing a plain expression? Not even possible in a macro assembler.
[I rewrote the ST's file system in assembly, btw. It started out as an honest effort, then got bogged down in stuff that would have been a no-brainer in C.]
Personally I haven't done any assembly programming in atleast the past 6-7 years but I still get the urge now and then to program in it again. However, even though it's unlikely that happens, my assembly experience has given me a thorough understanding of how the computer works at a basic instruction/memory level which has been extremely valuable when I want to create optimized code in higher level languages (like C). So yes, while learning C is certainly worthwhile even if you are going to write in even higher level languages, learning or atleast graping the fundamentals of assembly is in my opinion even better.
Maybe not, but this does: offsetof(struct stuff, a); offsetof(struct stuff, b); sizeof(struct stuff);
> C assumes you are running on some hardware from the 70s, it doesn't know about virtual memory, address spaces, memory pages, NUMA, ram with multiple channels, the no execute bit, GPGPU programming.
I'm not sure what you're complaining about; virtual memory is explicitly designed so that no code (even assembly language) "knows" about it except for the very small amount of code that sets it up. Likewise with most of the features you are mentioning. A memory reference in C operates at about the same level of abstraction in C as it does in assembly language, which is the lowest-level software interface available.
Well yes, but you're talking about library code there. Your code, which links in libraries, will never fully describe those libraries.
But that library code was very likely written in C...so...
I'm not sure how you can say that knowledge of assembly/hardware is obsolete. Perhaps unneeded for tasks that most programmers have in our hip new ad-driven world, but someone still has to understand and write the code that runs the code that eventually puts the cat-based meme on your screen.
Engineers seldom use calculus, but they used derived functions every day. The same applies to programming, everyone who programs should be exposed to the low level innards enough to at least take the magic away and have a basic concept of what a computer actually does. You'll be better for it.
Damn right it's good--for me--that most programmers think low-level programming/machine knowledge is obsolete. I'm pretty certain it means I'll never want for a (good) job as long as I can still think and move enough to edit code.
That's funny, because 97% of the Linux kernel (the code that implements the things you mentioned) is written in C.
I think you're getting confused by the fact that C has a standard library, so it's true that you don't have to write process spawning yourself. But the standard library is written mostly in C and calls into an OS that is written mostly in C. So C's computational model can indeed explain how low-level things work.
The C standard even defines a "freestanding implementation," which is a C platform that has no access to an OS. So C-the-language can work without any OS at all, and without any runtime/interpreter that is filling the role of a traditional OS. Very few languages can say that, and indeed it's the reason why it makes sense to develop an OS in the language. To write an OS in a language that requires an OS begs the question.
As for process spawning, I/O, etc.: it might be that C is just as good as assembly for those, but I would be surprised if either was sufficient. Presumably that's where "hardware and OS-related literature" comes in.
If you like C, use it. If you want to know what's going on, learn assembly.
If you know C, it is possible to comprehend a direct mapping of your "high level" code to a set of processor instructions. This is complicated by modern huge glib-style libraries and modern compiler optimizations, but it remains possible.
That, combined with the fact that C is foundational in almost all current production languages, and remains highly useful among them, are why I think anyone who takes programming seriously should know C.
Someone else compared C to Latin. Obviously C is more useful on its own than Latin, but the comparison is valid for the foundational aspect. Understanding threads and locking in C gets you a long way toward understanding those concepts in most popular languages.
I'd make the same arguments for understanding Unix, by the way...and also that you can't really understand Unix without understanding C. And the converse.
I disagree. It's complicated by those things, but more importantly it's complicated by modern processor architectures, which do not execute one instruction after another anymore like C would have you believe - they just mostly pretend to.
Which isn't to say this is any less true of the other high-level languages we have at hand, and I still think knowing C is valuable (although I'm wavering more on that then I was 5 years ago), I just think your characterization is incorrect.
At that level, even the compiled opcodes aren't strictly demonstrative of what's happening inside the processor, but you'd only know that if you were running a simulator and watching traces light up.
Extensions such as __builtin_prefetch, __builtin_expect are heavily used in the Linux kernel to allow a higher level of optimization by directly instructing the compiler how to handle branch prediction and caching (based upon careful benchmarking) in performance critical areas rather than leaving it up to the compiler's 'compile-time' heuristics.
It's possible to understand the entirety of practical computation from top to bottom without C needing to be in your toolbox. Correct me if I'm wrong.
However, C requires you to know more about how the compiler is going to turn your character string, say, or your looping construct, into assembly code.
Some languages don't even have structs or pointers, or explicit memory allocation! You're laughing (I hope), but these are real things in memory, with only a thin layer of magic (layout, pointer math) in C...whereas they do exist in other languages, but are hidden -- usually pretty well, but rarely completely to the point where you are better off not knowing about them.
And for pointers, I'll leave you with http://en.wikipedia.org/wiki/Tony_Hoare#Quotations
[1] Tho' processors since the Z80 have instructions to help you deal with null-terminated strings...
> However, C requires you to know more about how the compiler is going to turn your ... into assembly code.
But does it really require (with emphasis)? The nature of some debugging and optimisation might push you towards that, but it's still a choice.
I feel your point comes down to the use of C as a "vehicle" to learn low-level concepts. Which is fine, but someone can also just learn them directly (upfront or retroactively) and use whatever progression of languages they want.
Only if the OS was written in C...
- all the OS written before C ever existed, some still in use, even if in legacy deployments
- Symbian (written in C++)
- Mac OS X (is a mix of C++ and C)
- Windows is moving to C++ as the default systems language
- Native Oberon (Oberon)
- Lilitth (Modula-2)
- Spin (Modula-3)
- BeOS/Haiku (Written in C++)
- House (Haskell)
- MarteOS (Ada)
- IBM's OS/400 system (Written originally in Modula-2 and PL/MI)
- Singularity (Written in C# and C++)
- Lisp OS (Assembly and Lisp)
- Smalltalk Machines (Assembly and Smalltalk)
- Forth based OS for embedded systems
So one cannot expect that an OS will be always written in C.
And I have never met a single person who knows any of your remaining languages who isn't at last passably familiar with C. I've never asked, but I don't think any of them would call their C knowledge unimportant or not useful.
OS X and Windows have some C++ (mostly wrappers) but otherwise are C.
OS X drivers are written in C++, they are not wrappers over C.
http://developer.apple.com/library/mac/#documentation/device...
In Windows all the new APIs since Windows Vista are mostly COM (C++) based, including the new Userspace Driver Framework.
With Windows 8 this will increase, as Microsoft is moving to have C++ as their official systems language, by introducing WinRT as the new subsystem, and dropping support for anything besides C89.
Just because C is used a lot, you cannot say that all (even 99,9%) operating systems use it.
Actually I failed to mention the amount of operating systems that you may have running on your watch, vcr, radio, microwave, etc, which summed together are much more that the OS X, Windows, Linux base.
I do like C myself and plan to continue using it. I've been looking in to OCaml recently and as someone else in the comments indicated, they seem like they'd make a good team.
But, lines of assembly language professionally written: 0.
If you really want to know how current computers work at low level, then Assembly is the only way.
Everything else are higher level abstractions.
Same even goes for something like C# and Java. And in any event, the major influence on how efficiently code runs is the underlying CPU, not C. C is the same, no matter which CPU it's going to be compiled for; the resulting code, which is what actually gets run, will vary. `p[i]', where `int p[100]' is a global, is going to turn into something quite different when built for MIPS from what it would for x86.
So what good is learning C? Well, if somebody will give you a job with it, and that job is well paid, then maybe it would be worth learning seriously... not sure how common this is these days, though.
Or if you want to hack at and/or understand parts of Linux or any of countless utilities.
But, why learn C? Learn C to broaden your experience and gain a marketable skill in the most popular programming language[1]. You don't have to become an expert in it but I think everyone should dabble in it a little bit!
1: TIOBE index, June 2012
I agree with you, 100%. There are no ultimate languages. You have a broad field, as a developer, of words to use - choose the one you a) get along with, b) can comfortably use, c) and are interested in actually using for something.
What I have observed is that good developers learn other languages faster, the more they do it, i.e. if you set your goal higher, you get better at it each time you iterate. I tend to think, as a generality, that eventually there is a point for each individual developer where the effort to learn some new language/codebase/taxonomy gets flatter and flatter, to the point that there are no 'easier nor harder' levels of it any more.
C is still very, very useful. $35 worth of pocketable computing power and a built-in C compiler can still deliver kick-ass results.
I have not had so much fun programming since high school. The general feeling of increased brainpower lurks with me these days mostly because of it.
A distinguishing characteristic of Jikes RVM is that it is
implemented in the Java™ programming language and is
self-hosted i.e., its Java code runs on itself without
requiring a second virtual machine. Most other virtual
machines for the Java platform are written in native code
(typically, C or C++). A Java implementation provides ease
of portability, and a seamless integration of virtual
machine and application resources such as objects, threads,
and operating-system interfaces.Pretty much every programming language in existence has bindings to C.
I program on a mid range system. If I want to write in C for it I could but I have no reason too. I use two languages that appeared well before C and produce very effective and near bullet proof code because of it.
There are some big names in the language world that were here before C and some probably had influence on that language.
While I would not mind refreshing my C, having not had any use for it since 85, I am not even sure where to start.
Out of curiosity, what do you use? My history is not too great, but I can't think of many languages that came before C that are still in use today. I actually use fortran at work (along with C), and I'll give a pass to Lisp (it seems like c predates the dialects that are still in common use, ie common lisp and scheme), but it seems like most others (Pascal, COBOL, Basic) are more curiosities in the modern world. Please correct me if I'm wrong.
That is my observation anyway. I think it has something to do with the deeper understanding of references and dereferencing, and the inner workings of languages in general.
I just started to learn C and C++ about a week ago. I did a module of it at University many years ago, but I never kept it up afterwards and forgot nearly all of it. Anyway, I am completely loving learning it at the moment. It just feels like I have so much more control of what I'm doing, and I can't wait to build and release my first non-trivial application using it.
I want to know C well. Mostly, I just want to be able to confidently hack on the massive amount of software that has been written in C over the years.
[edit] I've always had this feeling in the back of my mind, that no matter how good I am at Perl or JavaScript or HTML or Java, I will never feel like an "expert" until I am good at C.
I've no project in mind yet, but, Eric, your libtm plan caught my eye a few months back and if you've anything on github I'd be interested to follow along. I'm a translator myself and often find myself optimising how MemoQ could be matching segments.
I'll see if I can put what I've got in Python up on github after I finish this big project at work this week and next. I'd be really interested in your input. Did you read the Tim Baldwin presentation on translation retrieval I referenced in my post on libtm? He's done some really cool research that has had a real impact on the way I keep my TMs (e.g., trying not to go over 20,000 segments).
We should be careful about the word "learn." I read it as "understand C and master C," not simply being able to write C code.
Why read Shakespeare when you're learning to write? Why study Bach when you're playing jazz? You can definitely skip Shakespeare and Bach, but I think it's universally acknowledged that studying their work gives you important foundational knowledge even if they don't apply directly to your work at hand. Same with C.
It'll be the Latin of computing. There will be some poor soul studying and using C in a thousand years.
I would hope that by that time there will be powerful algorithms you can use to transcompile old C code into $LANGUAGE code.
EDIT: Well actually I would hope by that time strong A.I has been invented and asking flesh and blood humans to write code will elicit some really strange looks.
This isn't 100% true: http://en.wikipedia.org/wiki/C%2B%2B#With_C
C++ is built on C. You need to know C (but maybe not the C library) to know C++. Like pointers
Learning C before C++ is a very bad idea, as it will make you try to use language patterns that are considered bad ideas in C++, as there are safer alternatives.
C on the other hand, has almost global compatibility with other general purpose languages. It should therefore be everyone's effort to write good C libraries and promote code reuse.
Of course, there's no harm in writing your library in C++ and exposing a C interface to it - but that's not necessarily as simple as it might sound. The better approach is to design your API in C and then figure out how to implement it in C++.
As for writing the actual applications (non-reusable part), there are obvious advantages to using C++ over C.
> Kernighan and Richie's The C Programming Language is one
> of most popular, if not the most popular, programming books,
> and it defined the ANSI standard.
Wait, what? ANSI defined the ANSI standard, which had quite a few additions from K&R C. It'd be more proper to say that the book defined C itself before the publication of the standard.These days, I'm not writing so much C, but more heavily depending on the deep understanding I have of how various layers of the whole stack of a computer, running an application, work. How C works, what a compiler is doing with C code, how to write good (and bad) C code .. all of this is a deep skill, still applicable to understanding and debugging at the OS and Application level.
The reason to learn C is this: there are a lot of components of the modern stack that are still composed of it. If you learn C, and know C, and can comfortably produce reliable, rock-solid, working systems around a C framework, then you will have exercised an ability that is, naturally, broadly applicable throughout the computer world.
That is not to say that you should not make C your main language; moreso, use other more advanced, more tailored languages as you see fit. But if you're going to exercise the ability to freely shift over the whole stack, C is going to be a mightier tool than most.
Re: giving K&R to newbies: it should be, give K&R plus "Expert C Programming - Deep C Secrets" to newbies, and professionals alike. Together, those two easy to read books will help you gain a fast grasp of how to write C programs, why to write them in certain ways, and so on. I find K&R a great reference, but Deep C Secrets a rather fun read; only have the latter on the crapper shelf in the bathroom, for example. Plus, I never get DeepCSecrets back when I loan it to fellow coders (ever), so I have a few extra copies, too, to give to newbies I work with. See: (http://books.google.at/books/about/Expert_C_Programming.html...)
If you are learning C, and haven't heard of cscope, you can do no better than get it set up, do the tutorial, learn how to use it as a tool. cscope is a very capable text-based navigation/browsing tool, for large C code bases. Unpack your favourite F/OSS application, fire up cscope on the root dir, search for main, build a key word list, navigate freely. Bonus points: it has superlative vim integration.
See: (http://cscope.sourceforge.net/) & (http://cscope.sourceforge.net/cscope_vim_tutorial.html)
C will not fail you when most other languages might. There are times you may realize, in fact, you are doing things that would be easier in C, off in Java and Haskell and Ruby land, ad&infinitum..
One last really, really good reason to learn C: Lua.
Lua kicks ass. Why? Because you can glom Lua into any C code base, and give yourself a much comfier language to do business/game/architecture code in, while still having a fairly large degree of raw control over the C runtime power, to boot. Master putting Lua into a small C lib collection, and you'll see what I mean.
Learning to add Lua to a big code-base is like .. somehow .. a final 'delivery' of the whole 'write code once, run it everywhere' promise, albeit its a developer mantra, not some CorporateOS-decides-to-bundle-your-interpreter/runtime issue.
Anyway, just my two cents worth. Hope I still see new C coders being made in a few more decades, eh ..