20 ms·
Getting started with C
- yitchelle 6y agoMay be an unpopular opinion, but K&R 2nd edition is still the best beginner's tutorial for a fast start into the C for me. As the knowledge starts to build, better learning material and experience will also be consumed. If I had read this lightning fast guide, I would be a bit lost. After displaying "Hello, world" on the terminal, do I really want to learn how to display "Hello, World" in a GTK window?
- lasagnaphil 6y agoThe writer intended this tutorial for absolute beginners without any experience in programming - which is, to be honest, totally bonkers! It's not a bad guide for someone trying to learn C as their second/third language though, it teaches you how to prepare a build environment to use third-party libraries in C, which is seldomly documented this clearly compared to other random articles on the Internet.
- priyanshuraj 6y agoThe article uses nano throughout. Is nano a popularly used editor? I have not seen anyone around using nano. I see vim, atom, sublime, vs code but never nano. Just curious if nano is popular. If nano is popular what are its pros and cons?
- csande17 6y agoNano is the easiest-to-use terminal-based editor that you're likely to find on a Unix system by default. All the important key commands are listed at the bottom of the screen and everything! It's a great editor to recommend in beginner tutorials since it requires very little setup or learning to use, but if you're already happy with a different one there's probably no reason to switch.
- saagarjha 6y agoI was kind of surprised that they had installation steps for it, to be honest–macOS certainly ships with it.
- FrankSansC 6y agoI use it on a daily basis. It's simple, fast and has syntax highlighting. And it's still actively developed[0] (latest release: 2020 December 2 - GNU nano 5.4 "Terre des hommes") [0] https://www.nano-editor.org/news.php https://www.nano-editor.org/news.php
- nethunters 6y agoI've found nano really easy to pick up for quick introductions to editing files via a terminal. Nano seems to have a shallow learning curve for new users in comparison to vi, vim emacs as it works similar to other simple text processors. It isn't as powerful as other editors and can't be configured as much but for some, especially beginners, that's a benefit.
- Jonnax 6y agoPersonally it's simple and all the important shortcuts are displayed on the screen. If I ssh into a machine to change a config value I prefer it to vim/vi. I'm not going to write anything using it, but for quick edits it's perfect.
- slmjkdbtl 6y agoto be honest nano is the best thing in this article, everything else is just disaster, nano is at least light weight and functional (imagine using an IDE for this)
- pjc50 6y agoIntroducing newbies to GTK is a great way to show them a lot of code they don't understand, will take a while to understand, and will break in non-obvious ways.
- 0x008 6y agoI would be really interested in a course that actually holds true to the premise in the title here. I know my way around the JVM and python and started learning a little Rust on the side. However, I feel the need to at least dive my toes into C for a couple of weeks for completeness sake. Do you have a recommendation for something that is a little less than a book and a little more than learnCin10minutes?
- enriquto 6y agoJust read K&R for 10 minutes.
- vkazanov 6y ago:-) I wish people would stop recommending this outdated book, along with a few other default goto books (say, the Dragon book). Its super optimistic approach is more about the language K&R wanted C to be than the real thing.
- icedchai 6y agoI taught myself C using K&R when I was a teenager... back in 1991. The second edition was only a couple of years old.
- redprince 6y agoMaybe there aren't any good alternatives to "read this go to book as the first book on the topic". Prove me wrong. (Please)
- shadowofneptune 6y ago
- opheliate 6y agoThis is a very strange "getting started" guide to C, and I'm not sure who the intended audience could possibly be. The tone at the start really does seem to be aimed at "absolute beginners" ("[...] programs must be written in a very strict way. Miss a comma and the program will stop working"), but then it quickly gets into the weeds without much explanation. I'm trying to imagine an beginner reading this: The first thing we're told to do is "Installation", that makes sense. Then the first line says "The Apple provided C compiler is the LLVM clang compiler". But what's a compiler? How does that help me learn C? The tutorial hasn't even indicated that C is a compiled language, or what a compiler does at this point, and it doesn't elaborate on this throughout. We go on to a hello world example, and we're told to type in some code ("Remember: programming languages are very strict, so type it exactly"), and then we're given an explanation of what it's doing: Great! I can't wait to learn what this means. But the explanation uses terms that a beginner would not be familiar with: The very first line of the explanation is "the first line loads the stdio.h function library, which provides the printf() function, used to write a message on the terminal". What's a function? What's a library? Obviously these things are trivial to anyone with programming experience, but for an absolute beginner, these terms don't yet have an intuitive meaning, and they're never fully explained in the article. We compile that program (again, never being told what compilation is or why it's necessary), and then we're suddenly thrust into writing a GTK+ program. We haven't covered the absolute basics like types, pointers, etc, and yet we're given a huge chunk of (reasonably complex) code with no explanation, and just told to "read the code bottom to top" as an "additional hint". I think a much better title for this would be "getting started with C tooling", but even then, the article does a very poor job of explaining why such tooling is even necessary.
- tragomaskhalos 6y agoWrt "the first line loads the stdio.h function library, which provides the printf() function", you're right, "loading a library" is not clear and, what's worse, is actively misleading as actually we're just telling the compiler that there is a function somewhere called printf that will get "loaded" (linked) later on. I remember that this was one of the things that confused me most about C when I learned it.
- ohazi 6y agoTotally unrelated, but I'm surprised to see pacman being used on Windows. Really? Windows, btw?
- 533474 6y agoThat escalated quickly, poor beginners :').
- AQXt 6y agoExactly! The author assumes the reader doesn't know what a terminal is, and goes from a "hello world" to a cross-platform graphical "hello world" using GTK-3 without explanation. "Trust me, I don't have time to explain!"
- pjc50 6y agoYou can teach anyone anything in ten minutes if you don't care about understanding or retention!
- st1x7 6y agoThis strikes me as very out of touch with reality. You can't go directly from hello world to 100+ lines of 3rd-party code that uses pointers all over the place and just brush it off with "don't worry about the complexity".
- dr_j_ 6y agoI think a lot of beginners would be put off by the verbosity of this.
- tigerlily 6y agoI think this has merit not only for beginners to programming, but also for programmers who have never touched C. I like that it gets straight to business. Good intro, and props to the author for introducing it on three major desktop platforms, using modern build tools.
- mistro 6y agoAgreed, but not for beginners. I just about fall into the second category (I am a school teacher) - I have published (internally to my school) a couple of VB apps and written a lot of code in Python, but I have never touched C. I understand nearly all of the terminology and feel that I would be able to use this to learn more about C. Beginners (of whom I have taught many!) simply wouldn't be able to use it.
- AlunAlun 6y agoStep 1: write hello world in console. Step 2: draw the rest of the fucking owl?!
- jpm48 6y agoOne of my biggest bugbears on C / C++ programming is return 0; I wish people would #include <stdlib.h> /<cstdlib> and use EXIT_SUCCESS / EXIT_FAILURE just to make the code more obvious for the beginner on what the intent is. :-) To quote the "zen of python" "Explicit is better than implicit" we know what "SUCCESS" or "FAILURE" is! And we can use enums for other exit values.
- mav3rick 6y agoThere should only be one way to succeed. Returning 0 for success makes complete sense. You can add layers of macros or enums to sugar coat this but retuning 0 for the success case makes complete sense
- saagarjha 6y agoReturning either EXIT_SUCCESS or 0 is a valid way to terminate the program with an implementation-defined exit code appropriate for your platform.
- ajarmst 6y agoThe immediate next step after "hello world" is GTK programming? This is only "getting started with C programming" if your idea of C programming is copying magic incantations into an editor. Which, sadly, is increasingly being treated as programming in the StackOverflow era.
- Haga 6y agoLaw&Court: a fast introduction to become a lawyer. Bone&Blood: Study medicine in just 4 weaks
- snvzz 6y agoChrist. What is this nonsense, and how did it get into the front page. If by any chance you reading this and want to learn C, do obtain and read The C Programming Language 2nd edition instead.
- AQXt 6y agoCan AI generate a better tutorial? To test my theory, I asked the OpenAI API to complete the following sentence: This tutorial will guide you through writing the "Hello World" program in the C programming language. And here's the result: This tutorial will guide you through writing the "Hello World" program in the C programming language. Hello World Program Structure The "Hello World" program is a very simple program. It consists of two parts: the main function and the printf function. The main function is the first function called when your program starts running. It is responsible for starting all other functions and initializing everything that needs to be initialized before your program starts doing anything useful. The printf function is responsible for printing text to the screen. In this case, it prints "Hello World!" to the screen. The main Function The main function must have one argument which is an integer that represents how many arguments were passed into it from the command line (if any). The following code shows a simple main function: #include <stdio.h> int main(int argc, char *argv[]) { printf("Hello World!n"); return 0; } This code will print "Hello World!" to the screen when executed. Let's go over each line of this code: #include <stdio.h> This line tells the compiler to include stdio.h in our program so we can use its functions (like printf ). int main(int argc, char *argv[]) This declares a new variable called main . It has two arguments: argc and argv . These are special variables that are used by C programs to pass information into their functions from their command line arguments (if any). We'll talk more about them later on in this tutorial when we cover how to use command line arguments with your programs. For now, just know that they're there so we can use them later on if we need them! ;-) printf("Hello World!n"); This line calls our friend printf . It tells it to print "Hello World!" followed by a newline character (the n at the end). return 0; This line returns 0 back into our calling environment (in this case, it's our shell). If you don't know what returning values means or why you would want to do it, don't worry about it for now; just know that every C program must return a value back into its calling environment when it finishes running (we'll talk more about this later on in this tutorial). Pretty good, eh? (I just formatted the text for clarity.)
- krebs_liebhaber 6y agoI want to believe this is real.
- the_only_law 6y agoC is the one language I think I can understand well enough to read, until I look at real code or see people complain about it.
- slmjkdbtl 6y agoI understand the motive to let readers get up and running quick, getting some graphical window instead of terminal black box, but if you think users want to avoid terminals and get to graphics, why spend a chunk of the article talking about it? Just when readers starting to like the terminal and the culture, the author goes "anyway, terminal sucks, let's get a graphic window! here's 100 lines of code you'll never understand" I don't understand the bloat of introdocing gtk, meson and ninja, more deps means more code, more code means more bugs, more steps and more difficult to compile, I'm sure a lot of people will fail to compile this, which is very frustration for beginners who're not familiar with error messages. You might think meson and ninja makes it easy for building, but that's wrong, that might be true for engineers who build 1000 times a day, not for one off beginner situations like this. Abstractions is for people who understand why the abstraction is needed.
- pharke 6y agoI'd recommend An Introduction to C & GUI Programming by Simon Long[0] instead. It's much more beginner friendly and is very well written. Doesn't go super in depth but it's a quick intro book so it shouldn't. Plus it's a book and it's cheap/free; [0] https://magpi.raspberrypi.org/books/c-gui-programming https://magpi.raspberrypi.org/books/c-gui-programming
- fifticon 6y agoIt is funny if this was not intended as funny. An instance of "now draw the rest of the owl" meme. I can see the need to teach a bit of the surroundings of C, but it ended up having very little to do with C.
- carapace 6y agoI think you nailed it: it's a "now draw the rest of the owl" gag. Very subtle.
- barrenko 6y agoLearned a bit of C, not convinced it had any real benefit besides learning about pointers kinda drives things home for you about passing stuff around by values or reference if you are a self-taught Java / C# developer. What set me on this path to enlightenement was an e-mail article from Dan Abramov (https://github.com/HackYourFutureBelgium/just-javascript/tree/master/04-counting-the-values-part-1 https://github.com/HackYourFutureBelgium/just-javascript/tre...) - here's an excerpt: "We will learn about the JavaScript world for what it is — without thinking about how it’s implemented. This is similar to how physicists can talk about properties of stars without answering the question whether the physical world is real. It doesn’t matter! We can still describe it on its own terms. Our mental model does not attempt to answer questions like “How is a value represented in the computer memory?” The answer changes all the time! In fact, the answer to this question changes even while your program is running. If you heard of a simple explanation about how JavaScript “really” represents numbers, strings, or objects in memory, it is most likely wrong. To me, each string is a value. Not a “pointer” or a “memory address” — but a value. In my universe, a value is good enough. Don’t allow “memory cells” and other low-level metaphors to distract you from building an accurate high-level mental model of JavaScript. It’s turtles all the way down anyway! If you’re coming from a lower-level language, set aside your intuitions about “passing by reference”, “allocating on stack”, “copying on write”, and so on. These models of how a computer works often make it harder to be confident what can or cannot happen in a JavaScript program. We’ll look at some of the lower level details, but only where it really matters. They can serve as an addition to our mental model, rather than its foundation. ... As for these strange visions, I don’t pay as much thought to them anymore. I have wires to point, questions to ask, and functions to call. I better get to it! The stars are bright when I look at them. Are they still there when I blink? I shrug.
- dcolkitt 6y agoThe most critical prerequisite to being a good C developer is to really understand pointers on an intuitive level. The biggest block is that most people don't take the effort to develop mental fluidity. So when they see something like a dereference to a pointer of a pointer, or a cast from a pointer to an array, it interrupts flow while they consciously work out what's going on.
- mywittyname 6y agoPersonally, my major issue with pointers is the C syntax for them. While I understand why the & and * symbols were chosen for the task way back when, but it feels like a kludge. Smart pointers in C++ provide a more logical and consistent interface for using pointers.
- Layke1123 6y agoI would be really interested in reading about why & and * were used as the symbols for pointers when C was originally conceived. Can you share a link or point me in the right direction for why that is? Please and thank you!
- zokier 6y agoI'd say the most critical thing is to understand the relationship between ISO C standard, compiler implementations, and you, the programmer. Understanding that "seems to work" is not good strategy to deal with C, nor is trusting that things work in a sensible manner. Seeing how nasal demons might lurk at every corner. Distrust all advice and guidance that is not founded upon current C standard and/or compiler documentation. If you internalize that mode of thinking, then pointers will be just a minor detail in the end.
- NDizzle 6y agohttps://viewsourcecode.org/snaptoken/kilo/ https://viewsourcecode.org/snaptoken/kilo/ This is the "getting started with coding" guide that I give to people who ask me about these kind of things. This is a defense mechanism - I tell them that I'll help them get started with a career in coding if they return me their completed text editor. So far nobody has, so maybe it's not the best beginner tutorial? I still thought it was a great tutorial.
- divtiwari 6y agoI have completed about 45 steps of this tutorial, totally love this!! Antirez is a total wizard. This is totally a no bullshit intro to C.
- aazaa 6y agoThe absolute beginner technical guide may be the most difficult kind of writing there is. You have to know what's already in the head of the absolute beginner. If it's garbage, you need to set that straight before anything else can happen. You also have to know what's not in the head of an absolute beginner. Then you need to fill the gaps with background material. Not too much or your audience will tune out. Not too little or you'll lose your audience in a puff of confusion. There needs to be just enough instant gratification to keep the reader with you. Many concepts you've mastered are tangled together in your head, and you'll realize this the minute you start trying to write about them. Before you started writing, you never noticed what a hairball of concepts you'd mastered, and if you don't expend some serious effort and energy unpacking it all, your absolute beginner guide will read more like a dissertation. You'll need to untangle each and every concept for the beginner audience. That means getting creative with how information is presented. With what information you reveal, and what information you simplify - but not too much. You should start thinking about ways to convey ideas graphically, which could excite you as much as it horrifies you. A good image can take as much effort as 2,000 edited words. You start off thinking that it will be easy. And then you realize you've vastly underestimated how much material there will be to get where you wanted to be. If you haven't given up in complete frustration at the size of the undertaking in front of you, scale back your vision for the guide and repeat. It may take a few iterations before you know what you're doing.
- ryandrake 6y agoYea, if I were writing something with the bold title "Absolute Beginner's Guide To..." I'd want to test it on my 8-year old who only knows how to launch the web browser and Minecraft. If she can use it successfully to cobble together a working Hello World program, then it's indeed an absolute beginner's guide.
- wonder_er 6y agoNot only can she cobble it together - it has to be something that helps her feel a sense of pride, dignity, and understanding. Some guides get someone to "the end", but they don't scaffold an understanding of the underlying principles, and the student feels even more confused at the end than at the beginning. I always go to great lengths to show the mistakes that I make all the time, and to show how much of a given domain I don't know. For example, in a Make an OSS contribution walk-through I did, I mention over and over how myself or the senior dev I was pairing with didn't understand something. [0] Most of the job of a modern software developer seems to be surfing across an ocean of complexity, avoiding everything except the _one specific issue_ we're working on. To persons unfamiliar with the field, they might think that we actually understand everything to any degree of depth. They would be deeply mistaken. [0]: https://intermediateruby.com/matt-swanson-jekyll-bug-p2 https://intermediateruby.com/matt-swanson-jekyll-bug-p2
- dezzeus 6y agoDespite some (already mentioned) shortcomings, I like it: it provides a simplified "real life" big picture of the whole process with sample tools; a thing that (in my experience) every other "getting started" guide seems to miss (most of them just mentions what is a makefile). I also like the overall format of the website and the radio feature is a nice touch. I'll visit it again in the future, out of curiosity. A follow-up article may extend it with collaboration (git) or may delve in either Unix-like (e.g. with the GNU utils etc) or Windows, or both. But the author should first address the highlighted shortcomings...
- anthk 6y agoThe C programming language 2nd Edition from K&R is much better than this. Just compile all code with "cc -o file file.c", except for the math library, where you need to add "-lm". Use whatever editor you like. Nano, nvi, emacs, whatever.
- mraza007 6y agoVery interesting article and as someone who’s a beginner in C you definitely did a great job explaining