c programming for Linux

We will start with hello world in C and explain the structure of the program


#include <stdio.h>

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

    return 0;
}

Now lets explain the code line by line.
The first line is #include and it will load the standard input and output library.

The second line is the int main(int argc, char *argv[]){ the main is the name of the function from which the program start.
Each program must have one main function
The int mean that the function return integer number to the OS.
The printf is used to send to text to the standard output, it will send the Hello world and the \n mean a new line.

The return 0, will return zero to the OS.
Eventually we have closing curly brackets.

Say we save this in a text file that is called start.c in order to compile it to executable code we need to run:
$ gcc start.c -o start

And it will compile it to an executable file called start. This is because the “-o” (short for output) tell the gcc compile what should be the name of the output file.

So we have created out first simple program in c.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s