Commonly, the first program written in a programming language is a “Hello World”, or “Hello World. Time to learn to use some C language to program. Start by opening your notepad, or any other plain text editor, and save this in a file named Hello.c:
#include <stdio.h>
/* This program will write "Hello world" to the console */
int main()
{
printf("Hello world!\n");
return 0;
}
The text of a C program is also known as the program code, or. simply, source code. This code is the program written in a programming language, such as Rust, PHP, Java or any other of them (in our case, the C language).
Shall we now analyze this code a little more specifically?
Line 1 represents the import of a library that will be used in the program (in our case, the stdio library, or “standard input and output”). This library will be responsible for writing things to the screen or capturing user-submitted data.
Line 2 is a comment, a code snippet that will not be used by the compiler, it could be documentation, name of the program author, description of functionality or anything else.
Line 3 represents the initialization of the main function, or main function. Everything inside it will run when the computer runs that program. The contents of this function are enclosed in braces { and }, on lines 4 and 7. The keyword int says that this function will return an integer (just like a math function), and the value 0 is used ( zero) as the default return value for the main function whenever there are no errors.
Line 5 calls the printf function, which will write its content to the console, in this case our “Hello, World”.
Line 6 indicates the function return. As we had no errors in the execution, we will return 0 (zero).
Compiling the program on Linux is quite simple. Open the folder where you have saved your file, and use the following command:
$ gcc Hello.c -o Hello
And to run it, just use the following command:
$ ./Hello
And that's it, the message Hello, world! will appear on your screen.
Did you like the tutorial? Was there any doubt? Do you have any add-ons? Leave it in the comments 😄🥰