First Sample C Program - C Language Tutorial

Description

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.

Procedure

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.

Source code

  1. //First simple program in c
  2. #include <stdio.h>
  3. #include <conio.h>
  4. void main()
  5. {
  6.    char ch;                
  7.    clrscr();
  8.    printf("Hello world");  
  9.    printf("\nEnter any character:");
  10.    scanf("%c",&ch);        
  11.    printf("Entered character is:%c",ch);   
  12.    getch();
  13. }

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.

Thank you for sharing Your Knowledge with others who need It.-Team LetML

Post a Comment

Copyright © LetML. Blogger Templates Designed by OddThemes