Your First C Program

C is a difficult language for beginners. Implemented features can work perfectly fine in one context, but crash in another. It demands that the programmer must know how each of the features works, and also how to use them. It is vital, as a C programmer, to understand everything that is typed. This language is not as forgiving as many modern languages i.e. Python and JavaScript. So, we will start off with a simple program but instead of just typing in the code and seeing that it works, I would like to explain, in as much detail as I can, what each line is and what is behind it. Below is a simple C program that prints Hello to the console. I will be explaining this operation on Windows Command Prompt (Cmd.exe).

C

                                
#include<stdio.h>

int main(int argc, char* argv[]) {
    printf("Hello");

    return 0;
}
                                
                                Code copied
                            

Looking at line 1, we have a octothorpe (hashtag) to the left of the word include. This is followed by stdio.h which is surrounded by less than and greater than signs. The octothorpe character is a special command line that controls the preprocessor. The C preprocessor processes the source text of a C program before the compiler reads the actual source program. The text that immediately follows is called a directive. In this case our directive is include. What include dictates is that it inserts text from another source file. That source file name that we are calling from is stdio.h. The stdio.h, or standard library, is provided by the installation of GCC. If you would like more information on GCC and its installation process, please visit here. This library supplies our program with a long list of library functions, but the one we are interested in using is printf. I would suggest commenting out the first line (insert // to the left of the octothorpe), and seeing the warning that occurs when compiling. To compile, make sure that GCC is installed by typing gcc -v, which should return a version of GCC that is on your device. To keep things simple we will just type gcc followed by the .c name given to the program.

c compilation
c warning

The error that we see specifies a warning that there is an implicit declaration of a built-in function. This is the reasoning behind including it in our c program. In order to execute a C program, we must include a main method that is expected by the runtime library. Main has two arguments, the first one being argc, which is the number of arguments present upon execution. This will contain one argument, which is our executable file named a.exe. The second argument is an array of character pointers which again, will only hold one character array called a.exe. We will take a look at how to use these two arguments in another blog post. Inside of our main function is the call to printf that prints the text that is surrounded by double quotation marks. Finally, we reach our return statment that returns an exit call. This exit call is 0, which means that it has succeeded. It takes some practice, but the best way to learn in C is to break your code and start reading the warnings and errors. This will not only increase your knowledge of this specific language, but it will also make debugging much simpler in the future.