Wednesday 18 April 2012

First C Program




Now, after all the introduction, I will show you how to make your first C program.

At the top of the program, you should write:

void main()

void means that the program returns nothing.
main() is the starting of every program.


Then, put a Left Brace (Curly Bracket):

{

After that, type clrscr();

clrscr(); function clears the screen when we runt the program and displays data what we have entered. If we do not put clrscr(); function, the the data will be displayed multiple times, when we run the program many times.

Now, type:

printf("Hello World");


printf is function which displays the data on screen.
The data you want to display should be enclosed by (" ")
And at last of the statement, don't forget to put a ;
; is put at the end of every statement.
At the end put a Right Brace (Curly Bracket)
Now, your program will look like this:



Click on File > Save > HelloWorld.c (Don't forget to put the extension .c) or Save using the shortcut F2.

Compile the program by clicking on Compile > Compile or Compile using the shortcut Alt + F9.

Now, we have to run this program. Click on Run > Run or Run using the shortcut Ctrl + F9.

The program runs, but we cannot see it because it runs and closes immediately.

To correct this, we should put getch(); function before the right curly bracket.

By using getch() function, the screen is displayed and it does not closes until we press a key from our keyboard.Now, save again and run the program.

It will show like this:



This is called user screen.

Now, when you press any key, this user screen will close and you will return to your program.

Now, your first C program is ready.

If you face any problems, then comment below.

0 comments: