What is a program?
A Program is a set of instructions given to the computer in the form of code. The computer calculates this set of code and gives us an output. but the computer can only understand the code in binaries (or Machine Language). If we start to convert each set of code into binary it will take too much time, that's why we use Compiler.
What is a Compiler?
A compiler is a special program which helps us in converting this set of code in Binary. For Ex. Borland Turbo C, Which we are going to use most of the times to write our programs.
There are also compilers like GNU, Codeblocks and Dev C++ which you can use. Download the software you are most comfortable with and start writing the C programs.
Now Let's Look at our first c program 'Hello World'.
Hello World C Program:
#include<stdio.h> #include<conio.h> void main() { clrscr(); printf("\n Hello World"); getch(); }
Output :
In this way "Hello World" gets displayed on the output screen. Now let's understand the code a bit.
First we start with
#include<stdio.h> #include<conio.h>
These are the header files which we need to write at the beginning of each program. These Files contain some inbuilt functions we use in the program Like printf (),scanf(), clrscr() and getch().
If we do not write this at the beginning of the program, we will get an error: function should have a prototype in turbo c.
Next is
If we do not write this at the beginning of the program, we will get an error: function should have a prototype in turbo c.
Next is
void main() { printf("Statements you want to print on the screen") ; }
This is the main function section, a program start to execute from the main function.
Printf function in C
printf() function is used to display the message on the output screen, whatever we write inside the double quotes gets printed on the screen.
In this program, we have written Hello world inside double quotes and that got displayed on the output screen. You can write whatever you want in double quotes and that will be displayed, amazing right? Try it yourself !
In this program, we have written Hello world inside double quotes and that got displayed on the output screen. You can write whatever you want in double quotes and that will be displayed, amazing right? Try it yourself !
Moving ahead, we have also used clrscr() and getch(), these are the functions from <conio.h> header file. clrscr() clears the screen and getch() holds the output on the screen until we press any key.
Tip for Users Other than Turbo C:
Unlike Borland Turbo C 'conio.h' header file can't use in other compilers mentioned above, so you have to change the code a bit like so:
#include <stdio.h> main() { printf("\n Hello World"); return 0; }
Output: Hello world
How to Compile and Execute the programs?
In Borland Turbo C the steps to compile and Execute are
1. Press F2 key and give a file name with '.c' extension. ( for ex. Hello.C)
2. Press ALT + F9 to compile and CTRL + F9 to run the program.
3. Press ALT + F5 to view the output.
For Other Users, there will be a different method to compile and execute the program refer to the help section for the respective compilers. I recommend you to use the Turbo C to develop the programs as it is easy to use, you can download it here.
With this said, Hope you have understood How to write your first c program in C. If you have any doubt, write below in the comment section lets discuss.