Live data from Hacker News

Where to begin when learning C? Start by making lots of errors

morganwilde.svbtle.com

11–20 of 90 posts

Re: Where to begin when learning C? Start by making lots of errors

#11
post #7

I learned C by reading K and R and doing the book's exercises . . . on paper. At the time I didn't have access to a C compiler, so I wrote them all out in a notebook. A month later I got a job at a shop that was running Unix, and got the chance to type my programs in and try them. I had a lot of things wrong. It took me a while to understand the difference between control-D and EOF, for instance (how embarrassing). B…

I find the visceral act of manually writing code to be an extremely effective tool when learning programming language syntax. I find it similar to the "Write these words 10 times" technique used when teaching grade school kids how to spell and read.

Re: Where to begin when learning C? Start by making lots of errors

#12
post #7

I learned C by reading K and R and doing the book's exercises . . . on paper. At the time I didn't have access to a C compiler, so I wrote them all out in a notebook. A month later I got a job at a shop that was running Unix, and got the chance to type my programs in and try them. I had a lot of things wrong. It took me a while to understand the difference between control-D and EOF, for instance (how embarrassing). B…

"Programming is learned by writing programs"

- Brain Kernighan

And that is what anyone who goes through K&R will do: Reading and writing lots of real programs. A simple program is worth a thousand words!

Which unfortunately the majority of technical books just can't get right. Explain the concepts in 5 full dull pages, and then at the end "Hey, checkout this little snippet of code, which by the way does nothing really interesting, but is here to illustrate what the author was talking about :)"

Re: Where to begin when learning C? Start by making lots of errors

#13
post #6

Earlier quoted context omitted.

I am using the default `cc` compiler on Mac OS X for this without any flags, are you saying I should something non-stock in this case?

Try passing at least the -Wall and -Wextra flags. For learning, you could also add -ansi, as it will make your compiler be stricter about standards.

instead of -ansi, i'd argue that -std=c99 (or -std=c11) would be more appropriate. also note the -pedantic flag.

Re: Where to begin when learning C? Start by making lots of errors

#14
post #7

I learned C by reading K and R and doing the book's exercises . . . on paper. At the time I didn't have access to a C compiler, so I wrote them all out in a notebook. A month later I got a job at a shop that was running Unix, and got the chance to type my programs in and try them. I had a lot of things wrong. It took me a while to understand the difference between control-D and EOF, for instance (how embarrassing). B…

"Programming is learned by writing programs" - Brain Kernighan And that is what anyone who goes through K&R will do: Reading and writing lots of real programs. A simple program is worth a thousand words! Which unfortunately the majority of technical books just can't get right. Explain the concepts in 5 full dull pages, and then at the end "Hey, checkout this little snippet of code, which by the way does nothing reall…

I'd like to add some emphasis on reading. For a beginner, writing is obviously very very important as the way to get to the point where he can just sit down and solve a given task.

But reading lots and lots of code (from many different sources) will help him pick up idioms and find common & good solutions to specific problems. Even for a smart person, I think it'll take a lot of time and effort to arrive to the cleanest way of doing things.

Of course there's a risk of picking up bad habits, but if you read lots of code, you should eventually develop a feel for what's clean and readable and easy to understand, and what's messy and wrong. This sadly doesn't help with issues like undefined behavior, but you don't learn these just by writing either. For that you need to look into the spec or some other text that'll cover these.

EDIT: I like to think that I'm a fairly good C programmer (having coded nearly all my life, with C mostly), but I still peek into others code all the time to find out how they've solved some things I'm about to do.

Re: Where to begin when learning C? Start by making lots of errors

#15
post #3

By making sure to enable all warnings , enable warnings as errors and use a compiler that integrates static analysis like clang.

or with GCC: gcc -ansi -pedantic -Wall -Werror or exchange -ansi with -std=c99 later on when you grasp the core of c89.

Re: Where to begin when learning C? Start by making lots of errors

#16
This situation is why there is "hello world"; that program seems trivial but actually has important implications; for instance, that your environment works, that you know how to feed programs to the compiler, that you can actually run the output.

I never took "hello world" seriously until I started doing embedded systems work; it's now an extraordinarily important tool for me (I work an anomalously weird number of different environments).

Re: Where to begin when learning C? Start by making lots of errors

#17
post #6

Earlier quoted context omitted.

I am using the default `cc` compiler on Mac OS X for this without any flags, are you saying I should something non-stock in this case?

Try passing at least the -Wall and -Wextra flags. For learning, you could also add -ansi, as it will make your compiler be stricter about standards.

And probably -Werror (all warnings become errors, breaking compilation).

Fun post. Now I know why everyone was raving about the clang error-messages:

    echo "void main() {}" > main.c


    gcc -Wall -Werror --std=c99 main.c 
    main.c:1:6: error: return type of ‘main’ is not ‘int’ [-Werror=main]
    cc1: all warnings being treated as errors


    clang -Wall -Werror --std=c99 main.c 
    main.c:1:1: error: 'main' must return 'int'
    void main() {}
    ^
    1 error generated.

    (not showing clangs pretty colours)

Re: Where to begin when learning C? Start by making lots of errors

#18
post #8

Earlier quoted context omitted.

The Mac OS X C compiler is clang, no need to install anything extra. C is insecure by design and IT is still paying for its widespread, when used by developers that don't use the best practices for secure C coding. So when learning how to use it, the best way is to learn those best practices from day one.

Absolutely, but my way of going about this would be to first show _why_ something is a best practice, rather than forcing people to take it at face value.

The problem is that people are allowed to take the easy route, they will never write secure code.

Writing secure code is always harder than not, and developers love to take shortcuts.

For example, many of the IO functions like gets() are no longer valid in C11 exactly because how unsafe they are.

Re: Where to begin when learning C? Start by making lots of errors

#19
post #17
post #6

Earlier quoted context omitted.

Try passing at least the -Wall and -Wextra flags. For learning, you could also add -ansi, as it will make your compiler be stricter about standards.

And probably -Werror (all warnings become errors, breaking compilation). Fun post. Now I know why everyone was raving about the clang error-messages: echo "void main() {}" > main.c gcc -Wall -Werror --std=c99 main.c main.c:1:6: error: return type of ‘main’ is not ‘int’ [-Werror=main] cc1: all warnings being treated as errors clang -Wall -Werror --std=c99 main.c main.c:1:1: error: 'main' must return 'int' void main()…

Nice suggestion. Although I can't seem where one would have to look for those "pretty colours" you mentioned, I certainly can't see any of them on my Mac terminal...

Re: Where to begin when learning C? Start by making lots of errors

#20
post #7

I learned C by reading K and R and doing the book's exercises . . . on paper. At the time I didn't have access to a C compiler, so I wrote them all out in a notebook. A month later I got a job at a shop that was running Unix, and got the chance to type my programs in and try them. I had a lot of things wrong. It took me a while to understand the difference between control-D and EOF, for instance (how embarrassing). B…

"Programming is learned by writing programs" - Brain Kernighan And that is what anyone who goes through K&R will do: Reading and writing lots of real programs. A simple program is worth a thousand words! Which unfortunately the majority of technical books just can't get right. Explain the concepts in 5 full dull pages, and then at the end "Hey, checkout this little snippet of code, which by the way does nothing reall…

> A simple program is worth a thousand words!

Whereas, the less said about complicated code, the better.

Post reply on HN