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…
Where to begin when learning C? Start by making lots of errors
11–20 of 90 posts
Re: Where to begin when learning C? Start by making lots of errors
#12I 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…
- 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
#13Earlier 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.
Re: Where to begin when learning C? Start by making lots of errors
#14I 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…
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
#15By making sure to enable all warnings , enable warnings as errors and use a compiler that integrates static analysis like clang.
Re: Where to begin when learning C? Start by making lots of errors
#16I 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
#17Earlier 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.
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
#18Earlier 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.
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
#19Earlier 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()…
Re: Where to begin when learning C? Start by making lots of errors
#20I 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…
Whereas, the less said about complicated code, the better.