Live data from Hacker News

Learn C The Hard Way

learncodethehardway.org

31–40 of 260 posts

Re: Learn C The Hard Way

#31
post #8

Why is %d used to interpolate an integer into a format string? I don't recall seeing this done for any reason before. I hope Zed actually covers how dangerous format strings can be if not handled properly. Format strings are still (hilariously) one of the major exploitation vectors in C-based applications today. Edit: According to Wikipedia, %i and %d are synonymous. Sorry for the confusion.

d for decimal?

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.

Re: Learn C The Hard Way

#32
post #20
post #10

Earlier quoted context omitted.

Got any feedback on the things students get wrong? My experience is they fail to grasp memory management, pointers, functions as pointers, linkers or just how a program actually runs. If you've got others I'd love to hear them.

dedicate some later chapters to ramping the reader onto assembler, the stack (and smashing it) and instructions there are a lot of C devs who don't understand what it is their code is producing, and how an application and memory are managed

While this is important, it probably shouldn't be in beginner C book.

I understand all of it now, but had no idea about the stack or the x86 instructions, etc for years. I was still productive, and wasn't hindered.

The details can come later. Even big details like "how it works at the low level".

The most important part is to keep things fun; LPTHW was fun. For me, mucking about in assembly wasn't, and that sentiment seems like it might be common among new C programmers. Assembly has a way of slowly steamrolling your motivation.

Re: Learn C The Hard Way

#33
post #17
post #16

Earlier quoted context omitted.

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.

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

If you really are that proficient, you should be able to figure out what you can skip.

Re: Learn C The Hard Way

#34
post #8

Why is %d used to interpolate an integer into a format string? I don't recall seeing this done for any reason before. I hope Zed actually covers how dangerous format strings can be if not handled properly. Format strings are still (hilariously) one of the major exploitation vectors in C-based applications today. Edit: According to Wikipedia, %i and %d are synonymous. Sorry for the confusion.

d is for dinteger, obviously.

Edit: Actually, I finally remembered why I always use %d, and why I find seeing %i odd. While %d and %i may be the same for output with printf, they are different for input with scanf. (Also I'm not sure that %i was part of the C89/90 standard for printf, it may have been a compiler specific thing (another reason to avoid); it's defined in C99 but I don't have a copy of the 89/90 pdf.)

Re: Learn C The Hard Way

#35
post #17
post #16

Earlier quoted context omitted.

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.

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).

Re: Learn C The Hard Way

#36
post #4

I always had an idea of a 'for programmers' series of books for those who know how to program in one language (say, C++, Java or PHP) and wish to pick up a new language eg. 'python for programmers' would not need the first half of it dedicated to explaining strings, loops etc. and could get straight into it from a programmers perspective - a bit like K&R. You could then dedicate more content to explaining philosophy,…

> this would also be a good format to learn new paradigms

Or you could have books dedicated to a paradigm, such as functional programming, and have examples in many languages that all serve to drive home a specific point (this is how you make something tail-recursive, perhaps), pointing out how each language is both similar and different.

"The Practice of Programming" by Kernighan and Pike is somewhat close to what I want, but is not it.

Re: Learn C The Hard Way

#37
post #10

Earlier quoted context omitted.

Got any feedback on the things students get wrong? My experience is they fail to grasp memory management, pointers, functions as pointers, linkers or just how a program actually runs. If you've got others I'd love to hear them.

The arrow operator and identifiers with leading underscores seem to be glossed over or skipped fairly often in education. It's intimidating to look at code that uses them if you don't know what they are.

Identifiers with leading underscores? I use those for member variables... (this makes more sense in C++ without the arrow notation.)

Re: Learn C The Hard Way

#38
post #13

Earlier quoted context omitted.

It's not for the type, but for the output. The 'd' means "decimal" output. For example, you can do %x to "hexadecimal" output, and %o for octal. Of course, there's some complex interplay with what type you hand that function. As for how "dangerous" it is, yeah it's not going to rape your family, it'll just crash. So I'll be showing people how to prevent it.

> it's not going to rape your family, it'll just crash Well, if it hits undefined behavior it could in fact rape your family, the standard allows for that.

Oh right, I forgot they added that to the C6.283185307179586 standard.

Re: Learn C The Hard Way

#39
post #31

Earlier quoted context omitted.

d for decimal?

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).
Post reply on HN