r/cprogramming • • 6d ago

New to C

I've recently been looking into C and I want to start learning it, mainly because I am interested in how it works as a low-level language after using Python for a while, and eventually plan to take on Gentoo Linux and Linux From Scratch (with prior understanding of C and compilation). I welcome any advice!

8 Upvotes

16 comments sorted by

10

u/Reasonable-Tour-8246 6d ago edited 5d ago

I recommend using the book K&R The C programming. It's a short book about 270 pages only but it's dense, pick it study each and everything deeply you might spend hours in few pages because the design book was made so(much compressed). Also do each and every exercise in it...

4

u/bearheart 6d ago

K&R — The C Programming Language — is a great book. It's simple and accessible. Just keep in mind that the language has evolved a bit since it was written.

1

u/RDGreenlaw 6d ago

Second Edition of K&R works with modern C compilers. The first edition uses constructs that will not compile without using switches to force older compiler behavior.

2

u/3lml 5d ago

That one is absolutely the best despite its age. I can also recommend "Programming in C" by S. Kochan and a bit more advanced "Patterns in C" by A. Thornhill, which is an interesting take on how to do stuff usually associated with C++ in C.
There's also this guy Jacob Sorber who makes absolutely brilliant short youtube videos about different aspects of C programming.

1

u/Hot_Technology8777 6d ago

bit of an old book and a bit more inclined for intermediate C programmers. A good book nevetheless

-1

u/AegisRaspin 6d ago

Is this a book I can find online for free (Like some python crash course books), or do I have to buy it?

4

u/Mediocre-Pumpkin6522 6d ago

You can find pdfs online. Finding the links is your homework assignment.

7

u/podule-games 6d ago

It's only recently people refer to C as a low-level language, it's actually a high level language in the sense that it is abstracted from machine language.

I second the advice to learn an assembly language if you actually want to understand how computers work at a low-ish* level. C won't teach you how computers work, it'll really only teach how a modern OS presents memory to user-level applications.

*I say low-ish because these days processors tend to run microcode that again abstracts how the processor actually works, the instruction sets you see are not the literal instructions the CPU runs anymore (like they were for older computers like 6502, Z80 or 68000). An assembly language is about as low-level as you can reasonably go though unless you get into FPGAs or something.

4

u/MathematicalHuman314 6d ago

To understand the bridge between C and what exactly happens within the machine I found Compiler Explorer to be quite useful. You can write C code snippets and it will show you the disassembled function/expression/whatever you wrote. That means it shows you what the CPU itself would do at the hardware level to run your code.

When writing C code you write text for a program called a "compiler" to process and output usually a program binary from your code. This is not a simple single step. Compilation itself consists of preprocessing, lexical analysis, parsing, semantic analysis and intermediate representation and assembly code generation.

This output is processed by another program called an "Assembler". It takes this generated assembly code and turns it into machine code or binary code.

Finally, a program called the "Linker" takes this machine code file which contains the binary instructions for the CPU to execute along with extra data and maps them together with your operating system's start up code such that your program can be run on your system. So in summary:

C source code => compiler (gcc) [=> Assembler => Linker]

The reason i used brackets after gcc is because in practice when you compile a program with "gcc file.c -o file.bin" you will find a compiled binary and not a file full of assembly code. This is because gcc is a huge tool which combines all these smaller tools into one. You can even use gcc and make it stop at intermediate representation! You can even actively see what gcc does while compiling i.e. using the compiler, assembler and linker binary when running that same compilation command with the "-v" tag for verbose feedback.

None of this knowledge made installing gentoo any easier or enjoyable for me. Wish you luck!

1

u/Hot_Technology8777 6d ago

My setup on one of my laptops is Linux mint, using the i3wm which is a windows manager so you can easily have multiple windows open and various other things. Learn vim if you haven't already it makes things so much quicker, and ideal for C since you're typing more code than other languages usually.

2

u/AegisRaspin 5d ago

I use Vim as my main text editor/code writer, especially because I don't have any high-end machines (and would prefer vim over VSCode anyway).

And for i3wm, I'll give that a look as i use X11 right now.

3

u/__rituraj 5d ago

The K&R C programming language book!

Its what you need!

4

u/Electronic-Yam-69 6d ago edited 6d ago

If you're really just interested in how computers work at a low level I'd suggest reading about assembler on an older chip (8086 or 80386). C is more or less directly translatable into assembler, but without knowing a chip some of the decisions they made in the design of  the language can be a little confusing.

2

u/west_sahara 6d ago

I found this to be good and easy to understand https://beej.us/guide/bgc/

It’s intended for people who already know programming in other langs (python included)

1

u/No-Protection-7019 6d ago

Commence LFS avant gentoo. Quand tu sera sous gentoo tu aura déjà eu un maximum de galère et ça va rouler tout seul. L'inverse non. Et quand tu sera vieux, tu finira sous debian comme tout le monde 😄

1

u/lucas_c1999 6d ago

After learning the basocs start a small project. Preferavly something visual so that it can keep you motivated. I cant recommend Raylib for small games enough!