First Simple Program
Rules for writing the program
- Preprocessor statements must be included at the beginning of the program like include <stdio.h>, <conio.h> etc.
- Every program should have one and only one main() function at the start.
- All the keyword names must be in lower case.
- A series of statements in a C program forms instructions to the computer, which should be in proper order.
- Each statement must end with a semicolon (;).
- Source code file should be saved with .c extension.
- Open the Turbo C editor and create a new file.
- Copy and paste the source code shown below or type it as it is.
- Save the file with name "firstprogram.c" or any other name you wish.
- Compile the program using Alt + F9, a window will appear showing you the result success or error.
- If there is an error, check it whether something is typed wrong.
- After success its time to run the program with Ctrl + F9, which will show you the output of the program.
//First simple program in c
- #include <stdio.h>
- #include <conio.h>
printf("\nEnter any character:");
printf("Entered character is:%c",ch);
Program working steps
- First line of the program is the comment, Its for user purpose to know what the program is all about.
- In second and third line #include is a preprocessor directive which tells the compiler to include stdio.h & conio.h header file into the source file. We have used these two header files because definition of functions like clrscr(), printf(), scanf() and getch() is defined over there.
- void main() is the function where execution of the program starts. void return type describes the main function doesn't return any value, you can also use int main() where the function will return integer value. Function starts with opening curely bracket and ends with closing bracket, in between what we write is the code which will be executed by the comipler.
- Character variable ch is declared to store the input typed by the user.
- clrscr() function clears the screen, like cls in windows and clear in linux.
- Nineth line prints the character quoted in double quotes i.e. Hello world.
- Escape sequence '\n' is used in printf function to print text on the next line.
- Scanf statements waits for the input character to be entered by the user. After entering the character it stores the character value in variable ch at location address pointed in &ch.
- Twelth line statement prints the character value of variable ch.
- getch() function takes the single input character from the keyboard and that character is not display on the console screen
- At closing curly bracket, the program ends.
Use debugging feature
To know how your program is executing step by step and to locate errors in the program.
By pressing F8 function key it starts debugging, if needed the program is recompiled and then frist line of the program which is to be executed is highlighted.
Each time when you press F8 key the highlight moves to the next line and continue pressing F8 key till last line of the program is executed.