Live data from Hacker News

Learn C The Hard Way

learncodethehardway.org

51–60 of 260 posts

Re: Learn C The Hard Way

#51
post #16
post #9

Earlier quoted context omitted.

Indeed, I currently get turned off by most books I pick up, simply because they start out on a too basic general level. If you however dig a little deeper you can usually find stuff that isn't too tutorialish. Like http://www.c-faq.com/top.html which has been an extremely solid resource for me.

I'm hoping to make this book ramp up faster than LPTHW, since I'm assuming people have either read that book or know one programming language already. However, I'm also a big proponent of practicing the syntax even if you think you're an expert already. It just makes things way easier later on.

Moving from Python to C is a very good move. It allows you to go from programming to inner workings, everyone should have a solid knowledge of C since it give you the tools to identify and correlate aspects of other languages to reality.

I don't understand what "practicing the syntax" means (language barrier?), but I'm not suggesting that I've stopped learning, which I never will.

Re: Learn C The Hard Way

#52
post #22

Earlier quoted context omitted.

As a teaching assistant for a class ( http://www.cs.cmu.edu/~410/ ) where students write a whole lot of code in C, please introduce the address-of (&) operator and explain how to obtain pointers without using malloc(). Do this before the concept of the heap is ever introduced at all. Be careful when explaining the compiler and how a program actually runs. I've found that a lot of student problems come from "the compi…

I find it is much more helpful, when teaching C, to avoid the word 'address', and call (&) the pointer-to operator. Thinking of pointers as numbers (which the address-analogy does) tends to be harmful for newer C programmers because they want to treat them like numbers.

They are numbers (either 32-bit or 64-bit). Nothing more, nothing less. I don't understand why you wouldn't want to think of them that way.

For example...

  const char* current = "ohai thar";
  const char* end     = current + strlen( current );

  assert( end >= current );
  size_t bytecount = (end - current);
(size_t is an unsigned type, so if 'end' is less than 'current', it will overflow. If you want to allow for that, use ptrdiff_t.)

Re: Learn C The Hard Way

#53
post #39
post #31

Earlier quoted context omitted.

Right, but I've never seen it used with integers. %.2d is probably the most common format I've seen. Messing up your format strings is a common exploitation vector, so I was wondering if he had typo'd that or otherwise used it accidentally.

You mean %.2f ? %d is for integers, %f is for reals (yo).

Oh I'm totally using that.

Re: Learn C The Hard Way

#55
post #50
post #29

Earlier quoted context omitted.

It'll crash and potentially allow code execution if the rest of the args are user coercible. Check out the first few chapters of The Shellcoder's Handbook for examples. Thanks for the explanation. I've never seen/had to use %d with integers. I've only used it with %.Nd for floats/doubles.

code/ex3.c:6: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’ You should probably read this book. :-)

Hmm, I don't recall MSVC++ ever warning about that! Unfortunately, I did use Windows at one point in my career.

I'll pass on reading it since I don't intend to write C ever again, but I hope the book works out well for you. :)

Edit: Just dug up some of my old C code. It is indeed %.2f that I was thinking of. Sorry for the confusion once again.

Re: Learn C The Hard Way

#56
post #18
post #5

Do people really think C is some mysterious, inscrutable language? "To many programmers, this makes C scary and evil." Is this actually true for people? I find C code generally very easy and straightforward to understand; there's not any magic behind the scenes, like there is in any language that's more "high level" than C.

Yes, actually people are deathly afraid of C thanks to other language inventor's excellent marketing against it. There's a few things that you need to really nail to be good at C, and they're difficult things, but it's not "dangerous" like people keep claiming.

C is difficult, yes. Dangerous - I don't know in what sense. I manage to get more exceptions in my Python, Ruby code, owing to undefined variables or incorrect types, than I get segfaults in C.

The main issue with C is it takes some time before you are ready to take it head on. An experienced C programmers would have his repertoire of generic data structures library with time complexity guarantees (programs without hashes, expandable lists and operations on them are a pain), will know how to properly use function pointers to do that dependency injection thing other programmers are raving about, separate interface from implementation, know the build environment, know how to use structures and function pointer to build abstractions etc.

But before that, C takes much work to produce little. For someone starting programming, the learning curve is steep. Or more like, the gratification is really, really delayed. It takes some time before he can take on a real world project(it does in the high level languages as well, but the initial progress is faster).

Re: Learn C The Hard Way

#57
post #39
post #31

Earlier quoted context omitted.

Right, but I've never seen it used with integers. %.2d is probably the most common format I've seen. Messing up your format strings is a common exploitation vector, so I was wondering if he had typo'd that or otherwise used it accidentally.

You mean %.2f ? %d is for integers, %f is for reals (yo).

[deleted]

Re: Learn C The Hard Way

#59
post #35
post #17

Earlier quoted context omitted.

how about 'if you are proficient at another language, feel free to skip to chapter x'? and put the generalized stuff at that point

Sometimes due to language discrepancies, skills don't transfer and there is no easy "feel free to skip to chapter x". For example, a conversant Python programmer's idea of string, and strings in C are 2 different things. A conversant C programmers idea of looping is different from idiomatic looping in Racket(and other lisps).

actually you are right, it completely doesn't work in that example

Re: Learn C The Hard Way

#60
Has anyone actually used "Learn Python the Hard Way" in a beneficial way? I started it several times but never got very far and just learned Python from other sources. I didn't really care for the approach.
Post reply on HN