Monday, November 11, 2013

Creating your first C program...

Closing unwanted windows: After opening Turbo C editor, if you observe that there are several unwanted programs opened on your screen, then first of all, close them using following techniques.
Press ALT+W to invoke the Windows menu and then choose Close all option or press hotkey e (The red color letter “e” in the close all menu option.) This procedure will close all open windows on the screen.
OR
You can close individual window by using Alt+F3 shortcut key combination.

Creating new program file:
If you are using mouse (generally, in windows environment) the click on the File menu and click on New option.

If you are keyboard lover then press ALT+F to popup the file menu and then choose New. A new program window will appear on the screen. Type the following code.

Program #1
#include <stdio.h>
main()
{
                printf(“One who makes no mistakes never makes anything.” );
}

Warning: C is a case sensitive language, therefore it is necessary to write programs in lowercase letters. Whereas strings, symbolic constants, variables can be written in both uppercase and lowercase letters or the combination of both.
So type the code exactly as written above. Note that the only text i.e. string “One who makes…” written in double quotation marks can be typed as you like.

Saving the program file: 
To save the program file, you can use one of the following methods:

Method 1: Press ALT+F to popup the file menu and then choose Save option. Specify the filename, say “Prog1.c” after removing the default file name and press Enter.

Method 2: Press F2 function key to open save dialog box. Remove the default filename using backspace key and then write a new name (E.g. Prog1.c).
Note that the filename should not be exceeding than 8 characters and all DOS filename rules are applicable.

Compiling the program
Before running a program directly, it is good idea to compile it first. To compile the program use ALT+F9 function key or press ALT+C to popup Compile menu. Choose Compile option. Checkout the errors if any. If there are errors in your program, don’t worry! Make sure that you have typed the exact code as shown above. Also make sure that you have written the whole program in lowercase letters (excluding text in “” marks.). After removing all errors, save the program again by pressing F2. Your program is error free now! Get ready to run your program!

Executing the program
Using CTRL+F9 you can execute your program. Or use ALT+R to evoke Run Menu. From the Run menu, select Run option. No output? And you are back in editor window?
What’s going wrong? Relax!! nothing’s gone wrong. Then what happens?
Actually your program displays the output and suddenly come back to TC editor. This happens in a flash, so you are not able to see the output. Press ALT+F5 to display output screen. Congratulations!! you have created your first C program and successfully execute it.

Understanding the program
Now, watch carefully at our source code line by line.
Line 1:   #include <stdio.h>
Line 2:   main()
Line 3:   {
Line 4:        printf(“One who makes no mistakes never makes anything.” );
Line 5:   }

Output:
#------------------------------------------------------------------------------#
One who makes no mistakes never makes anything.
#------------------------------------------------------------------------------#
Line 1: #include option is used to include the file in our C program. In the above program we can include “stdio.h”, which is header file inbuilt with Turbo C compiler and contains the definitions of several standard library functions including printf(). Note that “f” in printf() stands for formatted output. So, it is necessary to include each and every file containing function definition of respective function in the program.

Line 2-3: Indicates beginning of every C program. main() is a special function. Remember that, execution of your program always starts with main() function.

Line 4: It uses standard library function printf(). To display the message on the screen. You can write this message in uppercase, lowercase or combination of both. You can also include numbers and special characters in the message.

Line 4: Indicates the end of C program.

So, it is obvious that, C program is a collection of several functions. In our program, we use printf() function to print the message on the screen. main() is also one of the user defined function. Now, look at the following program:

Program #2
main()
{
}

What? Neither #include statement, nor any functions, even there is no any statement in main() ! Surprising…! But it is absolutely valid. You can also write the above same code in a single line. Like this.
main(){}
Remember that C is free form language i.e. C has no specific rules for the position at which a statement is to be written. Save the program, compile and run it. Obviously it will not produce any output, nor it will give any errors.

So, now you can put your name in “Limca Book of World Records” ! How? Because you have just written the World’s smallest C program.

No comments:

Post a Comment