Introduction
Preface
In this article, I'll be going over how to write your first program in C, and how to compile and run it! I'll also explain some of the basics of C without going into to much detail. I'll be compiling and running the program from the terminal.
Prerequisites
In order to follow along, you'll need the following:
- A C compiler
- A text editor, or an IDE
In this tutorial, I'll be using GCC's (GNU's Compiler Collection) C compiler, if you're using an IDE, usually there's an option to launch the terminal from within it, and usually it'll also have GCC preinstalled.
Creating a C File
Using your IDE, terminal, or simply your operating system, create a file and name it "main". If you're on Windows,
make sure to add the ".c" extension in the end for convenience.
This is where our main function will be, and this is where the program begins executing code statements.
The Main Function
As mentioned earlier, the main function, is where the execution of your code begins. The syntax of a main function is simply:
int main(){
return 0;
}
If you have programmed before, I assume you'll need no introduction. For those uninitiated, everything well get clearer throughout the rest of the articles, where variables and functions will be introduced and explained. This phase is about learning the basic structure of C code, and how to compile it. For now, copy it as it is into your code.
Printing Text
At the moment, the code would do nothing if we were to compile it as it is, let's change that and learn how to print text
onto the terminal. IO (input and output) is handled by The Standard C Library or libc for short.
libc, in non-technical terms, is a collection of function sets that we can use in our code that provide
utilities such as printing or calculating roots of a number. A function, in simple terms, is a reusable segment of code,
that serves a specific purpose, and can take an input, formally, parameters or arguments, and execute code
based on that input.
To print text onto the terminal, let's use printf, which stands for print formatted. printf, at non-trivial bare minimum (most simple use), takes in a string as a parameter, and prints it. To be able to use it, we need to include the relevant header file from libc:
#include <stdio.h>
int main(){
return 0;
}
The added line specifices that all the code inside of stdio.h (standard input and output), will be added during compilation, which allows us to call it's functions. Let's call printf, and pass the string "Success!" to it as an argument:
#include <stdio.h>
int main(){
printf("Success!");
return 0;
}
And now our code is ready for compilation!
Compiling
Windows
Simply open up the terminal (press the Windows key + R and type CMD), and type the following command:
gcc -o nameYourProgram -std=c99 main.c
And if everything compiles successfully, you should see nameYourProgram.exe in your current folder.
For those who're unfamiliar with the terminal, you can simply find your user folder (Look at the filepath
before the command, and the last phrase after the last backslash is your user's folder).
And to run it just type its name (nameYourProgram.exe) in the cmd, and you should see the results. Make sure you're on the same folder as before.
Linux
Well, you're on Linux, so I presume you have a bit of an idea how to work with the terminal, so just enter this command (same as the above):
gcc -o nameYourProgram -std=c99 main.c
And then just run it by typing it's name in the terminal.
Debugging
If the you had problems compiling, then you'll need to debug your code. Let's go over some common issues, but before we do, see if you can figure it out on your own in the terminal, if not, then go over this list:
- Missing a semicolon ";" at an end of a statement.
- Make sure there is no trailing semicolon after the #include directive
- Missing header (Did not include <stdio.h>).
- main is written in uppercase instead of lowercase.
- some code lines are outside of main's block (see the code snippets above).
Or it could be that you type the compilation command wrong, or that you have a typo somewhere in any phase.
Epilogue
Congratulations! You have made the first step into writing your own C programs. Now, in the next article we'll learn about variables, arrays, and how to format printf. We'll also learn about strings in C. Until then!