6 ms·
Why I Still Like C
- eventhough 18y agoWhat is the answer to the second question? int i[10]; <- allocates memory for 10 integers &i <- pointer to the first element in the array (i.e. i[0]). correct or not?
- gizmo 18y agoGeez. No. int i[10]; does allocate memory for 10 integers (on the stack) So i has the type int•. A pointer to a value: an array. A pointer to a value is the same thing as a pointer to the first value in the array. That's why you can use ++i, increment the pointer, and it will point to the second value in the array (++i => •i == i[1]) So &i is, wait for it, a int••, that is, a pointer to an array. So if you have an array of arrays, then you need to dereference twice. E.g. argv. It's a char••, so an array of strings which is an array of arrays of characters.
- kylec 18y agoIf you declare int i[10]; you can't do ++i because modification of the value of i is not allowed.
- gizmo 18y agoYeah, but that's not the point. It's about which address points to which place. An array always points to its 0th member in C by default - that was the point. Declare int• j = i; and you can do as much pointer shuffling as you want.
- tialys 18y agoI've just finished my freshman Java class and was severely disillusioned at how far from 'programming' it felt. I picked up a copy of K&R and have been enjoying myself since. I actually feel bad for my fellow majors who think that Java/OOP is the only way that things can be done.
- jrockway 18y agoI actually feel bad for my fellow majors who think that Java/OOP is the only way that things can be done. I feel bad for people that think imperative-style programming is the only way things can be done. C is not the anti-Java. It's Java minus modern features and with more rope to hang yourself with. (Then again, someone actually told me that spending an entire day with Valgrind hunting down a memory leak was fun.) If you want power, non-Java-ness, and modern features, learn a language like Lisp, Haskell, OCaml, etc. And learn Perl for actually getting things done.
- d0mine 18y agoLearning: You've missed the point. Learning C is not about imperative-style programming. It is about seeing one abstraction level down relative to your favorite programming language. http://news.ycombinator.com/item?id=235727 http://news.ycombinator.com/item?id=235727 Using: And C is not inferior to Java. They serve different purposes. http://news.ycombinator.com/item?id=235766 http://news.ycombinator.com/item?id=235766
- ConradHex 18y agoParty pooper. There's nothing wrong with learning and using C. It may not be your favorite flavor, but it's a perfectly valid and reasonable choice.
- jrockway 18y agoIt may not be your favorite flavor, but it's a perfectly valid and reasonable choice. I didn't say not to use C; there is plenty of good software written in C. Like... everything :) But, I don't think it's very valuable to know. It doesn't put you in a good position to write correct, maintainable software. There are no tools for that, only for twiddling bits. Therefore, I think C should be relegated to interfacing with UNIX (which you Just Have To Do) and small number-crunching routines. In those cases, there's really nothing to screw up (UNIX interface -- allocate resources, mutate them, deallocate; number crunching, call a function with some arguments). For both of those uses, C has been very successful. Just don't write a big application with it, there are much better tools for that.
- jrockway 18y agoMost of the article is "why I think learning assembly is important". I don't know assembly, but I do know how computers work (and I even know you have to trigger an interrupt to make a system call). I think he should just get to the point and say "people should learn how the computer works", rather than suggesting that you learn a programming language that makes you aware of some aspects of how a computer works. It's like a very sharp knife: you can cut yourself as well as whatever it is you're trying to slice. It's really easy to write broken or inflexible code in it. This is true of all powerful tools. I've seen horribly unmaintainable Lisp and Haskell. I've also seen very clean and maintainable Perl. C is no different than any other programming language, although it does actively discourage good code. You can ignore the discouragement, but why not use a language that encourages maintainability, or at least automatically deallocates memory when you're done using it? Anyway, I don't think this article shows why the author likes C, or why someone else should learn C. Sure, learn C if you want to learn C. But you can also learn about your computer by just learning about your computer.
- tlrobinson 18y agoI think he's basically saying you should know how computers work, and learning C is a pretty good way to do that. I like C because it's pretty "close to the metal" but much easier and more portable than assembly. I can usually visualize what's going on under the hood if I need to. That's much harder with higher level languages. And on top of that it's a useful language to know. Knowing assembly would also help you understand how computers work, even more so than C. But it's not as useful these days.
- gaius 18y agoI've also seen very clean and maintainable Perl Are you sure you just didn't imagine it? ;-)
- IsaacSchlueter 18y agoDial your time machine back a few decades, and you'll see assembly programmers telling those C upstarts that they should learn assembly for all the same reasons. A few more, and it's the logic circuit designers griping that you can't trust instruction cards because you can't see the program. A few more, and it's the vacuum tube enthusiasts complaining that digital switches are slower, less reliable, and too small to debug properly. If you want a visceral relationship with your hardware, get a soldering iron and go to town. That's a respectable hobby, and confers mega geek-points. If you want to write software and be productive at it, learn a real language. C is a powerful tool, and every programmer should know it. But it's woefully underpowered as a fulltime software development tool.
- utnick 18y agoarticle reminds me of my granddad complaining that us youngsters don't know how to kill and clean chickens because we can just go to the grocery store and buy them already ready to go.
- pmjordan 18y agoThis isn't crankyness. If you try to generalise the thoughts on asm/C then it boils down to this: - Abstractions will always be leaky and - be prepared for when an abstraction leaks. As for the ideology part: think for yourself and try to see beyond the hype And I have to say I absolutely agree. For example, I've written maybe a handful of lines of assembly, and maybe as many lines of inline assembly. However, understanding assembly language has saved me many times. Compiler bugs exist. Sometimes you don't have access to debug info and a debugger, just the instruction pointer and a register dump. Sometimes the compiler just can't optimise well enough. C is just one level of abstraction higher, so that's probably the level that's going to leak through when your high-level-language interpreter fails for some bizarre reason. (recent Ruby segfaults anyone?) It's not turtles all the way down, but rather a menagerie of different abstractions, and it's useful to know your zoology, as it were.
- jimbokun 18y agoWhat's the answer to the p, q, r question?
- parenthesis 18y agoWhat p points to shouldn't be modified (but p can be changed to point to something else). q mustn't be changed to point to something else (but what it points to can be changed -- except that in this particular example, q will point to a string constant, which shouldn't be modified). Both of the above.
- jimbokun 18y agoThanks!
- catechin 18y agoObviously you should be familiar with C. This entire conversation, however, is misguided by comparing it to Java or to fancy high-level languages. It's apples and oranges. Of all of those, only C/C++ offers extremely high performance as its key feature. Being primarily a shorthand for assembly, C occupies a permanent place as the systems and performance language of choice and will remain there. If your application demands performance, use it. If not, then don't.
- seano 18y agoSure, but first write the application in a higher level language and then, if required, you can re-write the bottleneck in C. Otherwise what you are doing amounts to premature optimisation.
- rw 18y agoOtherwise what you are doing amounts to premature optimisation. That's a good insight - what language you use is also a form of optimization.
- hsmyers 18y agoJust as a useful benchmark for all of you higher level language fans--- try programming without your C libraries...
- Oompa 18y agoCompletely agreed. I've been doing Euler problems in Ruby/Java, and my friend does them in C. The difference there is very noticeable, and quite interesting at the same time. Obviously our programs are only compared when we use the same algorithm.
- Enlightenment13 18y agoThe #1 thing that I hate about C++ is all the fcking side effects that people can bury in their constructors and every where else under the fcking sun. I want to start swinging a baseball bat at all the fcking idiots that don't think in advance of using the latest fcking cool C++ whatsamcallit. When you are maintaining a very very large project, the best thing about C is that when you read the code it is fairly obvious what is going on, and you don't have worry about some innocent looking code causing a bunch of other crap the be executed behind your back. On the downside of C, name space collisions are a problem and the ability to hide data and functions. I vote they should add C++ style classes to fix the name space problem, and private and public keywords to hide data and functions. I don't want all of the object-oriented B.S. like constructors and destructors that makes it difficult to read and debug other peoples code. Don't get me wrong.....I think some features of C++ are very useful, especially operator overloading which has allow us to make it easier to code big programs that handle lots of longer intergers and weird integer sizes like 56 bits, or 112 bits, or 128 bits. Yeah, these days we have uint64_t in C99 compilers, but back in the mid-90's we had to write code in C++ just because it had operator overloaded classes, which we also had to write. Even though I haven't stated earlier...I do real-time embedded software...so I'm not going to be writing an interrupt in Perl or Python anytime soon. C is not going away in the real-time / embedded software / driver world anytime soon. It is still very very common!
- Enlightenment13 18y agoThe #1 group that says C should be dieing off is f-ing book authors and sellers! Why? Because they can't write a bunch of new books on an older language. It is old...so we can't write book and make money on those older things. New stuff is cool...yeah new stuff is cool...buy our books...buy are books.
- Enlightenment13 18y ago>> C is a powerful tool, and every programmer should know it. But it's woefully underpowered as a fulltime software devlepment tool. Only a inexperienced fool says such a thing! The bottom line is that it all depends on your what you are trying to do. For embedded / real-time / drivers / O/S, both C and C++ are perfect, but for middleware and scripting and many other high level things they aren't the best language.