Wednesday 17 February 2016

How to run a basic C program in LINUX

This post show how to run a basic C program in Linux.

WRITING THE PROGRAM:
1. Open your HOME folder and create a new text file by RIGHT CLICK -> CREATE NEW DOCUMENT -> EMPTY FILE.  Name the file as "myprogram.c".
You can give any name of your choice with the ".c" extension.
                                 OR
Open TERMINAL (Ctrl+Alt+T). Open a file using your text editor command.
For UBUNTU : gedit myprogram.c
For LINUX MINT : pluma myprogram.c






2. Type the program into the file you just created.
Eg.

#include<stdio.h>
int main()
{
     printf("\n Hello Linux world");
}

3. Save the the file.
RUNNING THE PROGRAM
4. Go to the terminal and compile the program by typing "gcc" followed by the name of the program.
    Eg. gcc myprogram.c

gcc is the default compiler installed in Linux.

5. After running the above command, an executable file named "a.out" will be created in your HOME directory if your program compiles successfully without any errors. If any errors are shown, correct it in your program.

6. Run the executable file by typing "./a.out" in the Terminal. The output will be displayed.

Note: Whenever you compile any program, the name of the executable file is always "a.out" by default. if you want to specify the name of the executable file of your choice, then you can do so by typing "gcc myprogram.c -o myoutput". The name after "-o" specifies the name of your executable file. It can be any name of your choice. After doing so you can run the program as "./myoutput"   (Note that you don't have to specify the ".out" extension in this case).

For more detailed demonstration please watch my video below:

No comments:

Post a Comment