Books on learning C?

Hi.I am college student majoring in chemistry who wants to learn C programming.I don't like very dense books as I don't have much time for hobby.I took a course in python and tried to study The C Programming Language by KnR.However I found the practices on KnR to be very hard,and sometimes I find the explanations not very easy to understand (don't get me wrong,KnR is still an excellent book).
I want to learn C programming as a hobby,and eventually being able to write some simple command line utilities.Do you have any recommendation on books on C that are also relatively thin while being easier?
Best
 
K&R is OK. Only get 2nd edition. If C is not "easy", read the book 2-3-4 times.
I completely second this.

If you wanna learn C. Not just read it only, do the exercises - not just read them, get an idea, how you would solve that, but actually, really do them. Be prepared for lots of surprises you never thought of what you will learn by an exercise, when you just read them. You will get pretty solid fundamentals, and learn a lot more, like knowing, when to define the specification is fulfilled, and defining your own rules, like skip an exercise.
I wouldn't say, 'K&R is all you need' - depends on where you are going to. But if any above all this one first.
Being well versed in the K&R maybe all you need, and then you can see, if you need additional books to get into special topics deeper.

If you find C too hard, and don't target to program the system or advanced software, but "just" want to program some own tools for your daily use, you may reconsider if better chose some other programming language like sh-scripting, Python or Perl. Those for sure are no toys, but sophisticated, professional full-power programming languages.

After all, programming is not learned by any book or class, but by doing it yourself a lot.
Programming is like writing texts. School teaches you only the basics. But how to write a funny story or a thrilling novel easy to read must come from yourself, and by reading a lot of stuff by others.
And that's the same for any programming language.
 
Is your goal explicitly to learn C and incidentally write utilities or is your goal to write utilities. You can write utilities in many languages, including Python.

If you want to learn C, you should read K&R. C is not intuitive. K&R is terse, yes, but it is correct and explains the correct way to think about the language constructs. There are many books that attempt to teach C, but end up teaching their particular, sometimes, incorrect understanding of C. C is subtle and K&R's phrasing is intentional. Other books may be easier to read, but the underlying concepts will remain just as subtle.

Once you're done, you should have a solid foundation to understand the changes in C99, C11, and C23.
 
and what exactly is not clear? I think you need to read the book about algorithms Grokking Algorithms, Second Edition
An illustrated guide for programmers and other curious people
Автор: Адитья І Бхаргава. Наприклад, хочу сказати, що я також не одразу зрозумів, для чого потрібні масиви, чим динамічна пам'ять відрізняється від статичної ... Не соромтеся питати
don't teach the syntax of a programming language, the algorithm is our everything,
Code:
while (1 ) {
printf (" infinite loop \n ");
}
if you don't understand that instead of 1 you can write true or false. Programmers are not born, they are made
 
I learned it from Borland Turbo C, then Borland C++.
I have no reason to write it, so I stick with Delphi 7.

Lazarus and Embarcadero have inflated so much the environment is overwhelming.
All I need is a tree, a rope and tire swing.
Don't need a gilded swing set with mink covered seats.
 
Never went though an entire C-book and took most from FreeBSD man (3) pages. It covers the significant basics already.
#1 practical rule of C: everything is made of bytes. It can't be that complicated.
and what exactly is not clear? I think you need to read the book about algorithms Grokking Algorithms, Second Edition
An illustrated guide for programmers and other curious people
Автор: Адитья І Бхаргава. Наприклад, хочу сказати, що я також не одразу зрозумів, для чого потрібні масиви, чим динамічна пам'ять відрізняється від статичної ... Не соромтеся питати
don't teach the syntax of a programming language, the algorithm is our everything,
Code:
while (1 ) {
printf (" infinite loop \n ");
}
if you don't understand that instead of 1 you can write true or false. Programmers are not born, they are made
That requires the awareness of a predefined boolean type that's part of the language. "1" doesn't mean true or anything else unless you make it a standard.
 
I think you need to read the book about algorithms Grokking Algorithms, Second Edition
Without looking at the book, I don't see how an algorithms book will help a beginner learn C .
I found Beej's book to be silly but I wasn't a beginner when I read it so that may be why. Perhaps it's worth looking at as a beginner.
Starting life as an electronic engineer who designed computers from the chip level probably helped me a lot when understanding how it all works.
Starting programming from the microcode and assembly language level probably helped even more.
 
This kind of book is why I gave my original warning. The first two pages of Chapter 5 on pointers contains the kind of generalities and "common sense" explanations that cause problems.
pp 33
1 Typically. I’m sure there are exceptions out there in the dark corridors of computing history.
2 A byte is a number made up of no more than 8 binary digits, or bits for short. This means in decimal digits just like grandma
used to use, it can hold an unsigned number between 0 and 255, inclusive.

pp 34
This means that all these things mean the same thing, i.e. a number that represents a point in memory:
• Index into memory (if you’re thinking of memory like a big array)
• Address
• Location
These things may be true for your computer, but they are not true for all computers. More importantly, they are not how C actually works. C is not a high-level assembly that some people claim or want it to be. It has its own semantics. If you write code that violates the rules, your code may work on your machine today, but it can just as easily break with a seemingly unrelated change if you don't understand that you've violated a rule.
 
What does "learning C" actually mean?
Does it mean learning the syntax so you can compile and link something?
Does it mean learning how to use C to implement a specific algorithm (say a linked list)?

Syntax is the easy part, implementing algorithms is only moderately harder.
Comment the heck out of your code so you don't forget the intent.
Spend time looking at existing code (FreeBSD source is a good resource)
Understand the little things (like "what does a scaler on a pointer actually mean")
C is not garbage collected so you need to manage your memory:
Match your allocs/frees.

I know this is pretty generic, but my opinion/experience, once you really know one language, a lot of other languages are just figuring out syntax.
 
once you really know one language, a lot of other languages are just figuring out syntax.
I struggle with that. I'm not a programmer's programmer though a few times I've had programmer's admire my work. Every time I try to learn another language, and I write some code, I think to myself, "I could have done that just as easy in C!". Someone on this board pointed out that John Carmack has the same problem.
 
K&R is really the real thing... but its good to have a simple intro just to start learning with.

How about trying this free online tutorial? https://www.learn-c.org/

There are several similar ones on the web, but that one looks fine. Work through it and get some basic familiarity, then maybe have another look at K&R and it will look much less fearsome.:-) I think there was an oreilly book called "Practical C" that I seem to remember was quite good too.

Learning anything is an incremental process, like a rolling stone gathering moss; no-one learns everything up front in one go. You really only get so much from books, it's more important to actually try writing some real programs, ie learn by doing. My advice is don't wait until you think you've read everything in sufficient depth, before you start writing some programs, instead just read enough to get started and then dive in, you'll learn a lot more that way. You can always come back to the books later.
 
I struggle with that. I'm not a programmer's programmer though a few times I've had programmer's admire my work. Every time I try to learn another language, and I write some code, I think to myself, "I could have done that just as easy in C!". Someone on this board pointed out that John Carmack has the same problem.
I'm with you on that. My experience has been a lot of languages have "C-adjacent" syntax; so close to C but not exactly the same which for me I create uncompilable code in language X because I wrote C.

An advantage of the similarities is by knowing C it makes understanding/reading code in language X a bit easier.
Heck, even Java/JavaScript reads enough like C to be understandable. My problem comes in with differences like memory management.
If you are used to counting malloc/free, things like garbage collection are hard to accept. You understand the concept, but go "I did a new over there but where do I put the free".

I had a book on my shelves title was something like (paraphrased) "Data plus algorithms equals programs". Interesting read, language agnostic.
That is kind of where my "what does learning C actually mean" came from.
 
Back in the day when I was learning "C" -- I "owned a copy of K&R" and I referenced it... but it definitely was not my ONLY source book for learning "C".

Everyone learns to computer program differently - so find a book/author that fits your learning style and go with it. There is nothing wrong with the "C Programming Dummies" book if you pick that ... once you finish it... you won't be a dummy! :cool: (The C Programming Dummies book did not exist when I was learning C for the first time :-/ )

Other tips:
  • Take a look at "C" programs that already exist on the Internet and/other places. Figure HOW and WHY they work. Learn to compile and run those C programs and see those programs work.
  • FreeBSD, *BSD, Unix and Linux operating systems are the best places to learn how to code "C" for the first time.
  • Always make your early C coding mistakes on a safe Unix or Unix-like operating system - and ( do not ) learn to program on an O/S or machine that you care about. (Aka "I just trashed/lost my homework/term paper because I coded my "C" program incorrectly and when I ran it -- it deleted everything.").
  • (as above) - You can set up an Oracle Virtual Box, jail, bhyve, etc and sandbox your coding environment that way if you don't want to dedicate hardware in order to learn how to program C.
  • See your newly coded awesome C program run, die and dump 'core'. Then figure out how to open the 'core' dump file and determine what went wrong when your C program was running. (hint: use 'gdb').
 
Dear OP...C is like going hunting with a primitive long bow instead of a high powered scoped rifle. To succeed with the long bow you need to become intimately familiar with your prey, be able to anticipate its habits, and get close in...which leads to a more profound respect for your prey.

C is "next to hardware" programming. It's not just about learning syntax but also about learning systems architecture so that you can understand how it works. C is one level above assembler. Forget all the higher level OO stuff and concentrate on the translation from C code to what goes on under the hood.

I've been lurking and trying to figure out what beneficial things to say...As was mentioned above, python is also a good language to write command line utils in. Most of my command line stuff is in python these days. In fact, my only C now is kernel modules or embedded systems code.
 
K&R is really the real thing... but its good to have a simple intro just to start learning with.

How about trying this free online tutorial? https://www.learn-c.org/

There are several similar ones on the web, but that one looks fine. Work through it and get some basic familiarity, then maybe have another look at K&R and it will look much less fearsome.:-) I think there was an oreilly book called "Practical C" that I seem to remember was quite good too.

Learning anything is an incremental process, like a rolling stone gathering moss; no-one learns everything up front in one go. You really only get so much from books, it's more important to actually try writing some real programs, ie learn by doing. My advice is don't wait until you think you've read everything in sufficient depth, before you start writing some programs, instead just read enough to get started and then dive in, you'll learn a lot more that way. You can always come back to the books later.
I'm not sure that O'Reilly still sells books these days, but libraries often times have a subscription to their books.

Personally, I was messing around with C last year via a combination of boot.dev and this tutorial on how to write what is basically the Doom engine in C from scratch. IIRC it is slightly different from the original engine, but definitely close enough and rather engaging.
View: https://www.youtube.com/watch?v=huMO4VQEwPc&t=475s


Both of those require $0 to use them, although boot.dev does charge for the use of the tools past a certain point. But, not required.

I then went back to Perl and Java, but at some point I'll probably go back and do more C as it was both informative about how computers work and rather interesting, it just doesn't solve problems that I want to solve right now.
 
Back
Top