If you are a windows programmer, Linux programming might seem a bit daunting at first but it really isn’t. Most of you will feel pretty comfortable with, or just a bit awkward mostly for using the shell. In Linux, it’s quite standard that C programs are compiled under the GNU C Compiler named GCC. As with every GNU tool, you can download this for free in your Linux machine, maybe even using your package manager(synaptic for instance) or shell (apt-get install gcc) for Debian-Ubuntu users (like me).
If you know C Programming, or at least the basics of it, you will easily understand the following snippet of code. Let’s see it together and I will explain more below:
Code: Select all
#include <stdio.h>
main()
{
printf("My first C programme in Linux !");
}
This is the standard way to print something at a c program. We just include the file stdio.h which is included so that we can use the printf command. We open the main function, which is the entry point of our program and we just print the string “C Programming For Linux – My first program !” also appending for a new line after the string. main() returns void, which means that it returns nothing and it receives no arguments.
Let’s save this code with the filename example.c. In order to compile this little program, we use the command:
We call gcc with the -o switch which means something like -output and finally we pass the source filename. So to remember which one is first, the binary filename or the source code filename, just remember that -o stands for -output, the executable.
Of course, in order to do that you need to have opened the shell of your linux distribution. If you don’t know much about the shell, i’ll be writing an article soon to explain more about it. For now, you just need to now the cd command, the command that is used to change directory. So, if you created the source file at your desktop, you can open your shell and write “cd Desktop” and then execute the command above.
After you execute the command you now have an executable file named example. You can execute it by giving the command:
And it now prints “My first C programme in Linux !” at your command line.