Compiling and Running your program
So far we have discussed about C, features of C, structure of a C program, library function printf( ) and how to program in C language etc. So now we are moving into more detailed explanation. You can use Turbo C or Borland C compilers for your program. Both are providing you a way to code your program, compile it and execute the program. If you are using 'Borland C' then linking header files is necessary. Turbo C won't warn if you are not linked header files into your program. So now we can see a sample program as a step to explore the power of C language. So open your C editor (Turbo or Borland) and start coding.
Learning by Example I
Qn: A Program to print your Name on the Screen
Learning by Example I
Qn: A Program to print your Name on the Screen
/*Program to print name on the screen */
#include<stdio.h>
main( )
{
printf("Your Name");
}
So you've done! Now you can compile your program to convert it into object code. For that select Compile - > Compile; then a compiling box will be displayed on the screen showing file name, lines compiled, number of warnings & errors if any in the program. If your program has successfully compiled then you can turn into execute or run the program. If there are any errors in the program, number of errors will be displayed on the compiling window and when you hit 'Enter' key, details of the errors listed in the message window; select the error message using up/down arrow keys and hit 'Enter' key to locate the error instruction, correct the errors and compile it.
For executing the program select Run->Run from the menu. You r editor will suddenly blink and return back to the editor window where you've coded your program. So your program has been executed, you can view the output of the program by pressing Alt+F5 keys. You can now see 'Your name' (whatever given in printf) printed on the screen. Ok, now lets have a look at our code:
We have written a comment line on the first line. In link section we linked header file
If you again run your program, output will be displayed on the screen with the old program output. So you may be want to print your outputs on a clean window, you can clear your screen before you are printing any output. For this you can use a library function 'clrscr( )'. Just put this function in your program where you want to clear your screen. clrscr( ) function's header file is
It may be disturbing everytime pressing Alt+F5 keys to view the output. Why we are redirected to the editor window after program run? Because process control executes each line of instructions and when comes out of the closing brace, program terminates and get back to the coding window. You can pause the program termination by just putting getch( ) function just before the closing brace. getch( ) is actually a function which accepts a value from the keyboard. So if you insert a getch( ) function as your last instruction, the program control will wait for a value to be inputted to the getch( ); so till entering a value, you can view your program outputs on the screen. By this you can avoid the usage of hitting Alt+F5.
So from the above modification our program will be look as follows:
/*Program to print name on the screen */
#include<stdio.h>
#include<conio.h>
main( )
{
clrscr( );
printf("Your Name");
getch( );
}





