Writing Your First C program - "Hello World" Example

No comments
Now it’s the time to dip your fingers in the ‘Sea Of 'C'. Here we are going to code our first basic C program and compile it and run it on our system. This basic C program will help you in knowing the basic and necessary contents of a C program.In this program we will write the code to print a string "Hello World" on the output screen.



After writing the program, we will compile it and run it to get the desired output.So by the end of this tutorial you will be fully equipped with the knowledge- how to write ,compile and run a C program.

Where to write –

You can use any text editor like Notepad to write your program or Turbo C has an inbuilt Turbo C++ IDE which you can find in the bin folder.(Many other IDEs are available on the web to use).It is recommended for beginners to write the code in a simple text editor like notepad.
Program 1-Printing the “Hello World”
#include <stdio.h>
#include <conio.h>

void main(){
/*My first c program*/
printf("Hello World \n");

}
Before compiling and running our program let’s have a look ,what’s written in this program.
First and 2nd line - #include
-This is pre-processor command which is used to add library files before compilation.
-Here we have added two files stdio.h(standard input/output header file) and conio.h(console input/output header file).
-We can add more files according to our need
3rd line-void main()
-It is a function(we will look into functions later) main which starts the execution of the program.
-The program which we want to execute is written within { } clause ,after void main().
-Anything written inside { } gets executed and this is called the body of main();
4th line- /*My first c program*/
-Any thing written inside /* */ is a comment.
-Comments are added to the program for making it more readable.
-Anything written within it is excluded during the compilation.
5th line- printf("Hello World \n");
-printf() is a function used to print anything and is defined in the C library in stdio.h header file.
-This function is used to print any character, string, float, integer, octal and hexadecimal values on the screen.
-Anything written within “ ” gets printed on the output screen.(we will discuss more about it later).
-Here in this program it is used to print Hello World. (you can try writing different strings you want to print on your own).
Write the above program using a suitable text editor and save it with any name but with .c extension, (mine is hello.c), which is a necessary step.
Now we have a .c file with a C program written inside,the code written inside this file is called the source code.Now we need to compile this .c file to get an executable file which we can run to get the desired output.

Compiling A C program- compiling hello.c

We have already installed a C compiler in our system, which we will use to compile the .c file(hello.c program).Follow these steps to compile the program-
(here we will use command line to compile and run our program with gcc compiler.)
Step 1- Open command prompt in windows (windows+r, and type cmd then Enter).
Step 2- Using command line go to the directory (folder) where you have saved your program file(hello.c file). I have saved it in C:\c programs.
Step 3- Run the following command to compile the hello.c program gcc hello.c
Step 4- Now if there is no error in your program ,your program will be compiled and a.out file will be created , which we can run.

Running A C Program-

Step 1- Type the following command on command prompt after compiling your hello.c program-
.\a.exe
Step 2- Now your program starts executing and you will see “Hello World” printed in the next line.
Screenshot is given below for your convenience-
After this tutorial as promised ,we are fully equipped with the knowledge- how to write ,compile and run a C program.

No comments :

Post a Comment