lainchan / λ
self teaching C as first language
Lucky7 · 2022-06-19 21:55
hello lainons, I've decided to learn C as my first language.I plan on being self taught and would like to know what tips/advice anyone has?the books i plan on studying so far are C programming: A modern approach,practical C programming ,and The C programming language.Any help is much appreciated
TL:DR : how to self teach C?
Emmanuel · 2022-06-19 23:53
>>31536
The best method I found was to learn enough to do something basic, then just jump in. Find a fairly easy project you want to do and do as much as you can until you get stuck, search for the best answer, rinse and repeat. The more you do, the more you'll learn, and the less you will need to search for an answer; in my experience, it's similar to learning a new language: there's a lot of programs and books that will teach you, but having conversations with people that actually speak the language will teach you faster than anything else once you've learned the basics.
Any reason why C and not C++? While both are great to know, modern C++ has a few more modern features, and that knowledge can be mostly carried over to other languages.
Oh, and shave your head before you learn, it's less painful than tearing your hair out once you start.
Lucky7 · 2022-06-20 00:04
>>31537
thank you for the response,ill be sure to write this down, and i have no particular reason i chose c.i have heard that its the "mother of languages",however.i will probably go t0 c++ next
cond · 2022-06-20 04:00
First off, don't fall for people telling you to read the original K&R books. They're quite outdated at this point as first resources. They're fine to read after you've learned C though. The best book in my opinion is C Programming: A Modern Approach by K.N. King. The author does a stellar job at describing concepts in the language (and has enough perspective to foreshadow/introduce future features/concepts in an intelligent way that fulfills curiosity and sets up the reader to learn effectively). That's honestly all you need at the moment. If you aren't on Linux hop on it now, either by installing it on an empty disk (or a disk you're prepared to destroy the data on). Its far superior to Windows for software development and the book focuses on using GCC.
GCC is very easy to use. Open up a terminal and type "gcc -o [name] [C file]" for a quick and easy compilation. I recommend using -o [name] as compiled binaries default to a.out and every new binary will overwrite the previous one of the same name. To run your program type ./[name].
program.c
gcc -o test program.c
./test
Programming languages are an ecosystem and you must understand this and be able to visualize it in your mind. C is a standard (Like C99 (ISO/IEC 9899:1999)) that is implemented by compilers (such as GCC or Clang/LLVM) for use in a wide variety of areas, typically userspace applications running on top of an operating system (like Linux or FreeBSD) that interact with the underlying OS via system calls (POSIX). You can peek inside of a compiled program using tools such as objdump, radare, and strace (for analyzing a programs system calls as it runs). All of this should come later...
list · 2022-06-20 04:24
>>31539
>If you aren't on Linux hop on it now, either by installing it on an empty disk (or a disk you're prepared to destroy the data on). Its far superior to Windows for software development and the book focuses on using GCC.
you dont need to install linux to use gcc, mingw and tdm-gcc are always options
Crash Override · 2022-06-20 05:19
>>31539
awesome ,ty i appreciate ur advice
pop · 2022-06-20 05:21
>>31540
its ok,im alr on arch
Ivan Petrov · 2022-06-20 07:48
>>31536
hacking around with FOSS projects written in C that you're interested in
>>31540
mingw is nothing at all like using gcc in a gnu/linux distro
it has its own libraries and APIs for windows and is generally just a completely different programming environment despite having GCC code
Tank · 2022-06-20 10:30
>>31536
I'm quite fluent in C myself, it's the primary language that I use for all my projects from signal/audio processing to games.
I actually never read a C book, I learnt primarily from online tutorials, bits by bits, but 99% of my skills are from personal projects.
First you'll write tons of small useless projects, probably not finish them and that's okay. Then as you get more confident you start writing actually useful stuff, especially once you get used to linux/UNIX environments and start to get ideas for some cool tools.
As >>31539 said, it's a good idea to understand the context in which a C program runs, that would have saved me massive amount of time. Understanding the difference between the stack and the heap, system calls, static vs dynamic allocation, etc.
Finally please remember that C has no exception handling or builtin semantics dedicated to errors. So check those return values, always have a clear idea of whether a function *can* fail and if it does, how do you know it did. malloc/realloc can return NULL, fopen can fail for many reasons, etc. Check the return values. In my opinion that's the difference between amateur-ish and professional code. That and properly freeing resources. None of this probably makes sense if you just got started in C but keep it in mind for when you need it.
Vasya Pupkin · 2022-06-20 11:33
>>31542
>arch
You could try installing suckless' dwm and st and playing around with configuring them to get used to the syntax of C before you start learning how to actually write it
Nguoi La · 2022-06-20 12:15
>>31536
Write all examples yourself and try modifying them. Do all exercises. Try doing some exercises at https://projecteuler.net/ and https://exercism.org/
> C as first language
I would learn a high-level language like Lua first. Lua is also so small that you can know the whole language.
https://www.lua.org/pil/contents.html
>>31537
>>31538
>C++
I recommend you read PPP2. It's great book. C++ Primer is also a great book.
>>31539
This.
>gcc
I recommend using more CFLAGS like: -std=c99 -Wall -Wextra -Wformat=2 -Wformat-truncation -fverbose-asm -masm=intel -Og -g3 -ggdb
(perhaps even -pedantic)
Drifter · 2022-06-20 12:24
>>31550
>exercism.org
I'm not making an account for that but it does look pretty good. What do you actually get access to if you make an account?
Cowboy · 2022-06-20 14:58
>>31537
>>31538
This is pretty good advice and as someone who started with C and moved to C++ I can recommend you do the same. It's far better to be a C/C++ programmer who started closer to the metal and went up than a C++/Python programmer who started really abstract and went down. You will also understand the value and utility of newer languages more when you've written C.
Crazy Jake · 2022-06-20 20:25
>>31536
Step 1. Get the Kernighan and Ritchie book, read it, do the exercises.
Step 1.5. Learn how to debug a program. How to use a debugger, set breakpoints, read variables. How to use printf statements to track down the source of a problem.
Step 2. Browse through documentation for the C Standard Library. Write tiny test programs that use the features of the library so that you get the mental memory of using them.
Step 3. Use what you've learned to write a SMALL program that you want to write. A blackjack game, a calculator, something simple and easy that you can finish in a week.
Step 4. Read the C17 spec. Learn what the new features of the language are. Read your compiler's documentation to see if it supports the new features.
progn · 2022-06-23 19:09
>>31551
All the exercises on the site. It doesn't hide any of that behind a paywall or try getting you to buy stuff.
FILLER · 2022-07-12 01:41
>>31536
Just read K&R 2nd ed. and then read up on what's new in newer standards. Don't read >>31539 at first, K&R is much better as an introduction and has better spoken.
Ein · 2022-09-25 14:04
I heard that Haskell works maths-alike, since I've never had any problems with maths I decided to have it as my first language, currently learning with this book and I'm understanding pretty much everything.
So, any tips for someone who is self teaching Haskell as its first language?.
Unspecified · 2022-09-26 10:29
>>32857
You don't sound like you need much advice, though?
The typical 'Haskell beginner' advice is not to obsess over understanding every abstraction to the bottom, but instead getting used to how things are done and letting them click over time. Depending on how good at math you are, you may need this advice a bit less than most people.
Then there's the 'Haskell as a first language' advice, and I suspect you know the gist of it already. Should you want to learn another language afterwards (you should), you must know that almost every language will be entirely different, and almost every other language will allow in-place mutation of variables (and often enforce it as the primary programming mechanism). Should you want to write software that's efficient down to the byte and to the cycle, you must know that, with the current state of technology, you'll need to learn another language. What else is there?
Dozer · 2022-09-26 14:29
>>32860
W-well I was planning of learning how to use Bash on the terminal afterwards, but that's about it
bit · 2022-09-28 07:38
>>31536 Op and other lainons why would you learn C as your first language. I always heard that javascript was the best language for beginners.
I dream of being able to make websites, should I also start with C/C++ ?
Dox · 2022-09-28 10:48
>>32870
>I dream of being able to make websites
Learn html, css and javascript.
Incognito · 2022-09-28 18:39
handmade hero was how i learned
Case · 2022-09-30 06:32
>>31536
I'd recommend first installing a Linux distribution.
It really doesn't matter which one.
Then you're going to want a text editor, again, it doesn't really matter, but I wouldn't recommend an IDE.
Go through K&R, do the exercises.
Read the chapter on pointers, and memory allocation. Then work on some small projects. I can personally recommend recreating functions like putstr(), or atoi(). Eventually a good project is writing printf from scratch.
Learn how to use a debugger like gdb, and learn how to write makefiles.
Once you've done that you're on a good path.
If you're interested in graphical stuff, give raylib a try, if you know a decent amount of C you can write small games pretty easily.
The most important thing is that you have to keep pushing the limits of what you know.
With each project you should be learning something new.
Treat memory leaks as serious errors, it doesn't pay to be sloppy.
If you need help, ask here, or elsewhere online. If you know programmers IRL, ask them.
If you work hard, you'll be a really solid programmer a year from now.
Good luck Lain.
pop · 2022-09-30 14:57
>>32863
>W-well I was planning of learning how to use Bash on the terminal afterwards, but that's about it
I have two recommendations:
(1) read https://linuxcommand.org/tlcl.php
(2) do not use Bash (or any sh) for actual scripting, unless your script is very trivial. Instead, use a proper scripting language (like Perl or Python).
>>32870
>Op and other lainons why would you learn C as your first language.
The reasoning is that C should teach the novice programmer to think before they write. Learning asm is better if you want to learn how a computer operates (https://savannah.nongnu.org/projects/pgubook). But if you want to understand UNIX/Linux, you need to know C! However, C and C++ are bad first languages, instead it's better to learn a high-level programming language (like Python or Scheme) because they give immediate feedback to the programmer.
>I always heard that javascript was the best language for beginners.
Lol no. JavaScript (just like PHP) makes it very easy to make errors that are hard to detect (even by experienced programmers!) and JS, like PHP, has some really weird quirks and questionable design decisions. You should either learn the language you specifically want to learn (but no JS or PHP) or something like Scheme or Python. Scheme is better for pedagogical reasons but Python is the more pragmatic choice (and it's certainly more popular).
Here are some links:
>https://www.gnu.org/software/guile/ (Chicken scheme or Racket in R5RS mode can also be used) + Little Schemer + https://people.eecs.berkeley.edu/~bh/ss-toc2.html
>https://automatetheboringstuff.com/ + https://greenteapress.com/wp/think-python-2e/ (+ perhaps also Mark Lutz's books)
I recommend you start with Python if you are unsure (after learning basic HTML+CSS).
>I dream of being able to make websites, should I also start with C/C++ ?
No. In that case, you shouldn't learn C or C++. Instead you should begin with HTML+CSS:
>https://www.w3schools.com/html/ + https://www.w3schools.com/css/ (but do not use W3Fools for anything other than learning HTML+CSS because the rest of the tutorials on that site suck)
>https://developer.mozilla.org/en-US/docs/Learn/HTML
>https://neocities.org/ (also join the Lainchan webring!)
>https://eloquentjavascript.net/ (read this only after you have learned to program in another language!)
Install a good text editor, like VSCodium, NeoVim, Gnu Emacs or Geany/Notepad++ (try all of them and see which one you like the best). Learn to use your web browser's "inspect element" tool and other developer tools (like the error console).
>>32883
>I'd recommend first installing a Linux distribution.
This, I recommend Xubuntu. First use Windows disk management tools to shrink a partition, then use Rufus to make a flash drive (LiveUSB) that can used to test and/or install Xubuntu.
() · 2022-10-01 11:58
>>32883
While K&R is great, I've heard the code in it doesn't compile on modern compilers, idk if there's any updated version of the code so a beginner doesn't get tripped up by that issue.
Yamaoto Toshi · 2022-10-03 02:44
>>32897
It's true you can't copy and paste straight from the book, however the changes needed are very minimal.
The first function in K&R is defined as such
main() {
printf("Hello, world!\n");
} The only change you need to make for this to compile is adding "int" before main.
C is a very stable language, so you won't have any difficulties with differences.
protected · 2022-10-05 00:44
>>32897
k&r was written for c89/ANSI C, code compiles if you target c89. for gcc: change/add the `--std` flag to be `--std=c89`, then the snippet >>32919 works (if you also remember to also add "#include <stdio.h>" before main)
Haruyoshi · 2023-02-01 10:19
Before you start, you're going to need a linux OS.
If you don't have access to a spare PC, then you can easily set up a virtual machine.
Windows will come later.
You'll need an editor as well. I'd recommend Vim, but essentially any terminal editor will work.
Having the above, write:
A Fibonacci sequence solver. It takes a single command line argument, the length. It should work for all of these lengths:
1, 1000, "20"
And fail for all of these:
-1, Q, one, "+", 10 1, "10 1"
Success/failure should be reflected both in the program return value and in the STDERR message.
Next, you're going to write a program which will read a .csv file and summarize the content into a JSON output file. The program will take the infile and outfile as arguments, report any errors in formatting to STDERR, etc. It must accept both CSV and CSV2 files, *including* long-quoted text sections. The outfile should provide min/max/mean/geomean/median for numeric fields, and text length for textual fields.
Finally, you're going to write up a GTK+ application which runs "rm -rf /" when you click the "hello world" button.
BloodCat · 2023-02-03 02:39
>>31536
>how to self teach C?
Just get a compiler and read "the C programming language" and follow the exercises. For something fun, you can look at game engines like quake, idtech3 or modern projects built on them like quakespasm, ioquake3, iortcw, or openarena. There's another FPS more like Counter Strike built off idtech3 or ioquake3, can't remember the name of it. But you can just look at those programs and poke around and change things. There are other engines in C too but I don't really care since these are fun enough for me.
>>32870
>I dream of being able to make websites, should I also start with C/C++ ?
Start with just HTML and CSS. JavaScript should be lightweight and client-side only, don't worry about it for now.
All you need is a plain text editor to get started. You could download and look at some free css/website templates, those aren't as popular anymore what with bloated frameworks like bloatstrap, but it's something to look at and learn from. You could use bootstrap for a bit and make some offline/test sites with it, but you'll probably find there are better ways of doing things and you can just make your own CSS easily enough. Most people who use stuff like bootstrap fall into a trap of their site looking generic and without any original creative character like the old web used to be.
And when you get around to using JS (if you need it for a special video player or ajax or something), don't use a bunch of libraries and frameworks and bloat, and if you have to use something like jquery, just use one and rehost their .js file on your own site for security and so your site doesn't break if theirs goes down (you'd think it'd be obvious, but plenty of sites still do it). Ideally, your site should work without JS and it should only enhance the features of your site when users enable it like making a form submitted by ajax instead of changing the page or submitting to the same page.
>>32887
PHP is great and it's really nothing like JavaScript since you can't run it client side. It's easy to make bad practices because it's a language that respects the programmer, but it's also easy to read the references on php.net for pretty much everything you really need. Anything more there's usually someone who already asked what you need on some forum, just use a decent search engine. I don't know why people love python so and swear by it for almost everything, I can't stand it personally.
Hououin Kyouma · 2023-02-09 17:22
https://savannah.gnu.org/git/?group=c-intro-and-ref
drop · 2023-02-09 17:29
cvsGNU C Language Intro and Reference - Git Repositories
make c.pdf
Look in the Makefile