We've prepared the ground so far — now how about writing some actual code? What do you say?
Well, that's exactly what this chapter is about!
By the end of it, you'll have created your very first program!
Okay, sure, it'll be in black and white and all it will do is say "hello world" — so yeah, it'll be pretty useless. But it'll be your first program, and I promise, you'll be proud of it!
Let's go whenever you're ready!
Console or Window?
We touched on this briefly in the last chapter. Remember how the IDE asked what kind of program we wanted to create? I told you to choose console.
Well, here's the thing: there are really only two types of programs:
- Programs with windows (the correct term is graphical user interface, or GUI)
- Programs in the console
1. Windowed programs
These are the kinds of programs you already know. Paint, your web browser, Microsoft Word, 3D games running in a window — you name it.
I'm guessing you'd love to be able to make those kinds of programs, right? Well... not quite yet. 😜
Yes, it is possible to create window-based programs in C. But when you're just starting out, it's way too complicated! To begin, it's better to stick with console programs.
Which brings us to: what is a console program, anyway?
2. Console programs
Console programs are the originals, the OG.
Back in the early days, computers could only do black and white, and they weren't powerful enough to draw windows like we do now. They also weighted about a truck and a half or something.
Obviously, time has passed. Windows helped make computers "mainstream" thanks to how easy it was to use — mostly because of its use of windows for everything. The name should have been a giveaway.
Windows got so popular that today, most people have completely forgotten what the console even is or how it works.
Yes, you, don't look over your shoulder — I know you're wondering what on earth I'm talking about.
Here's the big news: the console isn't dead!
In fact, Linux brought it back into the spotlight. Especially in more advanced distributions — think Slackware or Linux Server, not Debian or Red Hat — where using the console is practically mandatory. No graphical interface at all. Want to open a web browser? You type out commands in a console, and the computer sends an HTTP request to a server online. Spooky, right?
So, now you've got a bit of an idea of what the console looks like.
A few quick points though:
- As you'll see, we can show color nowadays, so it's not all black and white anymore.
- The console isn't the most beginner-friendly environment out there.
- But it's actually a very powerful tool once you know how to use it.
As I mentioned earlier, creating console-based programs like we're about to do is super easy and perfect for beginners. (Which definitely can't be said for windowed programs!)
It does! It's just a bit... "hidden," let's say. 😉
You can find it by going to Start / Accessories / Command Prompt, or by clicking Start / Run and typing cmd
.
So, if you're on Windows, that little black window is where you'll be running your first programs.
If I've chosen to start with simple console programs, it's not to bore you — far from it! By starting with the console, you'll learn the core foundations you'll need later to build windowed applications.
Window-based programs aren't that much more complex (these days we have plenty of tools that make them easier), but they do require a lot more code. And when you're just starting out, having to read through 100 lines just to understand what's going on isn't great. With a console program? Five lines of C is enough.
So don't worry: once we've got the basics down, we'll absolutely learn how to create windowed programs too. 😊
A minimum of code
For any program, you'll need to write a minimum amount of code. This code won't do anything fancy, but it's essential.
That's what we're going to look at now: the minimum code needed to get a C program up and running. This will serve as the base for most of your programs in C.
Let Your IDE Generate the Minimal Code
Depending on which IDE you chose in the previous chapter, the process for creating a new project might differ. If you've forgotten how to do it, take a quick look back at that chapter.
Just as a reminder, in Visual Studio Code (which is the IDE I'll be using throughout this course), go to the menu File / New / Project, then select Console Application and choose the C language.
Visual Studio Code will then generate the minimum C code we need. Here it is:
#include <stdio.h> #include <stdlib.h>int main() { printf("Hello world!\n"); return 0; }
Take note of the blank line at the end of this code. Every C file should ideally end with an empty line like that. It's not a big deal if you forget it, but your compiler might throw you a little warning.
Also note the line:
int main()
... can also be written like this:
int main(int argc, char *argv[])
Both versions are perfectly valid, but the second (more complicated one) is more commonly used. So, I'll probably stick to that version in future chapters.
That said, whether you use one or the other won't really change anything for now. No need to stress over it, especially since we're not quite ready to dive into what it actually means.
If you're using a different IDE, just copy and paste this source code into your main.c
file so that we're all on the same page.
Go ahead and save everything. Yes, I know, we haven't done much yet — but save it anyway. It's a good habit to get into! :p You should only have one source file, main.c (everything else are just project files generated by your IDE).
Let's Break Down This Minimal Code
This minimal code we just saw probably looks like gibberish to you right now. And yet, what I see is a console program that prints a message to the screen. Time to learn how to read this stuff! ^^
Let's start with the first two lines that look pretty similar:
#include <stdio.h> #include <stdlib.h>
These are special lines you'll only find at the top of source files. They're easy to spot because they start with a hashtag #. These are called preprocessor directives (yeah, sounds fancy, doesn't it?). They're instructions that get processed by something called the preprocessor, which runs at the start of the compilation process.
Like I mentioned earlier, what we saw before was just a very simplified view of compilation. There's actually quite a bit going on behind the scenes. We'll go into more detail later, but for now, just remember to include these lines at the top of each file.
Good question!
The key word "include" in means... well, "include". These lines are asking the compiler to include certain files — specifically, to bring them in as part of the compilation.
There are two lines here, so we're including two files: stdio.h
and stdlib.h
. These are already-existing files, ready to use. Later on, we'll learn that they're part of what we call libraries. Basically, they contain ready-made code that lets us do things like print text to the screen.
stdio
stands for "standard input/output" and lets you display text (among other things), while stdlib
deals more with memory management and handy math functions.Without these files, printing text on the screen would be mission impossible. The computer, on its own, doesn't know how to do anything. You have to tell it everything. Talk about a struggle, huh?
Anyway, these first two lines include the libraries that will allow us (among other things) to print text to the screen fairly easily.
Let's move on. The rest of the code looks like this:
int main() { printf("Hello world!\n"); return 0; }
What you're looking at is what we call a function. A C program is made up of functions — almost entirely. For now, our program only contains one.
A function is, more or less, a way to group together a set of commands for the computer. Grouped like this, the commands perform a specific task. For example, you could create a function called open_file
that contains all the steps for opening a file.
The beauty of it is, once the function is written, you just have to say open_file
and boom — the computer knows what to do without you repeating all the steps! (Technology is amazing, right?)
Without going too deep into how functions work (we'll talk more about that later), let's at least go over the basics. The first line contains the name of the function — that's the second word.
Yep, our function is called main
. It's a special name that means main or principal. The main function is the entry point of your program. It's always where the program starts running.
A function has a beginning and an end, marked by curly braces {}. So everything in our main function is enclosed between those braces. If you're following along, our main function contains two lines:
printf("Hello world!\n"); return 0;
These lines inside a function are called instructions (that's one more word to remember). Each instruction is a command for the computer, telling it to do something specific.
As I mentioned earlier, by smartly grouping instructions into functions (that's your job as the programmer), you can build neat little reusable program blocks. Nothing's stopping you from creating a move_character function in a video game, for example, just like we talked about earlier.
At the end of the day, a program is really just a list of instructions: "do this" and "do that." You give your computer orders, and it follows them (well, if you've trained it properly).
VERY IMPORTANT: Every instruction must end with a semicolon ;
. That's how you know something is an instruction! If you forget to put a semicolon at the end of a line, your program simply won't compile!
Let's look at the first line:
printf("Hello world!\n");
This tells the computer to display the message "Hello world!" on the screen. When your program reaches this line, that message pops up, and then the next instruction runs.
Which brings us to:
return 0;
Basically, this means: "We're done!" :p (Yes, already). This line tells the program that it has reached the end of the main function and returns the value 0.
Well, every program, once it's finished running, returns a value — usually to let the system know how it went. 0 means everything went fine. Any other number usually indicates an error.
Most of the time, this return value isn't even used — but still, it's good practice to return something. Your program would work without the return 0, but let's be honest, putting it there is cleaner and more professional. So let's keep it :)
And there you go! We've just broken down how the minimal code works.
Sure, we didn't go super deep, and you probably still have a few lingering questions. But don't worry: all your questions will be answered, step by step. I can't drop everything on you all at once — otherwise it's guaranteed brain-meltdown time.
Speaking of which... you still with me?
If things are starting to blur, no worries. Don't force yourself to keep reading. Take a break, re-read this chapter when you're fresh. What we've just covered is fundamental — especially if you want to keep up later on :)
Testing Our Program
Testing this should be a breeze. All you need to do is compile the project and then run it (just click the "Build & Run" icon in Visual Studio Code).
If you haven't done so already, it'll ask you to save your files. Go ahead and do that.
After a painfully long wait (a.k.a. compiling :p), your very first program will appear before your eyes — eyes now filled with overwhelming joy.
The program prints out "Hello world!" (that first line).
The lines underneath were generated by the compiler — they tell you the program ran successfully and how long it took to run.
You'll be prompted to press any key to close the window. That's it — your program ends.
I know, I know — it's basic, it's ugly, it's whatever.
But hey! It's your very first program, a moment you'll remember for the rest of your life :-°
...No?
Alright, before you slip into your first programming-induced depression, let's move on, shall we?
Writing a Message on Screen
From here on out, we'll start editing the code in that minimal program ourselves. Your mission, should you choose to accept it: display the message "SiteRaw" (the name of this site, or your own name or whatever) on the screen.
Just like before, a console should open up. The message "SiteRaw" needs to appear in that console.
Turns out, it's actually pretty simple. If you start with the code shown earlier, all you have to do is replace "Hello world!" with "SiteRaw" in the line that uses printf
.
Like I mentioned before, printf is an instruction. It tells the computer: "Display this message on the screen."
You should know: printf is actually a function, already written by other programmers long before you came along.
Remember these two lines?
#include <stdio.h> #include <stdlib.h>
I told you these lines allow you to include libraries in your program. Libraries are files packed with pre-written functions. These particular ones — stdio.h
and stdlib.h
— contain most of the basic tools we need to get things going in C.
stdio.h
, in particular, has functions for showing output on the screen (like printf) and also for getting input from the user (we'll get to that later).
Introduce Yourself, Insolent Program!
Inside our main function, we're calling printf. That's a function calling another function (main calls printf). And get used to that idea — that's exactly how things usually go in C: functions contain instructions that call other functions, and so on.
So, how do we call a function? Easy: just write its name, add two parentheses, and finish it off with a semicolon.
printf();
Okay cool, but that's not enough. You still need to tell it what to print. To do that, you pass in the text you want it to display — inside the parentheses, in quotes.
Just like we did earlier in the minimal code.
In this case, you'd write exactly:
printf("SiteRaw");
And I really hope you didn't forget the semicolon at the end — remember, that's super important! It tells the computer that the instruction ends right there.
Here's the full source code you should have right now:
#include <stdio.h> #include <stdlib.h>int main() { printf("SiteRaw"); return 0; }
So we've got two instructions telling the computer:
- Print "SiteRaw" on the screen.
- The main function is done — return 0. The program ends.
Here's What That Program Looks Like:
As you can see, the line with "SiteRaw" is kinda stuck to the rest of the text. Unlike earlier, there's no nice clean space underneath it.
One way to make our program look a bit more polished is to do a line break after "SiteRaw" (like hitting Enter on the keyboard ^^)
But of course, it would be way too easy if we could just press Enter in our source code and expect a line break to magically happen on screen! Nope — we need to use something called special characters...
Special Characters
Special characters are, well, special! They let you do things like start a new line, tab over, etc.
You can spot them easily — they're always made of two characters. The first one is always a backslash (\
), and the second is either a letter or number.
Here are two common ones you'll likely use a lot:
\n
= new line (like pressing "Enter")\t
= tab
In our case, to add a new line after "SiteRaw", we'll use \n
.
So to make the program start a new line right after the word "SiteRaw," you'd write:
printf("SiteRaw\n");
Now your computer knows to show "SiteRaw" followed by a line break.
Already looking better, right? :)
You can even write more text after the \n
— no problem at all. Anything after it just starts on the next line.
Try this little practice run:
printf("SiteRaw\nThe Best Website on the Internets!\n");
That will print "SiteRaw" on the first line, and "The Best Website on the Internets!" on the line below.
The Best Website on the Internets!
Let me know when you're ready for the next part — or if you'd like to keep practicing printing fun stuff like ASCII cats or dramatic declarations! 😄
Comments are useful
Before we wrap up this first chapter of "real" programming, there's one more awesome thing I have to show you — something called comments. No matter what programming language you're using, you can always add comments to your code. And C is no exception!
It means writing bits of text inside your code to explain what it does, what a specific line is for, and so on. It's seriously essential — even if you're a programming genius, you're still going to need to leave yourself some notes here and there. Comments help:
- You make sense of your own code later. You'd be surprised how fast you forget how your own programs work! ^^ If you take even a few days off, you'll be glad you wrote some notes to help yourself find your way back through your code.
- Other people understand your project. If someone else (who hasn't seen your code before) needs to work on it, comments will help them get up to speed way faster.
- Me, actually! Throughout this tutorial, I'll be adding comments to the example code so I can better explain what each line is doing.
There are a couple of ways to write comments, depending on how long they are.
For short comments:
If your comment fits on one line — just a few words — use double slashes (//
) followed by your comment. Like this:
// This is a comment
You can put this kind of comment on its own line, or right next to a line of code. That second option is especially useful, because it clearly tells you what that line does.
Example:
printf("SiteRaw"); // This line prints "SiteRaw" to the screen
Just a heads-up: this type of comment originally came from C++, but don't worry — it works totally fine in C (unless you're a purist, of course).
For longer comments:
Got more to say? Need a few lines to explain something properly? Then you'll want to use a block comment, which has a clear start and end.
- To start the comment, type a slash followed by an asterisk:
/*
- To end the comment, type an asterisk followed by a slash:
*/
Here's what that looks like:
/* This is a comment that spans several lines */
Let's take our "SiteRaw" program and add some comments to it, just for practice:
/* Below are preprocessor directives. These lines let us include files in our project — files called "libraries." Thanks to these libraries, we can use pre-built functions to do things like print a message to the screen. */#include <stdio.h> #include <stdlib.h>
/* Below is the program's main function, called "main." Every C program starts here. Right now, my main function just displays "SiteRaw" on the screen. */
int main() { printf("SiteRaw"); // This instruction prints "SiteRaw" on the screen return 0; // The program returns 0, then stops }
That's what our program looks like with a few comments ;)
Sure, it looks a little longer now, but it's actually the exact same program as before. When you compile the code, all the comments are ignored — they won't show up in the final program. They're just for the programmer's eyes.
printf
prints a message — you don't need to explain that every time.It's better to comment sections of code instead. That way, you give a general idea of what a group of instructions is doing. If someone wants to dig into the details, they'll be smart enough to figure it out themselves.
So remember: comments are there to help the programmer navigate the code. Try commenting blocks of lines, not each line one-by-one.
And to wrap up with a fun bit of programming culture, here's a quote straight from IBM:
"If, after reading just the comments, you can't understand how a program works — throw it in the trash!" — IBM House Rule
No idea if true or not, but as a general rule it makes sense.
Enjoyed this C / C++ course?
If you liked this lesson, you can find the book "Learn C Programing for Beginners" from the same authors, available on SiteRaw, in bookstores and in online libraries in either digital or paperback format. You will find a complete C / C++ workshop with many exclusive bonus chapters.