Creating, Compiling and Running a C program in C

To create a C program in linux environment, firstly we need to open the terminal by typing Ctrl+Alt+T or open terminal application using GUI. After that we can create a C program using the following editor command with a filename having extension .c:

gedit filename.c

As soon as we type the command a text-editor window opens up. We type our program in the window, save and then close the window.

Then to compile the program we type the following command:

gcc -o filename filename.c

If there is any error in the program, we need to edit the program again using the editor command otherwise we need to run the output file generated after compiling.We type the following command to run the program:

./filename

Note:For linking the math functions from the standard C library i.e. the program includes the math.h library then we use the following command to compile the program.

gcc -o filename filename.c -lm

First Program in C

Consider the following program written in C language:

 
main()
{
	/*.....printing begins....*/
	printf("Hello,\nI am there.");
	/*.....printing ends......*/
}

The program when executed gives the following output:

Hello,
I am there.

Now, let us have a look at the program. The first line of the program informs the sytem that program is main and the execution begins at this line. The main( ) is a special function used by C to indicate the start of program. Every program must have exactly one main function.
The opening brace “{” indicates the opening of the function main and the closing brace “}” indicates the closing of the function. The part inside the braces is known as the body of the program which contains statement which contains code to be executed. The end of statement is marked by a semi-colon “;” .

Any thing written between “ /*” and “*/” is ignored by the C-compiler and is known as comment. Comments do not affect the speed and size of the program. This is used by programmers to increase the readability of the program. It is helpful in debugging and testing.

The printf() function is a pre-defined standard C function which prints everything passed within the double quotes ” ” as shown below:

printf("Hello,\n I am there.");

It is to be noted that “\n” is a special character in C which informs the compiler to go the next line. It works on the concept of carriage return key on the keyboard. So in this case the message written after this is printed in a new line and so the output.

Hello,
I am there.

The printf() function is pre-defined in stdio.h header therefore in order to use the functions such as printf and scanf we must declare it by including the following line in the program at the beginning otherwise a warning is generated at the compile time.

include<stdio.h>

We should also take care that by default main function returns integer value. So “return 0;” statement should be added in the program or use void keyword which tells the compiler that program returns no value.